forked from vansante/go-ffprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffprobe_test.go
102 lines (82 loc) · 2.09 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
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)
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.")
}
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(StreamAny)
if len(stream) != 2 {
t.Errorf("It should have two streams.")
}
// test Format.Duration
dration := data.Format.Duration()
if dration.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.")
}
}