forked from martinlindhe/subtitles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
55 lines (48 loc) · 1.24 KB
/
parser.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
package subtitles
import (
"fmt"
"io/ioutil"
"log"
"gonum.org/v1/gonum/stat"
)
// Parse tries to parse a subtitle
func Parse(s string) (sub Subtitle, err error) {
var maxVariance float64
key := -1
var res [4]Subtitle
res[0], _ = NewFromMicroDVD(s, -1)
res[1], _ = NewFromSRT(s)
res[2], _ = NewFromSSA(s)
res[3], _ = NewFromTMPlayerTXT(s)
// TODO: make the following convertion safe (will not panic
// from any input), and then bring them back
// res[4], _ = NewFromCCDBCapture(s)
// res[5], _ = NewFromDCSub(s)
// return the most varianced result
for k, v := range res {
vc := stat.Variance(toDataSet(v.AsPlainTXT()), nil)
if vc > maxVariance {
maxVariance = vc
key = k
}
}
if key >= 0 {
return res[key], nil
}
return Subtitle{}, fmt.Errorf("parse: unrecognized subtitle type")
}
func toDataSet(s string) (res []float64) {
for _, c := range s {
res = append(res, float64(c))
}
return
}
// LooksLikeTextSubtitle returns true i byte stream seems to be of a recognized format
func LooksLikeTextSubtitle(filename string) bool {
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
s := ConvertToUTF8(data)
return looksLikeCCDBCapture(s) || looksLikeSSA(s) || looksLikeDCSub(s) || looksLikeSRT(s)
}