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
|
package track_test
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"git.onurgunduz.com/mp4"
"git.onurgunduz.com/mp4/track"
)
type ffStream struct {
CodecType string `json:"codec_type"`
CodecName string `json:"codec_name"`
Width int `json:"width"`
Height int `json:"height"`
SampleRate string `json:"sample_rate"`
Channels int `json:"channels"`
NbFrames string `json:"nb_frames"`
}
type ffProbe struct {
Streams []ffStream `json:"streams"`
}
// codecFamily maps an RFC 6381 codec string to the codec name ffprobe reports.
func codecFamily(codec string) string {
switch {
case strings.HasPrefix(codec, "avc1"):
return "h264"
case strings.HasPrefix(codec, "av01"):
return "av1"
case strings.HasPrefix(codec, "mp4a"):
return "aac"
}
return codec
}
func readMoov(t *testing.T, path string) []byte {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Skipf("test file not available: %v", err)
}
r := mp4.NewReader(data)
for r.Next() {
if r.Type() == mp4.TypeMoov {
return append([]byte(nil), r.RawBox()...)
}
}
t.Fatalf("no moov box in %s", path)
return nil
}
func atoi(t *testing.T, s string) int {
t.Helper()
n, err := strconv.Atoi(strings.TrimSpace(s))
if err != nil {
t.Fatalf("parse %q: %v", s, err)
}
return n
}
// TestParityWithFFprobe checks the parser against ffprobe for each sample file:
// the audio+video stream count, codec family, video dimensions, audio sample
// rate, per-track sample count, and the video sync-sample count against
// ffprobe's keyframe count. It is skipped when ffprobe or a file is absent.
func TestParityWithFFprobe(t *testing.T) {
if _, err := exec.LookPath("ffprobe"); err != nil {
t.Skip("ffprobe not installed")
}
files := []string{
"../test-data/h264-aac-10s.mp4",
"../test-data/h264-aac-60s.mp4",
"../test-data/aac-stereo-2s.m4a",
"../test-data/alac-5.1-2s.m4a",
}
for _, path := range files {
t.Run(filepath.Base(path), func(t *testing.T) {
if _, err := os.Stat(path); err != nil {
t.Skipf("test file not available: %v", err)
}
tracks, _, _, err := track.ParseTracks(readMoov(t, path))
if err != nil {
t.Fatal(err)
}
out, err := exec.Command("ffprobe", "-v", "error",
"-print_format", "json", "-show_streams", path).Output()
if err != nil {
t.Fatalf("ffprobe: %v", err)
}
var probe ffProbe
if err := json.Unmarshal(out, &probe); err != nil {
t.Fatal(err)
}
var ffVideo, ffAudio *ffStream
ffCount := 0
for i := range probe.Streams {
switch probe.Streams[i].CodecType {
case "video":
ffVideo = &probe.Streams[i]
ffCount++
case "audio":
ffAudio = &probe.Streams[i]
ffCount++
}
}
if len(tracks) != ffCount {
t.Fatalf("track count = %d, ffprobe audio+video streams = %d", len(tracks), ffCount)
}
var libVideo, libAudio *track.Track
for _, tr := range tracks {
switch tr.Kind {
case track.TrackVideo:
libVideo = tr
case track.TrackAudio:
libAudio = tr
}
}
if ffVideo != nil {
if libVideo == nil {
t.Fatal("ffprobe found a video stream, parser did not")
}
if got := codecFamily(libVideo.Codec()); got != ffVideo.CodecName {
t.Errorf("video codec = %q (%q), ffprobe = %q", libVideo.Codec(), got, ffVideo.CodecName)
}
if int(libVideo.Width) != ffVideo.Width || int(libVideo.Height) != ffVideo.Height {
t.Errorf("video dimensions = %dx%d, ffprobe = %dx%d",
libVideo.Width, libVideo.Height, ffVideo.Width, ffVideo.Height)
}
if n := atoi(t, ffVideo.NbFrames); n != len(libVideo.Samples) {
t.Errorf("video sample count = %d, ffprobe frames = %d", len(libVideo.Samples), n)
}
sync := 0
for i := range libVideo.Samples {
if libVideo.Samples[i].IsSync() {
sync++
}
}
if kf := ffprobeKeyframes(t, path); sync != kf {
t.Errorf("video sync samples = %d, ffprobe keyframes = %d", sync, kf)
}
}
if ffAudio != nil {
if libAudio == nil {
t.Fatal("ffprobe found an audio stream, parser did not")
}
if got := codecFamily(libAudio.Codec()); got != ffAudio.CodecName {
t.Errorf("audio codec = %q (%q), ffprobe = %q", libAudio.Codec(), got, ffAudio.CodecName)
}
if sr := atoi(t, ffAudio.SampleRate); sr != int(libAudio.SampleRate) {
t.Errorf("audio sample rate = %d, ffprobe = %d", libAudio.SampleRate, sr)
}
if int(libAudio.ChannelCount) != ffAudio.Channels {
t.Errorf("audio channel count = %d, ffprobe = %d", libAudio.ChannelCount, ffAudio.Channels)
}
if n := atoi(t, ffAudio.NbFrames); n != len(libAudio.Samples) {
t.Errorf("audio sample count = %d, ffprobe frames = %d", len(libAudio.Samples), n)
}
}
})
}
}
// ffprobeKeyframes counts the keyframes in the first video stream.
func ffprobeKeyframes(t *testing.T, path string) int {
t.Helper()
out, err := exec.Command("ffprobe", "-v", "error", "-select_streams", "v:0",
"-skip_frame", "nokey", "-show_entries", "frame=pts_time",
"-of", "csv=p=0", path).Output()
if err != nil {
t.Fatalf("ffprobe keyframes: %v", err)
}
trimmed := strings.TrimSpace(string(out))
if trimmed == "" {
return 0
}
return strings.Count(trimmed, "\n") + 1
}
|