import mutagen import pytest from symuid._tag_interface import MP4 # pylint: disable=protected-access def test_set_comment(empty_mp4_path): iface = MP4(mutagen.File(empty_mp4_path)) assert iface.get_comment() is None iface.set_comment("latin") assert iface.get_comment() == "latin" iface.set_comment("mp4 你好") assert iface.get_comment() == "mp4 你好" iface.save() iface_reread = MP4(mutagen.File(empty_mp4_path)) assert iface_reread.get_comment() == "mp4 你好" tags = mutagen.File(iface.track_path).tags assert len(tags) == 1 assert tags.items()[0] == ("©cmt", ["mp4 你好"]) def test__get_free_uuid(empty_mp4_path): uuid = b"h\x97\x8c_1?B\t\x9d\xa3$\xdf\xd0Y\xa1\xc2" mutagen_file = mutagen.File(empty_mp4_path) mutagen_file["----:foo:bar"] = mutagen.mp4.MP4FreeForm( dataformat=mutagen.mp4.AtomDataType.UUID, data=uuid ) mutagen_file.save() mp4_iface = MP4(mutagen.File(empty_mp4_path)) assert mp4_iface._get_free_uuid("foo:bar") == uuid def test_get_track_uuid(empty_mp4_path): uuid = b"h\x97\x8c_1?B\t\x9d\xa3$\xdf\xd0Y\xa1\xc2" mutagen_file = mutagen.File(empty_mp4_path) mutagen_file["----:symuid:uuid"] = mutagen.mp4.MP4FreeForm( dataformat=mutagen.mp4.AtomDataType.UUID, data=uuid ) mutagen_file.save() mp4_iface = MP4(mutagen.File(empty_mp4_path)) assert mp4_iface.get_track_uuid() == uuid @pytest.mark.parametrize( ("nominator", "denominator"), [(21, 42), (-21, 42), (21, -42), (-21, -42), (0, 42),] ) def test_get_free_int_ratio(empty_mp4_path, nominator, denominator): mutagen_file = mutagen.File(empty_mp4_path) mutagen_file.tags["----:foo:bar"] = [ mutagen.mp4.MP4FreeForm( dataformat=mutagen.mp4.AtomDataType.INTEGER, data=i.to_bytes(4, byteorder="big", signed=True), ) for i in (nominator, denominator) ] mutagen_file.save() mp4_iface = MP4(mutagen.File(empty_mp4_path)) assert mp4_iface.get_free_int_ratio("foo:bar") == (nominator, denominator) @pytest.mark.parametrize( ("nominator", "denominator", "expected_tag_data"), [ (21, 42, [b"\x15", b"\x2a"]), (-21, 42, [b"\xeb", b"\x2a"]), (21, -42, [b"\x15", b"\xd6"]), (-21, -42, [b"\xeb", b"\xd6"]), (0, 42, [b"\x00", b"\x2a"]), ], ) def test_set_free_int_ratio(empty_mp4_path, nominator, denominator, expected_tag_data): mp4_iface = MP4(mutagen.File(empty_mp4_path)) mp4_iface.set_free_int_ratio("test:some-ratio", nominator, denominator) mp4_iface.save() mutagen_file = mutagen.File(empty_mp4_path) assert len(mutagen_file.tags) == 1 tag = mutagen_file.tags["----:test:some-ratio"] assert len(tag) == 2 assert all(f.dataformat == mutagen.mp4.AtomDataType.INTEGER for f in tag) assert [bytes(f) for f in tag] == expected_tag_data @pytest.mark.parametrize( ("integer", "expected_tag_data"), [ (0, b"\x00"), (4, b"\x04"), (-1, b"\xff"), (-2, b"\xfe"), (2 ** 6, b"\x40"), (2 ** 7 - 1, b"\x7f"), (2 ** 7, b"\x00\x80"), (2 ** 8, b"\x01\x00"), (2 ** 24, b"\x01\x00\x00\x00"), (2 ** 31 - 1, b"\x7f\xff\xff\xff"), ], ) def test_set_free_int(empty_mp4_path, integer, expected_tag_data): mp4_iface = MP4(mutagen.File(empty_mp4_path)) mp4_iface.set_free_int("foo:bar", integer) mp4_iface.save() mutagen_file = mutagen.File(empty_mp4_path) assert len(mutagen_file.tags) == 1 (tag,) = mutagen_file.get("----:foo:bar") assert tag.dataformat == mutagen.mp4.AtomDataType.INTEGER assert bytes(tag) == expected_tag_data assert MP4._freeform_to_int(tag) == integer