Skip to content

Commit 01b0599

Browse files
committed
export ParseTime
1 parent a4231fe commit 01b0599

File tree

2 files changed

+27
-48
lines changed

2 files changed

+27
-48
lines changed

srt.go

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@ func looksLikeSrt(s string) bool {
2020
}
2121

2222
// ParseSrt parses a .srt text into []Caption, assumes s is a clean utf8 string
23-
func parseSrt(s string) []caption {
24-
25-
var res []caption
26-
23+
func parseSrt(s string) (res []caption) {
2724
r1 := regexp.MustCompile("([0-9:.,]*) --> ([0-9:.,]*)")
28-
2925
lines := strings.Split(s, "\n")
30-
3126
outSeq := 1
3227

3328
for i := 0; i < len(lines); i++ {
34-
3529
seq := strings.Trim(lines[i], "\r ")
3630
if seq == "" {
3731
continue
@@ -57,13 +51,13 @@ func parseSrt(s string) []caption {
5751
continue
5852
}
5953

60-
o.Start, err = parseTime(matches[1])
54+
o.Start, err = ParseTime(matches[1])
6155
if err != nil {
6256
fmt.Printf("[srt] Parse error 3 at line %d: %v\n", i, err)
6357
continue
6458
}
6559

66-
o.End, err = parseTime(matches[2])
60+
o.End, err = ParseTime(matches[2])
6761
if err != nil {
6862
fmt.Printf("[srt] Parse error 4 at line %d: %v\n", i, err)
6963
continue
@@ -97,42 +91,36 @@ func parseSrt(s string) []caption {
9791
outSeq++
9892
}
9993
}
100-
101-
return res
94+
return
10295
}
10396

104-
func parseTime(in string) (time.Time, error) {
97+
// ParseTime parses a subtitle time (duration since start of film)
98+
func ParseTime(in string) (time.Time, error) {
99+
// . and , to :
100+
in = strings.Replace(in, ",", ":", -1)
101+
in = strings.Replace(in, ".", ":", -1)
105102

106-
// . to ,
107-
in = strings.Replace(in, ",", ".", 1)
108-
109-
if !strings.ContainsAny(in, ".") {
110-
in += ".000"
103+
if strings.Count(in, ":") == 2 {
104+
in += ":000"
111105
}
112106

113-
r1 := regexp.MustCompile("([0-9]+):([0-9]+):([0-9]+)[.]([0-9]+)")
114-
107+
r1 := regexp.MustCompile("([0-9]+):([0-9]+):([0-9]+):([0-9]+)")
115108
matches := r1.FindStringSubmatch(in)
116-
117109
if len(matches) < 5 {
118110
return time.Now(), fmt.Errorf("[srt] Regexp didnt match: %s", in)
119111
}
120-
121112
h, err := strconv.Atoi(matches[1])
122113
if err != nil {
123114
return time.Now(), err
124115
}
125-
126116
m, err := strconv.Atoi(matches[2])
127117
if err != nil {
128118
return time.Now(), err
129119
}
130-
131120
s, err := strconv.Atoi(matches[3])
132121
if err != nil {
133122
return time.Now(), err
134123
}
135-
136124
ms, err := strconv.Atoi(matches[4])
137125
if err != nil {
138126
return time.Now(), err
@@ -143,30 +131,19 @@ func parseTime(in string) (time.Time, error) {
143131

144132
// writeSrt prints a srt render to outFileName
145133
func writeSrt(subs []caption, outFileName string) error {
146-
147134
text := renderSrt(subs)
148-
149-
err := ioutil.WriteFile(outFileName, []byte(text), 0644)
150-
if err != nil {
151-
return err
152-
}
153-
return nil
135+
return ioutil.WriteFile(outFileName, []byte(text), 0644)
154136
}
155137

156138
// renderSrt produces a text representation of the subtitles
157-
func renderSrt(subs []caption) string {
158-
159-
res := ""
160-
139+
func renderSrt(subs []caption) (res string) {
161140
for _, sub := range subs {
162141
res += renderCaptionAsSrt(sub)
163142
}
164-
165-
return res
143+
return
166144
}
167145

168146
func renderCaptionAsSrt(cap caption) string {
169-
170147
res := fmt.Sprintf("%d", cap.Seq) + eol +
171148
cap.srtTime() + eol
172149

srt_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ import (
88

99
func TestParseTime(t *testing.T) {
1010

11-
t1, _ := parseTime("18:40:22.110")
12-
t2, _ := parseTime("18:40:22,110")
13-
t3, _ := parseTime("18:40:22")
14-
t4, _ := parseTime("00:00:0,500")
15-
t5, _ := parseTime("00:00:2,00")
16-
t6, _ := parseTime("00:14:52.12")
11+
t1, _ := ParseTime("18:40:22.110")
12+
t2, _ := ParseTime("18:40:22,110")
13+
t3, _ := ParseTime("18:40:22:110")
14+
t4, _ := ParseTime("18:40:22")
15+
t5, _ := ParseTime("00:00:0,500")
16+
t6, _ := ParseTime("00:00:2,00")
17+
t7, _ := ParseTime("00:14:52.12")
1718

1819
assert.Equal(t, makeTime(18, 40, 22, 110), t1)
1920
assert.Equal(t, makeTime(18, 40, 22, 110), t2)
20-
assert.Equal(t, makeTime(18, 40, 22, 0), t3)
21-
assert.Equal(t, makeTime(0, 0, 0, 500), t4)
22-
assert.Equal(t, makeTime(0, 0, 2, 0), t5)
23-
assert.Equal(t, makeTime(0, 14, 52, 12), t6)
21+
assert.Equal(t, makeTime(18, 40, 22, 110), t3)
22+
assert.Equal(t, makeTime(18, 40, 22, 0), t4)
23+
assert.Equal(t, makeTime(0, 0, 0, 500), t5)
24+
assert.Equal(t, makeTime(0, 0, 2, 0), t6)
25+
assert.Equal(t, makeTime(0, 14, 52, 12), t7)
2426
}
2527

2628
func TestParseSrt(t *testing.T) {

0 commit comments

Comments
 (0)