from pathlib import Path from ffwrap.probe import parse_probe from ffwrap.tools import parse_encoders SAMPLE = { "format": {"format_name": "matroska,webm", "duration": "1421.5"}, "streams": [ {"index": 0, "codec_type": "video", "codec_name": "hevc", "width": 1920, "height": 1080, "avg_frame_rate": "24000/1001", "pix_fmt": "yuv420p10le", "disposition": {"default": 1}}, {"index": 1, "codec_type": "audio", "codec_name": "eac3", "channels": 6, "channel_layout": "5.1(side)", "sample_rate": "48000", "tags": {"language": "jpn", "title": "Surround"}, "disposition": {"default": 1}}, {"index": 2, "codec_type": "audio", "codec_name": "aac", "channels": 2, "sample_rate": "44100", "tags": {"LANGUAGE": "und", "BPS": "128000"}}, {"index": 3, "codec_type": "subtitle", "codec_name": "subrip", "tags": {"language": "eng"}, "disposition": {"forced": 1}}, {"index": 4, "codec_type": "video", "codec_name": "mjpeg", "width": 600, "height": 900, "disposition": {"attached_pic": 1}}, ], } def test_parse_probe(): info = parse_probe(Path("x.mkv"), SAMPLE) assert info.duration == 1421.5 assert info.video.index == 0 and info.video.codec == "hevc" assert abs(info.video.fps - 23.976) < 0.001 assert [s.index for s in info.audio] == [1, 2] a1, a2 = info.audio assert a1.language == "jpn" and a1.title == "Surround" and a1.default and a1.channels == 6 assert a2.language is None and a2.bit_rate == 128000 # "und" -> None, BPS tag used assert info.subtitles[0].forced assert "EAC3" in a1.describe() and "5.1" in a1.describe() def test_rotation_swaps_display_size(): data = {"format": {}, "streams": [{"index": 0, "codec_type": "video", "codec_name": "h264", "width": 1920, "height": 1080, "side_data_list": [{"rotation": -90}]}]} info = parse_probe(Path("phone.mp4"), data) assert info.video.display_size == (1080, 1920) def test_duration_falls_back_to_streams(): data = {"format": {}, "streams": [{"index": 0, "codec_type": "audio", "codec_name": "aac", "duration": "12.5"}]} assert parse_probe(Path("a.m4a"), data).duration == 12.5 def test_parse_encoders(): text = """Encoders: V..... = Video ------ V....D libx264 libx264 H.264 / AVC A....D aac AAC (Advanced Audio Coding) S..... mov_text 3GPP Timed Text subtitle """ assert parse_encoders(text) == {"libx264", "aac", "mov_text"}