-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathstl_test.go
More file actions
312 lines (275 loc) · 9.28 KB
/
stl_test.go
File metadata and controls
312 lines (275 loc) · 9.28 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package astisub_test
import (
"bytes"
"io/ioutil"
"os"
"testing"
"time"
"github.com/asticode/go-astikit"
"github.com/asticode/go-astisub"
"github.com/stretchr/testify/assert"
)
func TestSTL(t *testing.T) {
// Init
creationDate, _ := time.Parse("060102", "170702")
revisionDate, _ := time.Parse("060102", "010101")
// Open
s, err := astisub.OpenFile("./testdata/example-in.stl")
assert.NoError(t, err)
assertSubtitleItems(t, s)
// Metadata
assert.Equal(t, &astisub.Metadata{
Framerate: 25,
Language: astisub.LanguageFrench,
STLCreationDate: &creationDate,
STLMaximumNumberOfDisplayableCharactersInAnyTextRow: astikit.IntPtr(40),
STLMaximumNumberOfDisplayableRows: astikit.IntPtr(23),
STLPublisher: "Copyright test",
STLDisplayStandardCode: "1",
STLRevisionDate: &revisionDate,
STLSubtitleListReferenceCode: "12345678",
STLCountryOfOrigin: "FRA",
Title: "Title test"},
s.Metadata)
// No subtitles to write
w := &bytes.Buffer{}
err = astisub.Subtitles{}.WriteToSTL(w)
assert.EqualError(t, err, astisub.ErrNoSubtitlesToWrite.Error())
// Write
c, err := ioutil.ReadFile("./testdata/example-out.stl")
assert.NoError(t, err)
err = s.WriteToSTL(w)
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}
func TestOPNSTL(t *testing.T) {
// Init
creationDate, _ := time.Parse("060102", "200110")
revisionDate, _ := time.Parse("060102", "200110")
// Open
s, err := astisub.OpenFile("./testdata/example-opn-in.stl")
assert.NoError(t, err)
// Metadata
assert.Equal(t, &astisub.Metadata{
Framerate: 25,
Language: astisub.LanguageEnglish,
STLCountryOfOrigin: "NOR",
STLCreationDate: &creationDate,
STLDisplayStandardCode: "0",
STLMaximumNumberOfDisplayableCharactersInAnyTextRow: astikit.IntPtr(38),
STLMaximumNumberOfDisplayableRows: astikit.IntPtr(11),
STLPublisher: "",
STLRevisionDate: &revisionDate,
STLRevisionNumber: 1,
Title: ""},
s.Metadata)
// No subtitles to write
w := &bytes.Buffer{}
err = astisub.Subtitles{}.WriteToSTL(w)
assert.EqualError(t, err, astisub.ErrNoSubtitlesToWrite.Error())
// Write
c, err := ioutil.ReadFile("./testdata/example-opn-out.stl")
assert.NoError(t, err)
err = s.WriteToSTL(w)
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}
func TestIgnoreTimecodeStartOfProgramme(t *testing.T) {
opts := astisub.STLOptions{IgnoreTimecodeStartOfProgramme: true}
r, err := os.Open("./testdata/example-in-nonzero-offset.stl")
assert.NoError(t, err)
defer r.Close()
s, err := astisub.ReadFromSTL(r, opts)
assert.NoError(t, err)
firstStart := 99 * time.Second
assert.Equal(t, firstStart, s.Items[0].StartAt, "first start at 0")
}
func TestTTMLToSTLGSIBlock(t *testing.T) {
// Test that TTML to STL conversion includes correct disk format code and display standard code
// This verifies the fix for the bug where "STL25.01" was missing from the GSI block
// when converting from TTML files that have no framerate metadata
// Open TTML file
s, err := astisub.OpenFile("./testdata/example-in.ttml")
assert.NoError(t, err)
assert.NotNil(t, s)
// TTML files typically don't have STL-specific metadata
assert.Empty(t, s.Metadata.STLDisplayStandardCode, "TTML should not have STL-specific display standard code")
// Write to STL
w := &bytes.Buffer{}
err = s.WriteToSTL(w)
assert.NoError(t, err)
stlData := w.Bytes()
assert.True(t, len(stlData) >= 1024, "STL file should have at least GSI block (1024 bytes)")
// Check GSI block header
// Bytes 0-2: Code page number (should be "850")
codePageNumber := string(stlData[0:3])
assert.Equal(t, "850", codePageNumber, "Code page number should be '850'")
// Bytes 3-10: Disk format code (should be "STL25.01" for 25fps, not spaces)
diskFormatCode := string(stlData[3:11])
assert.NotEqual(t, " ", diskFormatCode,
"Disk format code should not be empty spaces (this was the bug)")
assert.True(t, diskFormatCode == "STL25.01" || diskFormatCode == "STL30.01",
"Disk format code should be 'STL25.01' or 'STL30.01', got '%s'", diskFormatCode)
// Byte 11: Display standard code (should be "1" for Level 1 Teletext by default)
displayStandardCode := string(stlData[11:12])
assert.True(t, displayStandardCode == "0" || displayStandardCode == "1" || displayStandardCode == "2",
"Display standard code should be '0', '1', or '2', got '%s'", displayStandardCode)
}
func TestSTLColors(t *testing.T) {
// Create subtitles with different colored text
s := astisub.NewSubtitles()
s.Metadata = &astisub.Metadata{
Framerate: 25,
STLDisplayStandardCode: "0", // Open subtitling
}
// Add items with different colors
s.Items = []*astisub.Item{
{
StartAt: time.Second,
EndAt: 2 * time.Second,
Lines: []astisub.Line{
{
Items: []astisub.LineItem{
{
Text: "Red text",
InlineStyle: &astisub.StyleAttributes{
STLColor: astisub.ColorRed,
},
},
},
},
},
},
{
StartAt: 3 * time.Second,
EndAt: 4 * time.Second,
Lines: []astisub.Line{
{
Items: []astisub.LineItem{
{
Text: "Green text",
InlineStyle: &astisub.StyleAttributes{
STLColor: astisub.ColorGreen,
},
},
},
},
},
},
{
StartAt: 5 * time.Second,
EndAt: 6 * time.Second,
Lines: []astisub.Line{
{
Items: []astisub.LineItem{
{
Text: "Blue text",
InlineStyle: &astisub.StyleAttributes{
STLColor: astisub.ColorBlue,
},
},
},
},
},
},
{
StartAt: 7 * time.Second,
EndAt: 8 * time.Second,
Lines: []astisub.Line{
{
Items: []astisub.LineItem{
{
Text: "Yellow text",
InlineStyle: &astisub.StyleAttributes{
STLColor: astisub.ColorYellow,
},
},
},
},
},
},
}
// Write to STL
w := &bytes.Buffer{}
err := s.WriteToSTL(w)
assert.NoError(t, err)
// Read back from STL
s2, err := astisub.ReadFromSTL(bytes.NewReader(w.Bytes()), astisub.STLOptions{})
assert.NoError(t, err)
// Verify colors are preserved
assert.Equal(t, 4, len(s2.Items))
assert.Equal(t, astisub.ColorRed, s2.Items[0].Lines[0].Items[0].InlineStyle.STLColor)
assert.Equal(t, astisub.ColorGreen, s2.Items[1].Lines[0].Items[0].InlineStyle.STLColor)
assert.Equal(t, astisub.ColorBlue, s2.Items[2].Lines[0].Items[0].InlineStyle.STLColor)
assert.Equal(t, astisub.ColorYellow, s2.Items[3].Lines[0].Items[0].InlineStyle.STLColor)
}
func TestSTLColorsFromWebVTT(t *testing.T) {
// Open WebVTT file with colors
s, err := astisub.OpenFile("./testdata/example-out-styled.vtt")
assert.NoError(t, err)
assert.NotNil(t, s)
// Set metadata for STL
s.Metadata = &astisub.Metadata{
Framerate: 25,
STLDisplayStandardCode: "0", // Open subtitling
}
// Write to STL
w := &bytes.Buffer{}
err = s.WriteToSTL(w)
assert.NoError(t, err)
// Read back from STL
s2, err := astisub.ReadFromSTL(bytes.NewReader(w.Bytes()), astisub.STLOptions{})
assert.NoError(t, err)
// Verify colors are preserved through WebVTT -> STL -> STL round trip
// Item 1: lime color (maps to STLColor in WebVTT processing)
assert.Equal(t, 1, len(s2.Items[0].Lines))
if len(s2.Items[0].Lines[0].Items) > 0 {
assert.NotNil(t, s2.Items[0].Lines[0].Items[0].InlineStyle)
assert.NotNil(t, s2.Items[0].Lines[0].Items[0].InlineStyle.STLColor, "Item 1 should have STL color")
}
// Item 2: magenta color
assert.Equal(t, 1, len(s2.Items[1].Lines))
if len(s2.Items[1].Lines[0].Items) > 0 {
assert.NotNil(t, s2.Items[1].Lines[0].Items[0].InlineStyle)
assert.Equal(t, astisub.ColorMagenta, s2.Items[1].Lines[0].Items[0].InlineStyle.STLColor, "Item 2 should have magenta color")
}
}
func TestSTLColorsFromTTML(t *testing.T) {
// Open TTML file with colors
s, err := astisub.OpenFile("./testdata/example-in.ttml")
assert.NoError(t, err)
assert.NotNil(t, s)
// Set metadata for STL
s.Metadata = &astisub.Metadata{
Framerate: 25,
STLDisplayStandardCode: "0", // Open subtitling
}
// Write to STL
w := &bytes.Buffer{}
err = s.WriteToSTL(w)
assert.NoError(t, err)
// Read back from STL
s2, err := astisub.ReadFromSTL(bytes.NewReader(w.Bytes()), astisub.STLOptions{})
assert.NoError(t, err)
// Verify colors are preserved through TTML -> STL -> STL round trip
// Item 1: has black color in span
assert.Equal(t, 1, len(s2.Items[0].Lines))
if len(s2.Items[0].Lines[0].Items) > 0 {
assert.NotNil(t, s2.Items[0].Lines[0].Items[0].InlineStyle)
assert.Equal(t, astisub.ColorBlack, s2.Items[0].Lines[0].Items[0].InlineStyle.STLColor, "Item 1 should have black color")
}
// Item 2: has green color in one of the spans (across multiple lines due to <br/>)
foundGreen := false
for _, line := range s2.Items[1].Lines {
for _, item := range line.Items {
if item.InlineStyle != nil && item.InlineStyle.STLColor == astisub.ColorGreen {
foundGreen = true
break
}
}
if foundGreen {
break
}
}
assert.True(t, foundGreen, "Item 2 should have a span with green color")
}