-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
134 lines (104 loc) · 2.59 KB
/
converter.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/dwbuiten/go-mediainfo/mediainfo"
)
var mpl2Regex *regexp.Regexp = regexp.MustCompile(`^\[(\d+)\]\[(\d+)\](.+)`)
func convert(subtitles string, path string) (string, error) {
var converted string
format := format(subtitles)
fmt.Printf("format: %s\n", format)
switch format {
case "microDVD":
converted = microDVD(subtitles, path)
case "mpl2":
converted = mpl2(subtitles)
}
subtitlesPath, err := save(converted, path)
if err != nil {
return "", err
}
fmt.Printf("subtitles saved to %s\n", subtitlesPath)
return converted, nil
}
func save(converted string, path string) (string, error) {
dir := filepath.Dir(path)
filename := filepath.Base(path)
subtitlesName := strings.TrimSuffix(filename, filepath.Ext(filename)) + ".srt"
subtitlesPath := filepath.Join(dir, subtitlesName)
if err := ioutil.WriteFile(subtitlesPath, []byte(converted), 0644); err != nil {
return "", err
}
return subtitlesPath, nil
}
func format(subtitles string) string {
var format string
if mpl2Regex.MatchString(subtitles) {
format = "mpl2"
}
return format
}
func mpl2(subtitles string) string {
var result string
getTime := func(dsecStr string) (string, error) {
dsec, err := strconv.ParseUint(dsecStr, 10, 64)
if err != nil {
return "", err
}
t := time.Time{}
displayTime := t.Add(time.Duration(dsec*100) * time.Millisecond).Format("15:04:05.000")
displayTime = strings.Replace(displayTime, ".", ",", 1)
return displayTime, nil
}
lines := strings.Split(subtitles, "\n")
for i, line := range lines {
matches := mpl2Regex.FindStringSubmatch(line)
if len(matches) < 4 {
continue
}
startTime, err := getTime(matches[1])
if err != nil {
continue
}
stopTime, err := getTime(matches[2])
if err != nil {
continue
}
text := strings.Replace(matches[3], "\r", "", -1)
text = strings.Replace(text, "|", "\n", -1)
result += fmt.Sprintf("%d \n%s --> %s \n%s \n\n", i+1, startTime, stopTime, text)
}
return result
}
func microDVD(subtitles string, path string) string {
fps, err := getFPS(path)
if err != nil {
fmt.Println("FPS rate: 23.976 (default)")
fps = 23.976
}
fmt.Printf("FPS rate: %.3f\n", fps)
return ""
}
func getFPS(path string) (float64, error) {
mediainfo.Init()
info, err := mediainfo.Open(path)
if err != nil {
return 0, err
}
defer info.Close()
val, err := info.Get("FrameRate", 0, mediainfo.Video)
if err != nil {
return 0, err
}
fps, err := strconv.ParseFloat(val, 64)
if err != nil {
return 0, err
}
return fps, nil
}