fix(tracks): sniff attachment MIME from file bytes and keep ffmpeg autobsf on repack

This commit is contained in:
imSp4rky
2026-12-02 15:24:00 -07:00
parent 4afc4f7a04
commit 3868928d98
7 changed files with 179 additions and 58 deletions
+11 -17
View File
@@ -1162,10 +1162,11 @@ class Track:
A given ``bsf_v`` goes into the same pass as ``-bsf:v``, which normalises video VUI
colour metadata without a second full-file remux. Repackaging is mandatory. The
bitstream filter is best-effort: if the combined pass fails, and it is not the
AAC-retry case, unshackle tries it again once without ``bsf_v`` so the remux still
succeeds. Returns True if unshackle applied the requested ``bsf_v`` (always False
when ``bsf_v`` is None, because the caller requested nothing).
bitstream filter is best-effort: if the combined pass fails, unshackle tries it again
once without ``bsf_v`` so the remux still succeeds, and the caller falls back to
``normalize_vui``. Returns True if unshackle applied
the requested ``bsf_v`` (always False when ``bsf_v`` is None, because the caller
requested nothing).
"""
if not self.path or not self.path.exists():
raise ValueError("Cannot repackage a Track that has not been downloaded.")
@@ -1177,7 +1178,7 @@ class Track:
original_path = self.path
output_path = original_path.with_stem(f"{original_path.stem}_repack")
def ffmpeg(extra_args: list[str] | None = None, bsf: str | None = None) -> None:
def ffmpeg(bsf: str | None = None) -> None:
args: list[str | Path] = [
ffmpeg_bin,
"-nostdin",
@@ -1186,7 +1187,6 @@ class Track:
"error",
"-i",
original_path,
*(extra_args or []),
]
if hasattr(self, "data") and self.data.get("audio_language"):
@@ -1208,7 +1208,7 @@ class Track:
"-map_metadata",
"-1",
"-fflags",
"bitexact",
"+bitexact",
"-codec",
"copy",
"-strict",
@@ -1233,17 +1233,11 @@ class Track:
try:
ffmpeg(bsf=bsf_v)
bsf_applied = bsf_v is not None
except subprocess.CalledProcessError as e:
if b"Malformed AAC bitstream detected" in e.stderr:
ffmpeg(["-y", "-bsf:a", "aac_adtstoasc"], bsf=bsf_v)
bsf_applied = bsf_v is not None
elif bsf_v is not None:
# Repack is mandatory, the VUI bitstream filter is best-effort: retry
# without it so the remux still succeeds; caller falls back to normalize_vui.
output_path.unlink(missing_ok=True)
ffmpeg()
else:
except subprocess.CalledProcessError:
if bsf_v is None:
raise
output_path.unlink(missing_ok=True)
ffmpeg()
original_path.unlink()
self.path = output_path