-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
288 lines (248 loc) · 5.44 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
)
var globalOffset = flag.Int64("offset", 0, "global offset of subtitle, in case that srt file does not exactly match video.")
func init() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "%s [flags] <file> [mm:ss]\n\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "Flags:\n")
flag.PrintDefaults()
}
}
type Entry struct {
Idx uint64
Start int64
End int64
Text string
}
// srt subtitle use timestamp format 'hh:mm:ss,milli'
func parseTimestamp(s string) (int64, error) {
var millsecs int64
var hours int64
var minutes int64
var seconds int64
var err error
parts := strings.Split(s, ",")
if len(parts) == 2 {
millsecs, err = strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return 0, err
}
}
parts = strings.Split(parts[0], ":")
if len(parts) != 3 {
return 0, errors.New("invalid timestamp: " + s)
} else {
hours, err = strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return 0, err
}
minutes, err = strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return 0, err
}
seconds, err = strconv.ParseInt(parts[2], 10, 64)
if err != nil {
return 0, err
}
}
return hours*60*60*1000 + minutes*60*1000 + seconds*1000 + millsecs, nil
}
var validIdx = regexp.MustCompile(`^[0-9]+$`)
func readEntry(s *bufio.Scanner) (*Entry, error) {
var idx uint64
var err error
// Idx
if s.Scan() {
text := s.Text()
if !validIdx.MatchString(text) {
return nil, errors.New("invalid idx: " + text)
}
idx, err = strconv.ParseUint(text, 10, 0)
if err != nil {
return nil, err
}
} else {
return nil, nil
}
// timestamp lines are in format 'start --> end'
var start int64
var end int64
if s.Scan() {
texts := strings.Split(s.Text(), " --> ")
if len(texts) != 2 {
return nil, errors.New("invalid timestamp")
}
start, err = parseTimestamp(texts[0])
if err != nil {
return nil, err
}
end, err = parseTimestamp(texts[1])
if err != nil {
return nil, err
}
} else {
return nil, errors.New("invalid input: no timestamp after idx")
}
// text
lines := make([]string, 0)
for s.Scan() {
text := s.Text()
if text == "" {
break
} else {
lines = append(lines, text)
}
}
// TODO: handle scan error
if len(lines) == 0 {
return nil, errors.New("invalid input: no text")
} else {
return &Entry{
Idx: idx,
Start: start,
End: end,
Text: strings.Join(lines, "\n"),
}, nil
}
}
const OFFSET_HEADER string = "OFFSET:"
func tryParseGlobalOffset(s *bufio.Scanner) (*int64, error) {
if s.Scan() {
text := s.Text()
if strings.HasPrefix(text, OFFSET_HEADER) {
offset, err := strconv.ParseInt(strings.TrimPrefix(text, OFFSET_HEADER), 10, 64)
if err != nil {
return nil, err
}
return &offset, nil
}
}
return nil, nil
}
func parse(r io.ReadSeeker) ([]*Entry, error) {
entries := make([]*Entry, 0)
scanner := bufio.NewScanner(r)
offset, err := tryParseGlobalOffset(scanner)
if err != nil {
return entries, nil
}
if offset != nil {
*globalOffset = *offset
} else {
// reset reader and scanner if the first line is not global offset
r.Seek(0, io.SeekStart)
scanner = bufio.NewScanner(r)
}
for {
entry, err := readEntry(scanner)
if err != nil {
return entries, err
}
if entry == nil {
break
}
entries = append(entries, entry)
}
return entries, nil
}
// mpc outputs current time of song in 'hh:mm' format, this function
// parses it into milliseconds
func parseQuery(s string) (int64, error) {
parts := strings.Split(s, ":")
var minutes int64
var seconds int64
var err error
minutes, err = strconv.ParseInt(parts[0], 10, 32)
if err != nil {
return 0, err
}
seconds, err = strconv.ParseInt(parts[1], 10, 32)
if err != nil {
return 0, err
}
return minutes*60*1000 + seconds*1000, nil
}
var readQueryEOF = errors.New("EOF")
func readQuery() (int64, error) {
s := bufio.NewScanner(os.Stdin)
if !s.Scan() {
return 0, errors.New("can not read query")
}
text := s.Text()
if s.Text() == "" {
return 0, readQueryEOF
}
return parseQuery(text)
}
func queryAndPrint(entries []*Entry, query int64, offset int64) {
for _, e := range entries {
// +/- 1 second to make the search more tolerant
if e.End+offset+1000 >= query && e.Start+offset-1000 <= query {
fmt.Println(e.Text)
}
}
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
fmt.Println("Usage: srt <file> [mm:ss]")
os.Exit(1)
}
f, err := os.Open(args[0])
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
entries, err := parse(f)
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
if len(entries) == 0 {
fmt.Println("No entries found, now existing")
os.Exit(0)
}
stat, _ := os.Stdin.Stat()
if len(args) >= 2 {
query, err := parseQuery(args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
queryAndPrint(entries, query, *globalOffset)
} else if stat.Mode()&os.ModeCharDevice == 0 {
query, err := readQuery()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
queryAndPrint(entries, query, *globalOffset)
} else {
// interactive mode
for {
fmt.Print("Input timestamp: ")
query, err := readQuery()
if err != nil {
if errors.Is(err, readQueryEOF) {
os.Exit(0)
} else {
fmt.Println(err)
os.Exit(1)
}
}
queryAndPrint(entries, query, *globalOffset)
}
}
}