feat: 合成 MP4 时带上独立音频并支持按剧重新下载

This commit is contained in:
Nixevol
2026-10-21 22:06:50 +08:00
parent fe37a84971
commit e31e80384c
6 changed files with 647 additions and 25 deletions
+28 -15
View File
@@ -42,22 +42,22 @@ def convert_to_mp4(episode_dir: Path, mp4_path: Path) -> None:
exe = ensure_ffmpeg()
mp4_path.parent.mkdir(parents=True, exist_ok=True)
temporary = mp4_path.with_name(f"{mp4_path.stem}.part.mp4")
command = [
str(exe),
"-y",
"-allowed_extensions",
"ALL",
"-i",
"video.m3u8",
]
audio = episode_dir / "audio.m3u8"
if audio.is_file():
command.extend(["-i", "audio.m3u8", "-map", "0:v:0", "-map", "1:a:0"])
else:
command.extend(["-map", "0:v:0", "-map", "0:a?"])
command.extend(["-c", "copy", "-movflags", "+faststart", "-f", "mp4", str(temporary)])
completed = subprocess.run(
[
str(exe),
"-y",
"-allowed_extensions",
"ALL",
"-i",
"video.m3u8",
"-c",
"copy",
"-movflags",
"+faststart",
"-f",
"mp4",
str(temporary),
],
command,
cwd=episode_dir,
capture_output=True,
check=False,
@@ -66,9 +66,22 @@ def convert_to_mp4(episode_dir: Path, mp4_path: Path) -> None:
temporary.unlink(missing_ok=True)
detail = (completed.stderr or b"").decode("utf-8", "replace")[-400:]
raise RuntimeError("转换成 MP4 失败" + (f":{detail}" if detail else ""))
if not _output_has_audio(exe, temporary):
temporary.unlink(missing_ok=True)
raise RuntimeError("合成结果没有音轨。音频分片没有下载到,或者音频地址已经过期。")
temporary.replace(mp4_path)
def _output_has_audio(exe: Path, media: Path) -> bool:
completed = subprocess.run(
[str(exe), "-hide_banner", "-i", str(media)],
capture_output=True,
check=False,
)
text = (completed.stderr or b"").decode("utf-8", "replace")
return "Audio:" in text
def unused_zip_helper(path: Path) -> None:
if path.suffix == ".zip":
with zipfile.ZipFile(path) as archive: