98 lines
2.3 KiB
Go
98 lines
2.3 KiB
Go
package ass
|
||
|
||
import (
|
||
"fmt"
|
||
"regexp"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"sub-cli/internal/model"
|
||
)
|
||
|
||
// parseFormatLine 解析格式行中的各个字段
|
||
func parseFormatLine(formatStr string) []string {
|
||
fields := strings.Split(formatStr, ",")
|
||
result := make([]string, 0, len(fields))
|
||
|
||
for _, field := range fields {
|
||
result = append(result, strings.TrimSpace(field))
|
||
}
|
||
|
||
return result
|
||
}
|
||
|
||
// parseStyleLine 解析样式行
|
||
func parseStyleLine(line string) []string {
|
||
// 去掉"Style:"前缀
|
||
styleStr := strings.TrimPrefix(line, "Style:")
|
||
return splitCSV(styleStr)
|
||
}
|
||
|
||
// parseEventLine 解析事件行
|
||
func parseEventLine(line string) []string {
|
||
return splitCSV(line)
|
||
}
|
||
|
||
// splitCSV 拆分CSV格式的字符串,但保留Text字段中的逗号
|
||
func splitCSV(line string) []string {
|
||
var result []string
|
||
inText := false
|
||
current := ""
|
||
|
||
for _, char := range line {
|
||
if char == ',' && !inText {
|
||
result = append(result, strings.TrimSpace(current))
|
||
current = ""
|
||
} else {
|
||
current += string(char)
|
||
// 这是个简化处理,实际ASS格式更复杂
|
||
// 当处理到足够数量的字段后,剩余部分都当作Text字段
|
||
if len(result) >= 9 {
|
||
inText = true
|
||
}
|
||
}
|
||
}
|
||
|
||
if current != "" {
|
||
result = append(result, strings.TrimSpace(current))
|
||
}
|
||
|
||
return result
|
||
}
|
||
|
||
// parseASSTimestamp 解析ASS格式的时间戳 (h:mm:ss.cc)
|
||
func parseASSTimestamp(timeStr string) model.Timestamp {
|
||
// 匹配 h:mm:ss.cc 格式
|
||
re := regexp.MustCompile(`(\d+):(\d+):(\d+)\.(\d+)`)
|
||
matches := re.FindStringSubmatch(timeStr)
|
||
|
||
if len(matches) == 5 {
|
||
hours, _ := strconv.Atoi(matches[1])
|
||
minutes, _ := strconv.Atoi(matches[2])
|
||
seconds, _ := strconv.Atoi(matches[3])
|
||
// ASS使用厘秒(1/100秒),需要转换为毫秒
|
||
centiseconds, _ := strconv.Atoi(matches[4])
|
||
milliseconds := centiseconds * 10
|
||
|
||
return model.Timestamp{
|
||
Hours: hours,
|
||
Minutes: minutes,
|
||
Seconds: seconds,
|
||
Milliseconds: milliseconds,
|
||
}
|
||
}
|
||
|
||
// 返回零时间戳,如果解析失败
|
||
return model.Timestamp{}
|
||
}
|
||
|
||
// formatASSTimestamp 格式化为ASS格式的时间戳 (h:mm:ss.cc)
|
||
func formatASSTimestamp(timestamp model.Timestamp) string {
|
||
// ASS使用厘秒(1/100秒)
|
||
centiseconds := timestamp.Milliseconds / 10
|
||
return fmt.Sprintf("%d:%02d:%02d.%02d",
|
||
timestamp.Hours,
|
||
timestamp.Minutes,
|
||
timestamp.Seconds,
|
||
centiseconds)
|
||
}
|