-
Notifications
You must be signed in to change notification settings - Fork 15
/
srt.go
128 lines (106 loc) · 2.58 KB
/
srt.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
package subtitles
import (
"fmt"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
// Eol is the end of line characters to use when writing .srt data
var eol = "\n"
var r1 = regexp.MustCompile("([0-9:.,]*) (-->|->) ([0-9:.,]*)")
func init() {
if runtime.GOOS == "windows" {
eol = "\r\n"
}
}
func looksLikeSRT(s string) bool {
s = strings.TrimSpace(s)
return strings.HasPrefix(s, "0\n") || strings.HasPrefix(s, "0\r\n") || strings.HasPrefix(s, "1\n") || strings.HasPrefix(s, "1\r\n") || strings.HasPrefix(s, "2\n") || strings.HasPrefix(s, "2\r\n") || strings.HasPrefix(s, "3\n") || strings.HasPrefix(s, "3\r\n") || strings.HasPrefix(s, "4\n") || strings.HasPrefix(s, "4\r\n")
}
// NewFromSRT parses a .srt text into Subtitle, assumes s is a clean utf8 string
func NewFromSRT(s string) (res Subtitle, err error) {
lines := strings.Split(s, "\n")
outSeq := 1
for i := 0; i < len(lines); i++ {
seq := strings.Trim(lines[i], "\r ")
if seq == "" {
continue
}
if strings.TrimSpace(seq) == "" {
continue
}
_, err = strconv.Atoi(seq)
if err != nil {
err = fmt.Errorf("srt: atoi error at line %d: %v", i, err)
break
}
var o Caption
o.Seq = outSeq
i++
if i >= len(lines) {
break
}
matches := r1.FindStringSubmatch(lines[i])
if len(matches) < 3 {
err = fmt.Errorf("srt: parse error at line %d (idx out of range) for input '%s'", i, lines[i])
break
}
o.Start, err = parseSrtTime(matches[1])
if err != nil {
err = fmt.Errorf("srt: start error at line %d: %v", i, err)
break
}
o.End, err = parseSrtTime(matches[3])
if err != nil {
err = fmt.Errorf("srt: end error at line %d: %v", i, err)
break
}
i++
if i >= len(lines) {
break
}
textLine := 1
for {
line := strings.Trim(lines[i], "\r ")
if line == "" && textLine > 1 {
break
}
if line != "" {
o.Text = append(o.Text, line)
}
i++
if i >= len(lines) {
break
}
textLine++
}
if len(o.Text) > 0 {
res.Captions = append(res.Captions, o)
outSeq++
}
}
return
}
// AsSRT renders the sub in .srt format
func (subtitle *Subtitle) AsSRT() (res string) {
for _, sub := range subtitle.Captions {
res += sub.AsSRT()
}
return
}
// AsSRT renders the caption as srt
func (cap Caption) AsSRT() string {
res := fmt.Sprintf("%d", cap.Seq) + eol +
TimeSRT(cap.Start) + " --> " + TimeSRT(cap.End) + eol
for _, line := range cap.Text {
res += line + eol
}
return res + eol
}
// TimeSRT renders a timestamp for use in .srt
func TimeSRT(t time.Time) string {
res := t.Format("15:04:05.000")
return strings.Replace(res, ".", ",", 1)
}