feat(audiobook_library): use exiftool instead of kid3 and ffprobe

This commit is contained in:
Lucas Vivo committed 2026-10-30 13:04:24 +01:00
1 parent 0affe5f64a
commit fa3b9fb265
1 file changed
+22 -21
@@ -108,12 +108,8 @@ def get_metadata_all(file: Path) -> dict[str, str]:
try: try:
result = subprocess.run( result = subprocess.run(
[ [
"ffprobe", "exiftool",
"-v", "-j",
"quiet",
"-print_format",
"json",
"-show_format",
str(file), str(file),
], ],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
@@ -121,17 +117,21 @@ def get_metadata_all(file: Path) -> dict[str, str]:
text=True, text=True,
check=True, check=True,
) )
data = json.loads(result.stdout)[0]
metadata = { metadata = {
str(k): str(v) str(k): str(v)
for k, v in json.loads(result.stdout) for k, v in data.items()
.get("format", {}) if not k.startswith(("File", "Directory", "SourceFile"))
.get("tags", {})
.items()
} }
except (OSError, subprocess.SubprocessError, json.JSONDecodeError) as error: except (OSError, subprocess.SubprocessError, json.JSONDecodeError) as error:
print(f"Error getting metadata for {file}: {error}") print(f"Error getting metadata for {file}: {error}")
for field in metadata_fields: for field in metadata_fields:
metadata.setdefault(field, "") metadata.setdefault(field, "")
return metadata return metadata
def get_metadata_fields(file, fields) -> dict: def get_metadata_fields(file, fields) -> dict:
@@ -170,30 +170,31 @@ def get_duration_ms(media: Path) -> int:
return int(float(duration) * 1000) return int(float(duration) * 1000)
def write_metadata(file, fields, values, dry_run=False): def write_metadata(file, fields, values, dry_run=False):
cmd_base = [ cmd = ["exiftool", "-overwrite_original"]
"kid3-cli",
"-c",
"tag 2"
]
for field, value in zip(fields, values): for field, value in zip(fields, values):
cmd_base.extend( ["-c", f'set "{field}" "{value}"']) cmd.append(f"-{field}={value}")
cmd_base.extend(["-c", "save", str(file)])
cmd.append(str(file))
try: try:
logger.debug(f"Writing metadata for {file}") logger.debug(f"Writing metadata for {file}")
logger.trace(f"Cmd: {" ".join(cmd_base)}") logger.trace(f"Cmd: {' '.join(cmd)}")
if dry_run: if dry_run:
return return
result = subprocess.run(
cmd_base, subprocess.run(
cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
text=True, text=True,
check=True, check=True,
) )
except (OSError, subprocess.SubprocessError) as error: except (OSError, subprocess.SubprocessError) as error:
print(f"Error writing metadata for {file}: {error}") print(f"Error writing metadata for {file}: {error}")
return return
return
def normalize_name(name: str,series: str) -> str: def normalize_name(name: str,series: str) -> str:
# Remove anything in (), [], {} # Remove anything in (), [], {}