test_mp4.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. mutagen_file.save()
  24. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  25. assert mp4_iface._get_free_uuid('foo:bar') == uuid
  26. def test_get_track_uuid(empty_mp4_path):
  27. uuid = b'h\x97\x8c_1?B\t\x9d\xa3$\xdf\xd0Y\xa1\xc2'
  28. mutagen_file = mutagen.File(empty_mp4_path)
  29. mutagen_file['----:symuid:uuid'] = mutagen.mp4.MP4FreeForm(
  30. dataformat=mutagen.mp4.AtomDataType.UUID, data=uuid)
  31. mutagen_file.save()
  32. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  33. assert mp4_iface.get_track_uuid() == uuid
  34. @pytest.mark.parametrize(('nominator', 'denominator'), [
  35. (21, 42),
  36. (-21, 42),
  37. (21, -42),
  38. (-21, -42),
  39. (0, 42),
  40. ])
  41. def test_get_free_int_ratio(empty_mp4_path, nominator, denominator):
  42. mutagen_file = mutagen.File(empty_mp4_path)
  43. mutagen_file.tags['----:foo:bar'] = [
  44. mutagen.mp4.MP4FreeForm(dataformat=mutagen.mp4.AtomDataType.INTEGER,
  45. data=i.to_bytes(4, byteorder='big', signed=True))
  46. for i in (nominator, denominator)]
  47. mutagen_file.save()
  48. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  49. assert mp4_iface.get_free_int_ratio('foo:bar') == (nominator, denominator)
  50. @pytest.mark.parametrize(('nominator', 'denominator', 'expected_tag_data'), [
  51. (21, 42, [b'\x15', b'\x2a']),
  52. (-21, 42, [b'\xeb', b'\x2a']),
  53. (21, -42, [b'\x15', b'\xd6']),
  54. (-21, -42, [b'\xeb', b'\xd6']),
  55. (0, 42, [b'\x00', b'\x2a']),
  56. ])
  57. def test_set_free_int_ratio(empty_mp4_path, nominator, denominator, expected_tag_data):
  58. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  59. mp4_iface.set_free_int_ratio('test:some-ratio', nominator, denominator)
  60. mp4_iface.save()
  61. mutagen_file = mutagen.File(empty_mp4_path)
  62. assert len(mutagen_file.tags) == 1
  63. tag = mutagen_file.tags['----:test:some-ratio']
  64. assert len(tag) == 2
  65. assert all(f.dataformat == mutagen.mp4.AtomDataType.INTEGER for f in tag)
  66. assert [bytes(f) for f in tag] == expected_tag_data
  67. @pytest.mark.parametrize(('integer', 'expected_tag_data'), [
  68. (0, b'\x00'),
  69. (4, b'\x04'),
  70. (-1, b'\xff'),
  71. (-2, b'\xfe'),
  72. (2**6, b'\x40'),
  73. (2**7-1, b'\x7f'),
  74. (2**7, b'\x00\x80'),
  75. (2**8, b'\x01\x00'),
  76. (2**24, b'\x01\x00\x00\x00'),
  77. (2**31-1, b'\x7f\xff\xff\xff'),
  78. ])
  79. def test_set_free_int(empty_mp4_path, integer, expected_tag_data):
  80. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  81. mp4_iface.set_free_int('foo:bar', integer)
  82. mp4_iface.save()
  83. mutagen_file = mutagen.File(empty_mp4_path)
  84. assert len(mutagen_file.tags) == 1
  85. tag, = mutagen_file.get('----:foo:bar')
  86. assert tag.dataformat == mutagen.mp4.AtomDataType.INTEGER
  87. assert bytes(tag) == expected_tag_data
  88. assert MP4._freeform_to_int(tag) == integer