fix(audiobook_library): use mutage mp4 for m4b files
This commit is contained in:
1 parent
5c0ae62acb
commit
3c2dbafc44
1 file changed
+46
-12
@@ -15,10 +15,21 @@ from pathlib import Path
|
||||
from serverlibrary import *
|
||||
from mutagen.easyid3 import EasyID3
|
||||
from mutagen.id3 import ID3, ID3NoHeaderError
|
||||
|
||||
from mutagen.mp4 import MP4
|
||||
|
||||
logging.setLoggerClass(CustomLogger)
|
||||
logger: CustomLogger = logging.getLogger(__name__) # type: ignore[assignment]
|
||||
MP4_MAP = {
|
||||
"artist": "\xa9ART",
|
||||
"album": "\xa9alb",
|
||||
"title": "\xa9nam",
|
||||
"genre": "\xa9gen",
|
||||
"comment": "\xa9cmt",
|
||||
"composer": "\xa9wrt",
|
||||
"albumartist": "aART",
|
||||
"grouping": "\xa9grp",
|
||||
"description": "desc",
|
||||
}
|
||||
|
||||
metadata_fields = [
|
||||
"artist",
|
||||
@@ -179,28 +190,51 @@ def get_duration_ms(media: Path) -> int:
|
||||
|
||||
def write_metadata(file, fields, values, dry_run=False):
|
||||
logger.debug(f"Writing metadata for {file}")
|
||||
|
||||
changes = {
|
||||
field.lower().replace(" ", ""): str(value)
|
||||
for field, value in zip(fields, values)
|
||||
}
|
||||
|
||||
logger.trace(f"Changes: {changes}")
|
||||
|
||||
if dry_run:
|
||||
return
|
||||
|
||||
try:
|
||||
try:
|
||||
tags = EasyID3(file)
|
||||
except ID3NoHeaderError:
|
||||
tags = EasyID3()
|
||||
tags.save(file)
|
||||
suffix = file.suffix.lower()
|
||||
|
||||
if suffix == ".mp3":
|
||||
tags = EasyID3(file)
|
||||
|
||||
for field, value in changes.items():
|
||||
if value:
|
||||
tags[field] = [value]
|
||||
elif field in tags:
|
||||
del tags[field]
|
||||
for field, value in changes.items():
|
||||
if value:
|
||||
tags[field] = [value]
|
||||
elif field in tags:
|
||||
del tags[field]
|
||||
|
||||
tags.save()
|
||||
|
||||
elif suffix in {".m4b", ".m4a", ".mp4"}:
|
||||
tags = MP4(file)
|
||||
|
||||
for field, value in changes.items():
|
||||
mp4_key = MP4_MAP.get(field)
|
||||
|
||||
if not mp4_key:
|
||||
logger.debug(f"Unsupported MP4 tag: {field}")
|
||||
continue
|
||||
|
||||
if value:
|
||||
tags[mp4_key] = [value]
|
||||
else:
|
||||
tags.pop(mp4_key, None)
|
||||
|
||||
tags.save()
|
||||
|
||||
else:
|
||||
logger.warning(f"Unsupported file type: {file}")
|
||||
|
||||
tags.save()
|
||||
except Exception as e:
|
||||
logger.exception(f"Error writing metadata for {file}: {e}")
|
||||
|
||||
|
||||
Reference in new issue
Block a user