Browse Source

mp4 interface: added set_free_int_ratio()

Fabian Peter Hammerle 3 weeks ago
parent
commit
dd065ab546
2 changed files with 27 additions and 0 deletions
  1. 8 0
      symuid/_tag_interface.py
  2. 19 0
      tests/tag_interface/test_mp4.py

+ 8 - 0
symuid/_tag_interface.py

@@ -225,6 +225,14 @@ class MP4(_MutagenTagInterface):
             data=self._int_to_freeform_data(data),
         )
 
+    def set_free_int_ratio(self, tag_label, numerator: int, denominator: int) \
+            -> typing.List[mutagen.mp4.MP4FreeForm]:
+        tag = [mutagen.mp4.MP4FreeForm(dataformat=mutagen.mp4.AtomDataType.INTEGER,
+                                       data=self._int_to_freeform_data(i))
+               for i in (numerator, denominator)]
+        self._mutagen_file.tags['----:' + tag_label] = tag
+        return tag
+
     def _set_free_uuid(self, tag_label, data):
         return self._set_free(
             tag_label=tag_label,

+ 19 - 0
tests/tag_interface/test_mp4.py

@@ -59,6 +59,25 @@ def test_get_free_int_ratio(empty_mp4_path, nominator, denominator):
     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'),