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