-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSrt.go
64 lines (55 loc) · 1.34 KB
/
Srt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package csy
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
type SrtSubtitle struct {
Index int
StartTime string
EndTime string
Content string
}
func ParseSrtFile(filename string) ([]SrtSubtitle, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var subtitles []SrtSubtitle
var currentIndex int
var currentTime string
var currentContent string
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "00:") {
re := regexp.MustCompile(`(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})`)
matches := re.FindStringSubmatch(line)
if len(matches) == 3 {
currentTime = matches[1]
} else {
return nil, fmt.Errorf("无法解析时间戳: %s", line)
}
} else if line != "" {
currentContent += line + "\n"
} else if currentContent != "" {
subtitle := SrtSubtitle{
Index: currentIndex,
StartTime: currentTime,
EndTime: currentTime, // 这里可能需要根据实际情况调整
Content: regexp.MustCompile(`^\d{0,5}\n`).ReplaceAllLiteralString(strings.TrimSpace(currentContent), ""),
}
subtitles = append(subtitles, subtitle)
// 重置变量
currentIndex++
currentContent = ""
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return subtitles, nil
}