-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunite-sametime-srt-items.go
60 lines (54 loc) · 1.34 KB
/
unite-sametime-srt-items.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
package main
import (
"errors"
"flag"
"fmt"
"os"
"regexp"
"runtime"
"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", "", "output filename")
flag.Parse()
inputFilename := flag.Args()[0]
subs, err := astisub.OpenFile(inputFilename); fatalCheck(err)
newItems := make([]*astisub.Item, 0, len(subs.Items))
for _, it := range subs.Items {
if len(newItems)==0 {
newItems=append(newItems, it)
continue
}
lastIt := newItems[len(newItems)-1]
if it.StartAt == lastIt.StartAt && it.EndAt == lastIt.EndAt {
lastIt.Lines = append(lastIt.Lines, it.Lines...)
} else {
if it.StartAt < lastIt.EndAt {
fmt.Fprintln(os.Stderr, "warning: intersection")
}
newItems=append(newItems, it)
}
}
subs.Items = newItems
if outputFilename == "" {
outputFilename = regexp.MustCompile(`^(.*/|)([^/]+)\.[^.]+$`).
ReplaceAllString(inputFilename, "$2.united.srt")
if inputFilename == outputFilename {
fatalCheck(errors.New("inputFilename == outputFilename"))
}
}
err = subs.Write(outputFilename); fatalCheck(err)
}