test_mp4.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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"),
  38. [
  39. (21, 42),
  40. (-21, 42),
  41. (21, -42),
  42. (-21, -42),
  43. (0, 42),
  44. ],
  45. )
  46. def test_get_free_int_ratio(empty_mp4_path, nominator, denominator):
  47. mutagen_file = mutagen.File(empty_mp4_path)
  48. mutagen_file.tags["----:foo:bar"] = [
  49. mutagen.mp4.MP4FreeForm(
  50. dataformat=mutagen.mp4.AtomDataType.INTEGER,
  51. data=i.to_bytes(4, byteorder="big", signed=True),
  52. )
  53. for i in (nominator, denominator)
  54. ]
  55. mutagen_file.save()
  56. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  57. assert mp4_iface.get_free_int_ratio("foo:bar") == (nominator, denominator)
  58. @pytest.mark.parametrize(
  59. ("nominator", "denominator", "expected_tag_data"),
  60. [
  61. (21, 42, [b"\x15", b"\x2a"]),
  62. (-21, 42, [b"\xeb", b"\x2a"]),
  63. (21, -42, [b"\x15", b"\xd6"]),
  64. (-21, -42, [b"\xeb", b"\xd6"]),
  65. (0, 42, [b"\x00", b"\x2a"]),
  66. ],
  67. )
  68. def test_set_free_int_ratio(empty_mp4_path, nominator, denominator, expected_tag_data):
  69. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  70. mp4_iface.set_free_int_ratio("test:some-ratio", nominator, denominator)
  71. mp4_iface.save()
  72. mutagen_file = mutagen.File(empty_mp4_path)
  73. assert len(mutagen_file.tags) == 1
  74. tag = mutagen_file.tags["----:test:some-ratio"]
  75. assert len(tag) == 2
  76. assert all(f.dataformat == mutagen.mp4.AtomDataType.INTEGER for f in tag)
  77. assert [bytes(f) for f in tag] == expected_tag_data
  78. @pytest.mark.parametrize(
  79. ("integer", "expected_tag_data"),
  80. [
  81. (0, b"\x00"),
  82. (4, b"\x04"),
  83. (-1, b"\xff"),
  84. (-2, b"\xfe"),
  85. (2 ** 6, b"\x40"),
  86. (2 ** 7 - 1, b"\x7f"),
  87. (2 ** 7, b"\x00\x80"),
  88. (2 ** 8, b"\x01\x00"),
  89. (2 ** 24, b"\x01\x00\x00\x00"),
  90. (2 ** 31 - 1, b"\x7f\xff\xff\xff"),
  91. ],
  92. )
  93. def test_set_free_int(empty_mp4_path, integer, expected_tag_data):
  94. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  95. mp4_iface.set_free_int("foo:bar", integer)
  96. mp4_iface.save()
  97. mutagen_file = mutagen.File(empty_mp4_path)
  98. assert len(mutagen_file.tags) == 1
  99. (tag,) = mutagen_file.get("----:foo:bar")
  100. assert tag.dataformat == mutagen.mp4.AtomDataType.INTEGER
  101. assert bytes(tag) == expected_tag_data
  102. assert MP4._freeform_to_int(tag) == integer