|
@@ -9,15 +9,15 @@ import subprocess
|
|
|
|
|
|
# http://id3.org/id3v2.4.0-frames#4.1.
|
|
|
TRACK_UUID_ID3_OWNER_ID = 'symuid'
|
|
|
-
|
|
|
-# TODO http://mutagen.readthedocs.io/en/latest/api/mp4.html
|
|
|
-
|
|
|
+# freeform keys start with '----'
|
|
|
+# http://mutagen.readthedocs.io/en/latest/api/mp4.html
|
|
|
+TRACK_UUID_MP4_TAG = '----:symuid:uuid'
|
|
|
|
|
|
def generate_uuid():
|
|
|
return subprocess.check_output(['uuid', '-v', '4', '-F', 'BIN']).strip()
|
|
|
|
|
|
|
|
|
-def get_uuid(id3_tags):
|
|
|
+def get_uuid_id3(id3_tags):
|
|
|
assert isinstance(id3_tags, mutagen.id3.ID3), type(id3_tags)
|
|
|
ufids = id3_tags.getall('UFID')
|
|
|
for ufid in ufids:
|
|
@@ -26,8 +26,8 @@ def get_uuid(id3_tags):
|
|
|
return None
|
|
|
|
|
|
|
|
|
-def get_or_assign_uuid(id3_tags):
|
|
|
- uuid = get_uuid(id3_tags)
|
|
|
+def get_or_assign_uuid_id3(id3_tags):
|
|
|
+ uuid = get_uuid_id3(id3_tags)
|
|
|
if uuid is None:
|
|
|
# mutagen.id3._specs.EncodedTextSpec.write encodes 'owner'
|
|
|
id3_tags.add(mutagen.id3.UFID(
|
|
@@ -36,11 +36,25 @@ def get_or_assign_uuid(id3_tags):
|
|
|
))
|
|
|
id3_tags.save()
|
|
|
id3_tags.load(filename=id3_tags.filename)
|
|
|
- uuid = get_uuid(id3_tags)
|
|
|
+ uuid = get_uuid_id3(id3_tags)
|
|
|
print("{!r}: assigned uuid {!r}".format(id3_tags.filename, uuid))
|
|
|
assert uuid is not None
|
|
|
return uuid
|
|
|
|
|
|
+def get_or_assign_uuid_mp4(mp4_file):
|
|
|
+ if not TRACK_UUID_MP4_TAG in mp4_file:
|
|
|
+ mp4_file[TRACK_UUID_MP4_TAG] = mutagen.mp4.MP4FreeForm(
|
|
|
+ data=generate_uuid(),
|
|
|
+ # https://mutagen.readthedocs.io/en/latest/api/mp4.html#mutagen.mp4.AtomDataType.UUID
|
|
|
+ dataformat= mutagen.mp4.AtomDataType.UUID,
|
|
|
+ )
|
|
|
+ mp4_file.save()
|
|
|
+ mp4_file.load(filename=mp4_file.filename)
|
|
|
+ print("{!r}: assigned uuid {!r}".format(
|
|
|
+ mp4_file.filename, mp4_file[TRACK_UUID_MP4_TAG][0],
|
|
|
+ ))
|
|
|
+ return mp4_file[TRACK_UUID_MP4_TAG][0]
|
|
|
+
|
|
|
|
|
|
def symuid(path):
|
|
|
if os.path.isdir(path):
|
|
@@ -54,10 +68,12 @@ def symuid(path):
|
|
|
raise Exception(path)
|
|
|
if not f:
|
|
|
print("{!r}: unsupported filetype, ignored".format(path))
|
|
|
- elif isinstance(f.tags, mutagen.mp4.MP4Tags):
|
|
|
- print("{!r}: mp4 tags not supported, ignored".format(path))
|
|
|
+ elif isinstance(f, mutagen.mp4.MP4):
|
|
|
+ get_or_assign_uuid_mp4(f)
|
|
|
+ elif isinstance(f.tags, mutagen.id3.ID3):
|
|
|
+ get_or_assign_uuid_id3(f.tags)
|
|
|
else:
|
|
|
- get_or_assign_uuid(f.tags)
|
|
|
+ raise Exception(f)
|
|
|
|
|
|
|
|
|
def _init_argparser():
|