update refactoring and add new features
This commit is contained in:
+5
-90
@@ -523,8 +523,8 @@ def _read_sidecar_metadata(
|
||||
lower = line.lower()
|
||||
if lower.startswith("hash:"):
|
||||
hash_value = line.split(":", 1)[1].strip() if ":" in line else ""
|
||||
elif lower.startswith("url:") or lower.startswith("url:"):
|
||||
# Parse url (handle legacy 'url:' format)
|
||||
elif lower.startswith("url:"):
|
||||
# Parse url
|
||||
url_part = line.split(":", 1)[1].strip() if ":" in line else ""
|
||||
if url_part:
|
||||
for url_segment in url_part.split(","):
|
||||
@@ -638,44 +638,7 @@ def write_tags(
|
||||
return
|
||||
except Exception as e:
|
||||
debug(f"Failed to add tags to database: {e}", file=sys.stderr)
|
||||
# Fall through to sidecar creation as fallback
|
||||
|
||||
# Create sidecar path
|
||||
try:
|
||||
sidecar = media_path.parent / (media_path.name + ".tag")
|
||||
except Exception:
|
||||
sidecar = media_path.with_name(media_path.name + ".tag")
|
||||
|
||||
# Handle edge case: empty/invalid base name
|
||||
try:
|
||||
if not sidecar.stem or sidecar.name in {".tag",
|
||||
"-.tag",
|
||||
"_.tag"}:
|
||||
fallback_base = (
|
||||
media_path.stem
|
||||
or _sanitize_title_for_filename(extract_title(tag_list) or "")
|
||||
or "untitled"
|
||||
)
|
||||
sidecar = media_path.parent / f"{fallback_base}.tag"
|
||||
except Exception:
|
||||
logger.exception("Failed to determine fallback .tag sidecar base for %s", media_path)
|
||||
|
||||
# Write via consolidated function
|
||||
try:
|
||||
lines: List[str] = []
|
||||
lines.extend(str(tag).strip().lower() for tag in tag_list if str(tag).strip())
|
||||
|
||||
if lines:
|
||||
sidecar.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||||
if emit_debug:
|
||||
debug(f"Tags: {sidecar}")
|
||||
else:
|
||||
try:
|
||||
sidecar.unlink()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
except OSError as exc:
|
||||
debug(f"Failed to write tag sidecar {sidecar}: {exc}", file=sys.stderr)
|
||||
return
|
||||
|
||||
|
||||
def write_metadata(
|
||||
@@ -729,45 +692,7 @@ def write_metadata(
|
||||
return
|
||||
except Exception as e:
|
||||
debug(f"Failed to add metadata to database: {e}", file=sys.stderr)
|
||||
# Fall through to sidecar creation as fallback
|
||||
|
||||
# Create sidecar path
|
||||
try:
|
||||
sidecar = media_path.parent / (media_path.name + ".metadata")
|
||||
except Exception:
|
||||
sidecar = media_path.with_name(media_path.name + ".metadata")
|
||||
|
||||
try:
|
||||
lines = []
|
||||
|
||||
# Add hash if available
|
||||
if hash_value:
|
||||
lines.append(f"hash:{hash_value}")
|
||||
|
||||
# Add known url
|
||||
for url in url_list:
|
||||
if str(url).strip():
|
||||
clean = str(url).strip()
|
||||
lines.append(f"url:{clean}")
|
||||
|
||||
# Add relationships
|
||||
for rel in rel_list:
|
||||
if str(rel).strip():
|
||||
lines.append(f"relationship:{str(rel).strip()}")
|
||||
|
||||
# Write metadata file
|
||||
if lines:
|
||||
sidecar.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||||
if emit_debug:
|
||||
debug(f"Wrote metadata to {sidecar}")
|
||||
else:
|
||||
# Remove if no content
|
||||
try:
|
||||
sidecar.unlink()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
except OSError as exc:
|
||||
debug(f"Failed to write metadata sidecar {sidecar}: {exc}", file=sys.stderr)
|
||||
return
|
||||
|
||||
|
||||
def extract_title(tags: Iterable[str]) -> Optional[str]:
|
||||
@@ -2864,17 +2789,7 @@ def build_ffmpeg_command(
|
||||
"192k",
|
||||
])
|
||||
cmd.extend(["-f", "opus"])
|
||||
elif fmt == "audio":
|
||||
# Legacy format name for mp3
|
||||
cmd.extend([
|
||||
"-vn",
|
||||
"-c:a",
|
||||
"libmp3lame",
|
||||
"-b:a",
|
||||
"192k",
|
||||
])
|
||||
cmd.extend(["-f", "mp3"])
|
||||
elif fmt != "copy":
|
||||
if fmt not in ("mp4", "webm", "mp3", "flac", "wav", "aac", "m4a", "ogg", "opus", "copy"):
|
||||
raise ValueError(f"Unsupported format: {fmt}")
|
||||
|
||||
cmd.append(str(output_path))
|
||||
|
||||
Reference in New Issue
Block a user