forked from martinlindhe/subtitles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssa.go
177 lines (139 loc) · 3.25 KB
/
ssa.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package subtitles
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// Subtitle holds a parsed subtitle
type Subtitle struct {
Captions []Caption
}
func looksLikeSSA(s string) bool {
return s[0] == '['
}
// NewFromSSA parses a .ssa text into []Caption, assumes s is a clean utf8 string
func NewFromSSA(s string) (res Subtitle, err error) {
var chunk string
chunk, err = extractSsaChunk("[Events]", s)
if err != nil {
return
}
lines := strings.Split(chunk, "\n")
if len(lines) <= 0 {
return
}
// first line must now be a "Format:"
format := lines[0]
// parse format into column numbers
startCol := parseSsaFormat(format, "Start")
endCol := parseSsaFormat(format, "End")
textCol := parseSsaFormat(format, "Text")
dialogueColumns := columnCountFromSsaFormat(format)
outSeq := 1
for i := 1; i < len(lines); i++ {
seq := strings.Trim(lines[i], "\r ")
if seq == "" {
break
}
start := parseSsaDialogue(lines[i], startCol, dialogueColumns)
end := parseSsaDialogue(lines[i], endCol, dialogueColumns)
text := parseSsaDialogue(lines[i], textCol, dialogueColumns)
if len(start) <= 0 ||
len(end) <= 0 ||
len(text) <= 0 {
continue
}
var o Caption
o.Seq = outSeq
o.Start, err = parseSsaTime(start)
if err != nil {
fmt.Println(err)
continue
}
o.End, err = parseSsaTime(end)
if err != nil {
fmt.Println(err)
continue
}
o.Text = strings.Split(text, "\\N")
if len(o.Text) > 0 {
res.Captions = append(res.Captions, o)
outSeq++
}
}
return
}
// return column idx from s
func parseSsaDialogue(s string, idx int, columns int) string {
pos := strings.Index(s, ": ")
if pos == -1 || pos+2 >= len(s) {
return ""
}
s = s[pos+2:]
cols := strings.SplitN(s, ",", columns)
if idx >= len(cols) || idx <= 0 {
return ""
}
return strings.TrimSpace(cols[idx])
}
func parseSsaFormat(s string, colName string) int {
pos := strings.Index(s, ": ")
if pos == -1 || pos+2 >= len(s) {
return -1
}
s = s[pos+2:]
cols := strings.Split(s, ",")
for idx, col := range cols {
col = strings.TrimSpace(col)
if col == colName {
return idx
}
}
// -1 if not found
return -1
}
func columnCountFromSsaFormat(s string) int {
pos := strings.Index(s, ": ")
if pos == -1 || pos+2 >= len(s) {
return -1
}
s = s[pos+2:]
cols := strings.Split(s, ",")
return len(cols)
}
func extractSsaChunk(chunk string, s string) (string, error) {
pos := strings.Index(s, chunk)
if pos == -1 {
return "", fmt.Errorf("Parse error in chunk")
}
// XXX this will break if there is a chunk after [Events]
res := s[pos+len(chunk):]
return strings.Trim(res, "\r\n "), nil
}
func parseSsaTime(in string) (time.Time, error) {
// "0:01:06.37" => h:mm:ss.cc (centisec)
r1 := regexp.MustCompile("([0-9]+):([0-9]+):([0-9]+)[.]([0-9]+)")
matches := r1.FindStringSubmatch(in)
if len(matches) < 5 {
return time.Now(), fmt.Errorf("[ssa] Regexp didnt match: %s", in)
}
h, err := strconv.Atoi(matches[1])
if err != nil {
return time.Now(), err
}
m, err := strconv.Atoi(matches[2])
if err != nil {
return time.Now(), err
}
s, err := strconv.Atoi(matches[3])
if err != nil {
return time.Now(), err
}
cs, err := strconv.Atoi(matches[4])
if err != nil {
return time.Now(), err
}
return makeTime(h, m, s, cs*10), nil
}