-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrt-unicode-conveter.go
96 lines (74 loc) · 1.93 KB
/
srt-unicode-conveter.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
package main
import (
"bytes"
"flag"
"fmt"
"github.com/saintfish/chardet"
"golang.org/x/net/html/charset"
"io/ioutil"
"os"
"path/filepath"
)
func main() {
var srtPath = flag.String("p", "nil", "Input srt path to convert")
flag.Parse()
if srtPath == nil || len(*srtPath) == 0 || *srtPath == "nil" {
panic("set path to file with -p")
}
fileBytes, err := ioutil.ReadFile(*srtPath)
if err != nil {
panic("what the fuck is this file?")
}
text := string(fileBytes)
if len(text) == 0 {
panic("file is empty")
}
result, err := detectEncoding(text)
if err != nil {
panic("cannot find unicode of file")
}
fmt.Printf(
"Detected charset is %s, language is %s\n",
result.Charset,
result.Language)
convertResult := convertToUTF8(text, result.Charset)
result, err = detectEncoding(convertResult)
if err != nil {
panic("converted encoding not recognized!")
}
fmt.Printf(
"New Detected charset is %s, language is %s\n",
result.Charset,
result.Language)
srtPathStrong := *srtPath
srtFileName := srtPathStrong[0 : len(srtPathStrong)-len(filepath.Ext(*srtPath))]
newName := fmt.Sprintf("%s-utf-8%s", srtFileName, filepath.Ext(*srtPath))
// open output file
outputFile, err := os.Create(newName)
if err != nil {
panic("cannot create output file!")
}
defer func() {
if err := outputFile.Close(); err != nil {
panic(err)
}
}()
err = ioutil.WriteFile(newName, []byte(convertResult), 0644)
if err != nil {
panic("cannot write to file")
}
fmt.Printf(
"Srt converted successfully : %s \n",
newName)
}
func detectEncoding(str string) (r *chardet.Result, err error) {
detector := chardet.NewTextDetector()
return detector.DetectBest([]byte(str))
}
func convertToUTF8(str string, origEncoding string) string {
strBytes := []byte(str)
byteReader := bytes.NewReader(strBytes)
reader, _ := charset.NewReaderLabel(origEncoding, byteReader)
strBytes, _ = ioutil.ReadAll(reader)
return string(strBytes)
}