-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytcc-fix.go
88 lines (70 loc) · 1.69 KB
/
ytcc-fix.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
package main
import (
"bufio"
"flag"
"os"
"./timestamp"
)
// NEWLINE neue Zeile
const NEWLINE = "\n"
// OFFSET Zeilenabstand zwischen zwei Untertiteln
const OFFSET = 3
var infilename = flag.String("infile", "captions.sbv",
"YouTube captions input file name")
var outfilename = flag.String("outfile", "captions-fixed.sbv",
"repaired output captions")
func check(e error) {
if e != nil {
panic(e)
}
}
func fixTimecode(this, next string) string {
thisStart, thisEnd := timestamp.ReadTwoFromString(this)
nextStart, _ := timestamp.ReadTwoFromString(next)
var newEnd timestamp.Timestamp
// test if we are already not overlapping, so keep it
if thisEnd.Normalize() < nextStart.Normalize() {
newEnd = thisEnd
} else {
// if we are overlapping, just cut it down.
newEnd = timestamp.ReadFromInt(nextStart.Normalize() - 1)
}
return thisStart.AsString() + "," + newEnd.AsString()
}
func main() {
flag.Parse()
lines := make(map[int]string)
infile, err := os.Open(*infilename)
check(err)
outfile, err := os.Create(*outfilename)
check(err)
input := bufio.NewScanner(infile)
// slurp the full input file
i := 0
for input.Scan() {
lines[i] = input.Text()
i++
}
// gou through the input data and fix
// timecodes if needed
for y := 0; y < i; {
if y+OFFSET < i {
_, err = outfile.WriteString(fixTimecode(lines[y], lines[y+OFFSET]))
check(err)
} else {
_, err = outfile.WriteString(lines[y])
check(err)
}
_, err = outfile.WriteString(NEWLINE)
check(err)
_, err = outfile.WriteString(lines[y+1])
check(err)
_, err = outfile.WriteString(NEWLINE)
check(err)
_, err = outfile.WriteString(NEWLINE)
check(err)
y += OFFSET
}
infile.Close()
outfile.Close()
}