test_mp4.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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(('integer', 'expected_tag_data'), [
  51. (0, b'\x00'),
  52. (4, b'\x04'),
  53. (-1, b'\xff'),
  54. (-2, b'\xfe'),
  55. (2**6, b'\x40'),
  56. (2**7-1, b'\x7f'),
  57. (2**7, b'\x00\x80'),
  58. (2**8, b'\x01\x00'),
  59. (2**24, b'\x01\x00\x00\x00'),
  60. (2**31-1, b'\x7f\xff\xff\xff'),
  61. ])
  62. def test_set_free_int(empty_mp4_path, integer, expected_tag_data):
  63. mp4_iface = MP4(mutagen.File(empty_mp4_path))
  64. mp4_iface.set_free_int('foo:bar', integer)
  65. mp4_iface.save()
  66. mutagen_file = mutagen.File(empty_mp4_path)
  67. assert len(mutagen_file.tags) == 1
  68. tag, = mutagen_file.get('----:foo:bar')
  69. assert tag.dataformat == mutagen.mp4.AtomDataType.INTEGER
  70. assert bytes(tag) == expected_tag_data
  71. assert MP4._freeform_to_int(tag) == integer