-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
52 lines (46 loc) · 1.17 KB
/
sync.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
package main
import (
"errors"
"flag"
"fmt"
"os"
"regexp"
"runtime"
"time"
"github.com/Des-Nerger/go-astisub"
. "github.com/Des-Nerger/sinojapsubs-tools/ensure_go_duration_format"
"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()
//fmt.Printf("%#v\n", flag.Args())
inputFilename := flag.Args()[0]
subs, err := astisub.OpenFile(inputFilename); fatalCheck(err)
ds := []time.Duration{}
for _, arg := range flag.Args()[1:] {
duration, err := time.ParseDuration(EnsureGoDurationFormat(arg)); fatalCheck(err)
ds = append(ds, duration)
}
subs.Sync(ds...)
if outputFilename == "" {
outputFilename = regexp.MustCompile(`^(.*/|)([^/]+)\.[^.]+$`).
ReplaceAllString(inputFilename, "$2.synced.srt")
if inputFilename == outputFilename {
fatalCheck(errors.New("inputFilename == outputFilename"))
}
}
err = subs.Write(outputFilename); fatalCheck(err)
}