130 lines
3.3 KiB
Go
130 lines
3.3 KiB
Go
package media
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestParseProbeJSON_MultiAudio(t *testing.T) {
|
|
sample := `{
|
|
"format": {
|
|
"filename": "clip.mp4",
|
|
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
|
|
"duration": "125.500000",
|
|
"size": "104857600",
|
|
"bit_rate": "6683824"
|
|
},
|
|
"streams": [
|
|
{
|
|
"index": 0,
|
|
"codec_type": "video",
|
|
"codec_name": "h264",
|
|
"width": 1920,
|
|
"height": 1080,
|
|
"pix_fmt": "yuv420p",
|
|
"avg_frame_rate": "60/1",
|
|
"r_frame_rate": "60/1"
|
|
},
|
|
{
|
|
"index": 1,
|
|
"codec_type": "audio",
|
|
"codec_name": "aac",
|
|
"channels": 2,
|
|
"channel_layout": "stereo",
|
|
"sample_rate": "48000",
|
|
"tags": {"title": "Desktop Audio"}
|
|
},
|
|
{
|
|
"index": 2,
|
|
"codec_type": "audio",
|
|
"codec_name": "aac",
|
|
"channels": 1,
|
|
"channel_layout": "mono",
|
|
"sample_rate": "48000",
|
|
"tags": {"title": "Microphone", "language": "rus"}
|
|
}
|
|
]
|
|
}`
|
|
|
|
info, err := ParseProbeJSON([]byte(sample), `C:\clips\clip.mp4`, 123)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if info.Width != 1920 || info.Height != 1080 {
|
|
t.Fatalf("unexpected resolution: %dx%d", info.Width, info.Height)
|
|
}
|
|
if info.FPS != 60 {
|
|
t.Fatalf("expected 60 fps, got %v", info.FPS)
|
|
}
|
|
if info.Duration != 125.5 {
|
|
t.Fatalf("expected duration 125.5, got %v", info.Duration)
|
|
}
|
|
if len(info.AudioStreams) != 2 {
|
|
t.Fatalf("expected 2 audio streams, got %d", len(info.AudioStreams))
|
|
}
|
|
if info.AudioStreams[1].AudioOrder != 1 || info.AudioStreams[1].Title != "Microphone" || info.AudioStreams[1].Language != "rus" {
|
|
t.Fatalf("unexpected audio stream 1: %+v", info.AudioStreams[1])
|
|
}
|
|
if !info.BrowserNative {
|
|
t.Fatalf("expected mp4 h264+aac to be browser native")
|
|
}
|
|
}
|
|
|
|
func TestParseProbeJSON_NoVideo(t *testing.T) {
|
|
sample := `{"format":{"duration":"10"},"streams":[{"index":0,"codec_type":"audio","codec_name":"mp3"}]}`
|
|
if _, err := ParseProbeJSON([]byte(sample), "audio.mp3", 100); err == nil {
|
|
t.Fatalf("expected error when no video stream exists")
|
|
}
|
|
}
|
|
|
|
func TestParseRationalFPS(t *testing.T) {
|
|
cases := []struct {
|
|
in string
|
|
want float64
|
|
}{
|
|
{"60/1", 60},
|
|
{"30000/1001", 30000.0 / 1001.0},
|
|
{"0/0", 0},
|
|
{"", 0},
|
|
{"24", 24},
|
|
}
|
|
for _, tc := range cases {
|
|
got := parseRationalFPS(tc.in)
|
|
if got != tc.want {
|
|
t.Fatalf("parseRationalFPS(%q) = %v, want %v", tc.in, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseProbeJSON_H264PixFmtCompatibility(t *testing.T) {
|
|
makeSample := func(pixFmt string) []byte {
|
|
return []byte(`{
|
|
"format": {"duration": "10.0"},
|
|
"streams": [{"index": 0, "codec_type": "video", "codec_name": "h264", "width": 1280, "height": 720, "pix_fmt": "` + pixFmt + `"}]
|
|
}`)
|
|
}
|
|
|
|
okInfo, err := ParseProbeJSON(makeSample("yuv420p"), "clip.mp4", 100)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !okInfo.BrowserNative {
|
|
t.Fatalf("expected yuv420p mp4 to be browser native")
|
|
}
|
|
|
|
hi10Info, err := ParseProbeJSON(makeSample("yuv420p10le"), "clip10.mp4", 100)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if hi10Info.BrowserNative {
|
|
t.Fatalf("expected 10-bit yuv420p10le mp4 NOT to be browser native")
|
|
}
|
|
|
|
fmt422Info, err := ParseProbeJSON(makeSample("yuv422p"), "clip422.mp4", 100)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if fmt422Info.BrowserNative {
|
|
t.Fatalf("expected yuv422p mp4 NOT to be browser native")
|
|
}
|
|
}
|