-
Notifications
You must be signed in to change notification settings - Fork 45
/
ffprobe_test.go
240 lines (198 loc) · 5.77 KB
/
ffprobe_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
235
236
237
238
239
240
package ffprobe
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"
)
const (
testPath = "assets/test.mp4"
testPathError = "assets/test.avi"
)
func Test_ProbeURL(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFn()
data, err := ProbeURL(ctx, testPath)
if err != nil {
t.Errorf("Error getting data: %v", err)
}
validateData(t, data)
}
func Test_ProbeURL_Error(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFn()
_, err := ProbeURL(ctx, testPathError, "-loglevel", "error")
if err == nil {
t.Errorf("No error reading bad asset")
}
if strings.Contains(err.Error(), "[]") {
t.Errorf("No stderr included in error message")
}
}
func Test_ProbeURL_HTTP(t *testing.T) {
const testPort = 20811
ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFn()
// Serve all files in assets
go func() {
http.Handle("/", http.FileServer(http.Dir("./assets")))
err := http.ListenAndServe(fmt.Sprintf(":%d", testPort), nil) //nolint:gosec
t.Log(err)
}()
// Make sure HTTP is up
time.Sleep(time.Second)
data, err := ProbeURL(ctx, fmt.Sprintf("http://127.0.0.1:%d/test.mp4", testPort))
if err != nil {
t.Errorf("Error getting data: %v", err)
}
validateData(t, data)
_, err = ProbeURL(ctx, fmt.Sprintf("http://127.0.0.1:%d/test.avi", testPort), "-loglevel", "error")
if err == nil {
t.Errorf("No error reading bad asset")
}
if strings.Contains(err.Error(), "[]") {
t.Errorf("No stderr included in error message")
}
}
func Test_ProbeReader(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFn()
fileReader, err := os.Open(testPath)
if err != nil {
t.Errorf("Error opening test file: %v", err)
}
data, err := ProbeReader(ctx, fileReader)
if err != nil {
t.Errorf("Error getting data: %v", err)
}
validateData(t, data)
}
func Test_ProbeReader_Error(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFn()
fileReader, err := os.Open(testPathError)
if err != nil {
t.Errorf("Error opening test file: %v", err)
}
_, err = ProbeReader(ctx, fileReader, "-loglevel", "error")
if err == nil {
t.Errorf("No error reading bad asset")
}
if strings.Contains(err.Error(), "[]") {
t.Errorf("No stderr included in error message")
}
}
func validateData(t *testing.T, data *ProbeData) {
validateStreams(t, data)
// Check some Tags
const testMajorBrand = "isom"
if data.Format.Tags.MajorBrand != testMajorBrand {
t.Errorf("MajorBrand format tag is not %s", testMajorBrand)
}
if val, err := data.Format.TagList.GetString("major_brand"); err != nil {
t.Errorf("retrieving major_brand tag errors: %v", err)
} else if val != testMajorBrand {
t.Errorf("MajorBrand format tag is not %s", testMajorBrand)
}
// test Format.Duration
duration := data.Format.Duration()
if duration.Seconds() != 5.312 {
t.Errorf("this video is 5.312s.")
}
// test Format.StartTime
startTime := data.Format.StartTime()
if startTime != time.Duration(0) {
t.Errorf("this video starts at 0s.")
}
validateChapters(t, data)
}
func validateStreams(t *testing.T, data *ProbeData) {
// test ProbeData.GetStream
stream := data.StreamType(StreamVideo)
if len(stream) != 1 {
t.Errorf("It just has one video stream.")
}
// Check some Tags
const testLanguage = "und"
if stream[0].Tags.Rotate != 0 {
t.Errorf("Video stream rotate tag is not 0")
}
if stream[0].Tags.Language != testLanguage {
t.Errorf("Video stream language tag is not %s", testLanguage)
}
if val, err := stream[0].TagList.GetString("language"); err != nil {
t.Errorf("retrieving language tag errors: %v", err)
} else if val != testLanguage {
t.Errorf("Video stream language tag is not %s", testLanguage)
}
stream = data.StreamType(StreamAudio)
if len(stream) != 1 {
t.Errorf("It just has one audio stream.")
}
// this stream is []
stream = data.StreamType(StreamSubtitle)
if len(stream) != 0 {
t.Errorf("It does not have a subtitle stream.")
}
stream = data.StreamType(StreamData)
// We expect at least one data stream, since there are chapters
if len(stream) == 0 {
t.Errorf("It does not have a data stream.")
}
stream = data.StreamType(StreamAttachment)
if len(stream) != 0 {
t.Errorf("It does not have an attachment stream.")
}
stream = data.StreamType(StreamAny)
if len(stream) != 3 {
t.Errorf("It should have three streams.")
}
}
func validateChapters(t *testing.T, data *ProbeData) {
chapters := data.Chapters
if chapters == nil {
t.Error("Chapters List was nil")
return
}
if len(chapters) != 3 {
t.Errorf("Expected 3 chapters. Got %d", len(chapters))
return
}
chapterToTest := chapters[1]
if chapterToTest.Title() != "Middle" {
t.Errorf("Bad Chapter Name. Got %s", chapterToTest.Title())
}
if chapterToTest.StartTimeSeconds != 2.0 {
t.Errorf("Bad Chapter Start Time. Got %f", chapterToTest.StartTimeSeconds)
}
if chapterToTest.EndTimeSeconds != 4.0 {
t.Errorf("Bad Chapter End Time. Got %f", chapterToTest.EndTimeSeconds)
}
}
func Test_ProbeSideData(t *testing.T) {
ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFn()
fileReader, err := os.Open("assets/test.mov")
if err != nil {
t.Errorf("Error opening test file: %v", err)
}
data, err := ProbeReader(ctx, fileReader)
if err != nil {
t.Errorf("Error getting data: %v", err)
}
videoStream := data.FirstVideoStream()
if videoStream == nil {
t.Error("Video Stream was nil")
return
}
sideData, err := videoStream.SideDataList.GetDisplayMatrix()
if err != nil {
t.Errorf("Error getting display matrix: %v", err)
}
if sideData.Rotation != -180 {
t.Errorf("Expected rotation to be -180, got %d", sideData.Rotation)
}
}