forked from vansante/go-ffprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffprobe_test.go
173 lines (141 loc) · 3.99 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
package ffprobe
import (
"context"
"fmt"
"net/http"
"os"
"testing"
"time"
)
const (
testPath = "assets/test.mp4"
)
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_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)
}
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 validateData(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)
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) != 2 {
t.Errorf("It should have two streams.")
}
// 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.")
}
}
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.GetSideData("Display Matrix")
if err != nil {
t.Errorf("Error getting side data: %v", err)
}
rotation, err := sideData.GetInt("rotation")
if err != nil {
t.Errorf("Error getting rotation field: %v", err)
}
if rotation != -180 {
t.Errorf("Expected rotation to be -180, got %d", rotation)
}
}