-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_subs_items.go
45 lines (39 loc) · 1000 Bytes
/
sort_subs_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
package main
import (
"errors"
"flag"
"fmt"
"os"
"regexp"
"runtime"
"sort"
"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.Arg(0)
subs, err := astisub.OpenFile(inputFilename); fatalCheck(err)
sort.SliceStable(subs.Items, func(i,j int) bool {return subs.Items[i].StartAt < subs.Items[j].StartAt})
if outputFilename == "" {
outputFilename = regexp.MustCompile(`^(.*/|)([^/]+)\.[^.]+$`).
ReplaceAllString(inputFilename, "$2.sorted.srt")
if inputFilename == outputFilename {
fatalCheck(errors.New("inputFilename == outputFilename"))
}
}
err = subs.Write(outputFilename); fatalCheck(err)
}