test_mp4.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import mutagen
  2. import pytest
  3. from symuid._tag_interface import MP4
  4. # pylint: disable=protected-access
  5. def test_set_comment(empty_mp4_path):
  6. iface = MP4(mutagen.File(empty_mp4_path))
  7. assert iface.get_comment() is None
  8. iface.set_comment("latin")
  9. assert iface.get_comment() == "latin"
  10. iface.set_comment("mp4 你好")
  11. assert iface.get_comment() == "mp4 你好"
  12. iface.save()
  13. iface_reread = MP4(mutagen.File(empty_mp4_path))
  14. assert iface_reread.get_comment() == "mp4 你好"
  15. tags = mutagen.File(iface.track_path).tags
  16. assert len(tags) == 1
  17. assert tags.items()[0] == ("©cmt", ["mp4 你好"])
  18. def test__get_free_uuid(empty_mp4_path):
  19. uuid = b"h\x97\x8c_1?B\t\x9d\xa3$\xdf\xd0Y\xa1\xc2"
  20. mutagen_file = mutagen.File(empty_mp4_path)
  21. mutagen_file["----:foo:bar"] = mutagen.mp4.MP4FreeForm(
  22. dataformat=mutagen.mp4.AtomDataType.UUID, data=uuid
  23. )
  24. mutagen_file.save()
  25. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  26. assert mp4_iface._get_free_uuid("foo:bar") == uuid
  27. def test_get_track_uuid(empty_mp4_path):
  28. uuid = b"h\x97\x8c_1?B\t\x9d\xa3$\xdf\xd0Y\xa1\xc2"
  29. mutagen_file = mutagen.File(empty_mp4_path)
  30. mutagen_file["----:symuid:uuid"] = mutagen.mp4.MP4FreeForm(
  31. dataformat=mutagen.mp4.AtomDataType.UUID, data=uuid
  32. )
  33. mutagen_file.save()
  34. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  35. assert mp4_iface.get_track_uuid() == uuid
  36. @pytest.mark.parametrize(
  37. ("nominator", "denominator"), [(21, 42), (-21, 42), (21, -42), (-21, -42), (0, 42),]
  38. )
  39. def test_get_free_int_ratio(empty_mp4_path, nominator, denominator):
  40. mutagen_file = mutagen.File(empty_mp4_path)
  41. mutagen_file.tags["----:foo:bar"] = [
  42. mutagen.mp4.MP4FreeForm(
  43. dataformat=mutagen.mp4.AtomDataType.INTEGER,
  44. data=i.to_bytes(4, byteorder="big", signed=True),
  45. )
  46. for i in (nominator, denominator)
  47. ]
  48. mutagen_file.save()
  49. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  50. assert mp4_iface.get_free_int_ratio("foo:bar") == (nominator, denominator)
  51. @pytest.mark.parametrize(
  52. ("nominator", "denominator", "expected_tag_data"),
  53. [
  54. (21, 42, [b"\x15", b"\x2a"]),
  55. (-21, 42, [b"\xeb", b"\x2a"]),
  56. (21, -42, [b"\x15", b"\xd6"]),
  57. (-21, -42, [b"\xeb", b"\xd6"]),
  58. (0, 42, [b"\x00", b"\x2a"]),
  59. ],
  60. )
  61. def test_set_free_int_ratio(empty_mp4_path, nominator, denominator, expected_tag_data):
  62. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  63. mp4_iface.set_free_int_ratio("test:some-ratio", nominator, denominator)
  64. mp4_iface.save()
  65. mutagen_file = mutagen.File(empty_mp4_path)
  66. assert len(mutagen_file.tags) == 1
  67. tag = mutagen_file.tags["----:test:some-ratio"]
  68. assert len(tag) == 2
  69. assert all(f.dataformat == mutagen.mp4.AtomDataType.INTEGER for f in tag)
  70. assert [bytes(f) for f in tag] == expected_tag_data
  71. @pytest.mark.parametrize(
  72. ("integer", "expected_tag_data"),
  73. [
  74. (0, b"\x00"),
  75. (4, b"\x04"),
  76. (-1, b"\xff"),
  77. (-2, b"\xfe"),
  78. (2 ** 6, b"\x40"),
  79. (2 ** 7 - 1, b"\x7f"),
  80. (2 ** 7, b"\x00\x80"),
  81. (2 ** 8, b"\x01\x00"),
  82. (2 ** 24, b"\x01\x00\x00\x00"),
  83. (2 ** 31 - 1, b"\x7f\xff\xff\xff"),
  84. ],
  85. )
  86. def test_set_free_int(empty_mp4_path, integer, expected_tag_data):
  87. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  88. mp4_iface.set_free_int("foo:bar", integer)
  89. mp4_iface.save()
  90. mutagen_file = mutagen.File(empty_mp4_path)
  91. assert len(mutagen_file.tags) == 1
  92. (tag,) = mutagen_file.get("----:foo:bar")
  93. assert tag.dataformat == mutagen.mp4.AtomDataType.INTEGER
  94. assert bytes(tag) == expected_tag_data
  95. assert MP4._freeform_to_int(tag) == integer