-
Notifications
You must be signed in to change notification settings - Fork 0
/
concat_subs.go
55 lines (48 loc) · 1.39 KB
/
concat_subs.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
package main
import (
"errors"
"flag"
"fmt"
"os"
"runtime"
"time"
"github.com/Des-Nerger/go-astisub"
"github.com/asticode/go-astilog"
)
func init() {
astilog.SetLogger(astilog.New( astilog.Configuration{Verbose: true} ))
}
func fatalCheck(err error) {
if err != nil {
fmt.Fprintln(os.Stdout, err)
//os.Exit(1)
runtime.Goexit()
}
}
func main() {
defer os.Exit(0)
var outputFilename string
flag.StringVar(&outputFilename, "o", "/dev/stdout", "output filename")
flag.Parse()
subses, duration, itemsCount := make([]*astisub.Subtitles, 0, flag.NArg()), time.Duration(0), 0
for _, a := range flag.Args() {
if a[0]=='+' {
parsedDuration, err := time.ParseDuration(a); fatalCheck(err)
duration += func() time.Duration {
if len(subses)==0 {return 0}
subs := subses[len(subses)-1]
return subs.Items[len(subs.Items)-1].EndAt
} () + parsedDuration
continue
}
subs, err := astisub.OpenFile(a); fatalCheck(err)
if len(subs.Items)==0 {continue}
if duration!=0 {for _, it := range subs.Items {it.StartAt+=duration; it.EndAt+=duration}}
subses=append(subses,subs); itemsCount+=len(subs.Items)
}
items := make([]*astisub.Item, 0, itemsCount)
for _,subs := range subses {items = append(items, subs.Items...)}
if cap(items)!=len(items) {fatalCheck(errors.New("cap(items)!=len(items)"))}
subses[0].Items = items
err := subses[0].Write(outputFilename); fatalCheck(err)
}