Skip to content

Commit

Permalink
Fix metadata extraction for ivf container
Browse files Browse the repository at this point in the history
Add more test video files and few more tests.
  • Loading branch information
jurisevo committed Oct 29, 2024
1 parent 24b6167 commit 8a76dc8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/evolution-gaming/ease

go 1.21
go 1.22

require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
Expand Down
8 changes: 5 additions & 3 deletions internal/tools/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
package tools

import (
"cmp"
"encoding/json"
"fmt"
"math"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -99,8 +99,10 @@ func FfprobeExtractMetadata(videoFile string) (video.Metadata, error) {
}

vmeta = video.Metadata(meta.Streams[0])
// For mkv container Streams does not contain duration, so we have to look into Format.
vmeta.Duration = math.Max(vmeta.Duration, meta.Format.Duration)
// For mkv, ivf containers Streams do not contain duration and bitrate, set it from
// section Format as fallback.
vmeta.Duration = cmp.Or(vmeta.Duration, meta.Format.Duration)
vmeta.BitRate = cmp.Or(vmeta.BitRate, meta.Format.BitRate)
logging.Debugf("%s %+v", videoFile, vmeta)

return vmeta, nil
Expand Down
91 changes: 75 additions & 16 deletions internal/tools/ffmpeg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,81 @@ func Test_Path_Negative(t *testing.T) {
}

func Test_FfprobeExtractMetadata(t *testing.T) {
videoFile := "../../testdata/video/testsrc02.mp4"
t.Run("Should extract VideoMetadata from video file", func(t *testing.T) {
want := video.Metadata{
Duration: 10,
Width: 1280,
Height: 720,
BitRate: 86740,
FrameCount: 240,
CodecName: "h264",
FrameRate: "24/1",
}

got, err := FfprobeExtractMetadata(videoFile)
assert.NoError(t, err)
assert.Equal(t, want, got)
})
type testCase struct {
videoFile string
wantMetadata video.Metadata
}

tests := map[string]testCase{
"h264 mp4 small": {
videoFile: "../../testdata/video/testsrc01.mp4",
wantMetadata: video.Metadata{
Duration: 1,
Width: 320,
Height: 240,
BitRate: 56112,
FrameCount: 10,
CodecName: "h264",
FrameRate: "10/1",
},
},
"h264 mp4 large": {
videoFile: "../../testdata/video/testsrc02.mp4",
wantMetadata: video.Metadata{
Duration: 10,
Width: 1280,
Height: 720,
BitRate: 86740,
FrameCount: 240,
CodecName: "h264",
FrameRate: "24/1",
},
},
"av1 ivf": {
videoFile: "../../testdata/video/testsrc03.ivf",
wantMetadata: video.Metadata{
Duration: 1,
Width: 1920,
Height: 1080,
BitRate: 114648,
FrameCount: 24,
CodecName: "av1",
FrameRate: "24/1",
},
},
"av1 mp4": {
videoFile: "../../testdata/video/testsrc04.mp4",
wantMetadata: video.Metadata{
Duration: 1,
Width: 1920,
Height: 1080,
BitRate: 111704,
FrameCount: 24,
CodecName: "av1",
FrameRate: "24/1",
},
},
"h264 mkv": {
videoFile: "../../testdata/video/testsrc05.mkv",
wantMetadata: video.Metadata{
Duration: 1,
Width: 320,
Height: 240,
BitRate: 62648,
FrameCount: 10,
CodecName: "h264",
FrameRate: "10/1",
},
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
gotMetadata, err := FfprobeExtractMetadata(tc.videoFile)
assert.NoError(t, err)
assert.Equal(t, tc.wantMetadata, gotMetadata)
})
}
}

func Test_FfprobeExtractMetadata_Negative(t *testing.T) {
Expand Down
Binary file added testdata/video/testsrc03.ivf
Binary file not shown.
Binary file added testdata/video/testsrc04.mp4
Binary file not shown.
Binary file added testdata/video/testsrc05.mkv
Binary file not shown.

0 comments on commit 8a76dc8

Please sign in to comment.