-
Notifications
You must be signed in to change notification settings - Fork 111
/
webvtt_test.go
234 lines (180 loc) · 5.95 KB
/
webvtt_test.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package astisub_test
import (
"bytes"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/asticode/go-astisub"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWebVTT(t *testing.T) {
// Open
s, err := astisub.OpenFile("./testdata/example-in.vtt")
assert.NoError(t, err)
assertSubtitleItems(t, s)
// Comments
assert.Equal(t, []string{"this a nice example", "of a VTT"}, s.Items[0].Comments)
assert.Equal(t, []string{"This a comment inside the VTT", "and this is the second line"}, s.Items[1].Comments)
// Regions
assert.Equal(t, 2, len(s.Regions))
assert.Equal(t, astisub.Region{ID: "fred", InlineStyle: &astisub.StyleAttributes{WebVTTLines: 3, WebVTTRegionAnchor: "0%,100%", WebVTTScroll: "up", WebVTTViewportAnchor: "10%,90%", WebVTTWidth: "40%"}}, *s.Regions["fred"])
assert.Equal(t, astisub.Region{ID: "bill", InlineStyle: &astisub.StyleAttributes{WebVTTLines: 3, WebVTTRegionAnchor: "100%,100%", WebVTTScroll: "up", WebVTTViewportAnchor: "90%,90%", WebVTTWidth: "40%"}}, *s.Regions["bill"])
assert.Equal(t, s.Regions["bill"], s.Items[0].Region)
assert.Equal(t, s.Regions["fred"], s.Items[1].Region)
// Styles
assert.Equal(t, astisub.StyleAttributes{WebVTTAlign: "left", WebVTTPosition: "10%,start", WebVTTSize: "35%"}, *s.Items[1].InlineStyle)
// No subtitles to write
w := &bytes.Buffer{}
err = astisub.Subtitles{}.WriteToWebVTT(w)
assert.EqualError(t, err, astisub.ErrNoSubtitlesToWrite.Error())
// Write
c, err := ioutil.ReadFile("./testdata/example-out.vtt")
assert.NoError(t, err)
err = s.WriteToWebVTT(w)
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}
func TestBroken1WebVTT(t *testing.T) {
// Open bad, broken WebVTT file
_, err := astisub.OpenFile("./testdata/broken-1-in.vtt")
assert.Nil(t, err)
}
func TestNonUTF8WebVTT(t *testing.T) {
_, err := astisub.OpenFile("./testdata/example-in-non-utf8.vtt")
assert.Error(t, err)
}
func TestWebVTTWithVoiceName(t *testing.T) {
testData := `WEBVTT
NOTE this a example with voicename
1
00:02:34.000 --> 00:02:35.000
<v.first.local Roger Bingham> I'm the fist speaker
2
00:02:34.000 --> 00:02:35.000
<v Bingham> I'm the second speaker
3
00:00:04.000 --> 00:00:08.000
<v Lee>What are you doing here?</v>
4
00:00:04.000 --> 00:00:08.000
<v Bob>Incorrect tag?</vi>`
s, err := astisub.ReadFromWebVTT(strings.NewReader(testData))
assert.NoError(t, err)
assert.Len(t, s.Items, 4)
assert.Equal(t, "Roger Bingham", s.Items[0].Lines[0].VoiceName)
assert.Equal(t, "Bingham", s.Items[1].Lines[0].VoiceName)
assert.Equal(t, "Lee", s.Items[2].Lines[0].VoiceName)
assert.Equal(t, "Bob", s.Items[3].Lines[0].VoiceName)
b := &bytes.Buffer{}
err = s.WriteToWebVTT(b)
assert.NoError(t, err)
assert.Equal(t, `WEBVTT
NOTE this a example with voicename
1
00:02:34.000 --> 00:02:35.000
<v Roger Bingham>I'm the fist speaker
2
00:02:34.000 --> 00:02:35.000
<v Bingham>I'm the second speaker
3
00:00:04.000 --> 00:00:08.000
<v Lee>What are you doing here?
4
00:00:04.000 --> 00:00:08.000
<v Bob>Incorrect tag?
`, b.String())
}
func TestWebVTTWithTimestampMap(t *testing.T) {
testData := `WEBVTT
X-TIMESTAMP-MAP=MPEGTS:180000, LOCAL:00:00:00.000
00:00.933 --> 00:02.366
♪ ♪
00:02.400 --> 00:03.633
Evening.`
s, err := astisub.ReadFromWebVTT(strings.NewReader(testData))
assert.NoError(t, err)
assert.Len(t, s.Items, 2)
assert.Equal(t, s.Items[0].StartAt.Milliseconds(), int64(933))
assert.Equal(t, s.Items[0].EndAt.Milliseconds(), int64(2366))
assert.Equal(t, s.Items[1].StartAt.Milliseconds(), int64(2400))
assert.Equal(t, s.Items[1].EndAt.Milliseconds(), int64(3633))
assert.Equal(t, s.Metadata.WebVTTTimestampMap.Offset(), time.Duration(time.Second*2))
b := &bytes.Buffer{}
err = s.WriteToWebVTT(b)
assert.NoError(t, err)
assert.Equal(t, `WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:180000
1
00:00:00.933 --> 00:00:02.366
♪ ♪
2
00:00:02.400 --> 00:00:03.633
Evening.
`, b.String())
}
func TestWebVTTEscape(t *testing.T) {
testData := `WEBVTT
00:01:00.000 --> 00:02:00.000
Sentence with an & in the middle
00:02:00.000 --> 00:03:00.000
Sentence with an < in the middle`
s, err := astisub.ReadFromWebVTT(strings.NewReader(testData))
require.NoError(t, err)
require.Len(t, s.Items, 2)
require.Equal(t, "Sentence with an & in the middle", s.Items[0].String())
require.Equal(t, "Sentence with an < in the middle", s.Items[1].String())
b := &bytes.Buffer{}
err = s.WriteToWebVTT(b)
require.NoError(t, err)
require.Equal(t, `WEBVTT
1
00:01:00.000 --> 00:02:00.000
Sentence with an & in the middle
2
00:02:00.000 --> 00:03:00.000
Sentence with an < in the middle
`, b.String())
}
func TestWebVTTTags(t *testing.T) {
testData := `WEBVTT
00:01:00.000 --> 00:02:00.000
<u><i>Italic with underline text</i></u> some extra
00:02:00.000 --> 00:03:00.000
<lang en>English here</lang> <c.yellow.bg_blue>Yellow text on blue background</c>
00:03:00.000 --> 00:04:00.000
<v Joe><c.red><i>Joe's words are red in italic</i></c>
00:04:00.000 --> 00:05:00.000
<customed_tag.class1.class2>Text here</customed_tag>
00:05:00.000 --> 00:06:00.000
<v Joe>Joe says something</v> <v Bob>Bob says something</v>
00:06:00.000 --> 00:07:00.000
Text with a <00:06:30.000>timestamp in the middle`
s, err := astisub.ReadFromWebVTT(strings.NewReader(testData))
require.NoError(t, err)
require.Len(t, s.Items, 6)
b := &bytes.Buffer{}
err = s.WriteToWebVTT(b)
require.NoError(t, err)
require.Equal(t, `WEBVTT
1
00:01:00.000 --> 00:02:00.000
<u><i>Italic with underline text</i></u> some extra
2
00:02:00.000 --> 00:03:00.000
<lang en>English here</lang> <c.yellow.bg_blue>Yellow text on blue background</c>
3
00:03:00.000 --> 00:04:00.000
<v Joe><c.red><i>Joe's words are red in italic</i></c>
4
00:04:00.000 --> 00:05:00.000
<customed_tag.class1.class2>Text here</customed_tag>
5
00:05:00.000 --> 00:06:00.000
<v Joe>Joe says something Bob says something
6
00:06:00.000 --> 00:07:00.000
Text with a <00:06:30.000>timestamp in the middle
`, b.String())
}