-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
100 lines (77 loc) · 1.56 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
package main
import (
"encoding/json"
"errors"
"flag"
"io/ioutil"
"log"
"os"
"strings"
)
type Scene struct {
ID string
Start string
End string
Sentences string
}
func getFileContent(filepath string) string {
dat, err := ioutil.ReadFile(filepath)
check(err)
return string(dat)
}
func check(e error) {
if e != nil {
panic(e)
}
}
func generateSceneItem(scene string) (*Scene, error) {
sp := strings.Split
sceneItems := sp(scene, "\n")
if len(sceneItems) >= 2 {
ID, Time, Sentences :=
sceneItems[0], sp(sceneItems[1], " --> "), sceneItems[2]
Start := Time[0]
End := Time[1]
SceneItem := Scene{
ID: ID,
Start: Start,
End: End,
Sentences: Sentences,
}
return &SceneItem, nil
}
return nil, errors.New("Range Error")
}
func GetScenes(path string) []Scene {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
filepath := dir + "/" + path
subtitles := getFileContent(filepath)
var scenes []Scene
sceneChunk := strings.Split(subtitles, "\n\n")
for _, scene := range sceneChunk {
SceneItem, err := generateSceneItem(scene)
if err != nil {
// fmt.Println(err)
} else {
scenes = append(scenes, *SceneItem)
}
}
return scenes
}
func Srt2Json() {
flag.Parse()
filename := flag.Arg(0)
exportFileName := flag.Arg(1)
subt := GetScenes(filename)
file, _ := json.MarshalIndent(subt, " ", " ")
if exportFileName == "" {
exportFileName = strings.Replace(filename, ".srt", "", -1)
}
_ = ioutil.WriteFile(exportFileName+".json", file, 0644)
}
func main() {
Srt2Json()
}