Browse Source

implement symuid._tag_interface.MP4.set_comment()

Fabian Peter Hammerle 4 years ago
parent
commit
aef8354643

+ 5 - 4
symuid/_tag_interface.py

@@ -129,9 +129,10 @@ class ID3(_MutagenTagInterface):
 class MP4(_MutagenTagInterface):
 
     _UUID_TAG_KEY = 'symuid:uuid'
+    _COMMENT_TAG_KEY = '\xa9cmt'
 
     def __init__(self, mutagen_file):
-        assert mutagen_file.tags, mutagen_file
+        assert mutagen_file.tags is not None, mutagen_file
         assert isinstance(mutagen_file.tags, mutagen.mp4.MP4Tags), \
             mutagen_file.tags
         super().__init__(mutagen_file)
@@ -201,10 +202,10 @@ class MP4(_MutagenTagInterface):
         )
 
     def get_comment(self):
-        return self._get_single('\xa9cmt')
+        return self._get_single(self._COMMENT_TAG_KEY)
 
-    def set_comment(self, comment):
-        raise NotImplementedError()
+    def set_comment(self, comment: str) -> None:
+        self._mutagen_file[self._COMMENT_TAG_KEY] = [comment]
 
     def get_track_uuid(self):
         return self._get_free_uuid(self._UUID_TAG_KEY)

+ 10 - 0
tests/conftest.py

@@ -21,6 +21,16 @@ def empty_id3_path(tmpdir, tracks_dir_path):
     return path
 
 
+@pytest.fixture
+def empty_mp4_path(tmpdir, tracks_dir_path):
+    path = tmpdir.join('empty.m4a').strpath
+    shutil.copyfile(
+        src=os.path.join(tracks_dir_path, 'mp4-aac-empty.m4a'),
+        dst=path,
+    )
+    return path
+
+
 @pytest.fixture
 def empty_ogg_opus_path(tmpdir, tracks_dir_path):
     path = tmpdir.join('empty.opus').strpath

+ 20 - 0
tests/tag_interface/test_mp4.py

@@ -0,0 +1,20 @@
+import mutagen
+
+from symuid._tag_interface import MP4
+
+# pylint: disable=protected-access
+
+
+def test_set_comment(empty_mp4_path):
+    iface = MP4(mutagen.File(empty_mp4_path))
+    assert iface.get_comment() is None
+    iface.set_comment('latin')
+    assert iface.get_comment() == 'latin'
+    iface.set_comment('mp4 你好')
+    assert iface.get_comment() == 'mp4 你好'
+    iface.save()
+    iface_reread = MP4(mutagen.File(empty_mp4_path))
+    assert iface_reread.get_comment() == 'mp4 你好'
+    tags = mutagen.File(iface.track_path).tags
+    assert len(tags) == 1
+    assert tags.items()[0] == ('©cmt', ['mp4 你好'])

+ 1 - 0
tests/test_track.py

@@ -198,5 +198,6 @@ def test_walk(tracks_dir_path):
         path_ignore_regex=re.compile(r'typical'))
     track_names = set(os.path.basename(t.path) for t in tracks)
     assert track_names == {'id3v2.4-empty.mp3',
+                           'mp4-aac-empty.m4a',
                            'ogg-opus-empty.opus',
                            'ogg-vorbis-empty.ogg'}

BIN
tests/tracks/mp4-aac-empty.m4a