forked from asticode/go-astisub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (97 loc) · 2.72 KB
/
main.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
package main
import (
"flag"
"log"
"github.com/asticode/go-astikit"
"github.com/asticode/go-astisub"
)
// Flags
var (
fragmentDuration = flag.Duration("f", 0, "the fragment duration")
inputPath = astikit.NewFlagStrings()
teletextPage = flag.Int("p", 0, "the teletext page")
outputPath = flag.String("o", "", "the output path")
syncDuration = flag.Duration("s", 0, "the sync duration")
)
func main() {
// Init
cmd := astikit.FlagCmd()
flag.Var(&inputPath, "i", "the input paths")
flag.Parse()
// Validate input path
if len(*inputPath.Slice) == 0 {
log.Fatal("Use -i to provide at least one input path")
}
// Validate output path
if len(*outputPath) <= 0 {
log.Fatal("Use -o to provide an output path")
}
// Open first input path
var sub *astisub.Subtitles
var err error
if sub, err = astisub.Open(astisub.Options{Filename: (*inputPath.Slice)[0], Teletext: astisub.TeletextOptions{Page: *teletextPage}}); err != nil {
log.Fatalf("%s while opening %s", err, (*inputPath.Slice)[0])
}
// Switch on subcommand
switch cmd {
case "convert":
// Write
if err = sub.Write(*outputPath); err != nil {
log.Fatalf("%s while writing to %s", err, *outputPath)
}
case "fragment":
// Validate fragment duration
if *fragmentDuration <= 0 {
log.Fatal("Use -f to provide a fragment duration")
}
// Fragment
sub.Fragment(*fragmentDuration)
// Write
if err = sub.Write(*outputPath); err != nil {
log.Fatalf("%s while writing to %s", err, *outputPath)
}
case "merge":
// Validate second input path
if len(*inputPath.Slice) == 1 {
log.Fatal("Use -i to provide at least two input paths")
}
// Open second input path
var sub2 *astisub.Subtitles
if sub2, err = astisub.Open(astisub.Options{Filename: (*inputPath.Slice)[1], Teletext: astisub.TeletextOptions{Page: *teletextPage}}); err != nil {
log.Fatalf("%s while opening %s", err, (*inputPath.Slice)[1])
}
// Merge
sub.Merge(sub2)
// Write
if err = sub.Write(*outputPath); err != nil {
log.Fatalf("%s while writing to %s", err, *outputPath)
}
case "optimize":
// Optimize
sub.Optimize()
// Write
if err = sub.Write(*outputPath); err != nil {
log.Fatalf("%s while writing to %s", err, *outputPath)
}
case "sync":
// Validate sync duration
if *syncDuration == 0 {
log.Fatal("Use -s to provide a sync duration")
}
// Fragment
sub.Add(*syncDuration)
// Write
if err = sub.Write(*outputPath); err != nil {
log.Fatalf("%s while writing to %s", err, *outputPath)
}
case "unfragment":
// Unfragment
sub.Unfragment()
// Write
if err = sub.Write(*outputPath); err != nil {
log.Fatalf("%s while writing to %s", err, *outputPath)
}
default:
log.Fatalf("Invalid subcommand %s", cmd)
}
}