/
subzer.go
230 lines (174 loc) · 5.31 KB
/
subzer.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
package main
import (
"bufio"
"container/list"
"fmt"
flags "github.com/jessevdk/go-flags"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
type SrtBlock struct {
number uint64
start time.Duration
end time.Duration
text []string
}
func NewSrtBlock(number uint64, start, end time.Duration, text []string) *SrtBlock {
b := SrtBlock{number, start, end, text}
return &b
}
func (s *SrtBlock) String() string {
return fmt.Sprintf("%#v", s)
}
func (s *SrtBlock) TextAsLine() string {
return strings.Join(s.text, " ")
}
func (s *SrtBlock) StartSeconds() uint64 {
return uint64(s.start.Seconds())
}
func (s *SrtBlock) EndSeconds() uint64 {
return uint64(s.end.Seconds())
}
func SrtBlockParse(lines []string) *SrtBlock {
if len(lines) < 3 {
panic("Block should have at least 3 lines.")
}
number, _ := strconv.ParseUint(lines[0], 10, 64)
durations := strings.Split(lines[1], " --> ")
if len(durations) != 2 {
panic("Block should have two timestamps.")
}
start := ParseTimestamp(durations[0])
end := ParseTimestamp(durations[1])
text := lines[2:]
return NewSrtBlock(number, start, end, text)
}
func ParseTimestamp(timestamp string) time.Duration {
pieces := regexp.MustCompile(":|,").Split(timestamp, 4)
hours, _ := strconv.ParseUint(pieces[0], 10, 8)
minutes, _ := strconv.ParseUint(pieces[1], 10, 8)
seconds, _ := strconv.ParseUint(pieces[2], 10, 8)
milliseconds, _ := strconv.ParseUint(pieces[3], 10, 16)
return time.Duration(hours)*time.Hour +
time.Duration(minutes)*time.Minute +
time.Duration(seconds)*time.Second +
time.Duration(milliseconds)*time.Millisecond
}
func ListToSlice(list *list.List) []string {
slice := make([]string, list.Len())
i := 0
for e := list.Front(); e != nil; e = e.Next() {
slice[i] = e.Value.(string)
i++
}
return slice
}
func ParseSrtStream(source io.Reader) *list.List {
reader := bufio.NewScanner(source)
blocks := list.New()
for {
block_lines := list.New()
for reader.Scan() {
line := reader.Text()
if line == "" {
break
}
block_lines.PushBack(line)
}
if block_lines.Len() == 0 {
break
}
srt_block := SrtBlockParse(ListToSlice(block_lines))
blocks.PushBack(srt_block)
}
return blocks
}
func WriteFileLine(writer io.Writer, seconds uint64, text string) {
line := fmt.Sprintf("%06d\t%s\n", seconds, text)
io.WriteString(writer, line)
}
func ConvertSrtStream(filename string, source io.Reader, writer io.Writer) uint64 {
blocks := ParseSrtStream(source)
WriteFileLine(writer, 0, filename)
var end_seconds uint64 = 0
for e := blocks.Front(); e != nil; e = e.Next() {
block := e.Value.(*SrtBlock)
WriteFileLine(writer, block.StartSeconds(), block.TextAsLine())
end_seconds = block.EndSeconds()
}
return end_seconds
}
func WriteTotalSeconds(seeker io.Seeker, writer io.Writer, seconds uint64) {
io.WriteString(writer, fmt.Sprintf("%06d", seconds))
}
func StreamConversion(destination_name string, source io.Reader, writer io.Writer, seeker io.Seeker) {
seconds := ConvertSrtStream(destination_name, source, writer)
seeker.Seek(0, 0)
WriteFileLine(writer, seconds, destination_name)
}
func ProcessFile(source_path string) {
destination_path := regexp.MustCompile("srt$").ReplaceAllString(source_path, "txt")
destination_name := filepath.Base(destination_path)
source, err := os.Open(source_path)
if err != nil {
panic("Cannot open source file.")
}
defer source.Close()
destination, err := os.Create(destination_path)
if err != nil {
panic("Cannot open source file.")
}
defer destination.Close()
StreamConversion(destination_name, source, destination, destination)
}
func main() {
var opts struct {
File string `short:"i" description:"File input"`
Directory string `short:"r" description:"Folder input"`
}
parser := flags.NewParser(&opts, flags.Default)
parser.ApplicationName = "subzer"
parser.Usage = "[OPTIONS]"
_, err := parser.Parse()
if err != nil {
os.Exit(1)
}
file := opts.File
dir := opts.Directory
if len(file) > 0 && len(dir) > 0 {
fmt.Println("Error: input a file or a directory, not both.")
os.Exit(1)
}
if len(file) == 0 && len(dir) == 0 {
parser.WriteHelp(os.Stdout)
os.Exit(1)
}
files := list.New()
if len(file) > 0 {
if _, err := os.Stat(file); err != nil {
fmt.Printf("Error: file \"%s\" does not exist.\n", file)
os.Exit(1)
}
files.PushBack(file)
}
if len(dir) > 0 {
if _, err := os.Stat(dir); err != nil {
fmt.Printf("Error: directory %s does not exist.\n", dir)
os.Exit(1)
}
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".srt" {
files.PushBack(path)
}
return err
})
}
for e := files.Front(); e != nil; e = e.Next() {
ProcessFile(e.Value.(string))
}
}