81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
import subprocess
|
|
from typing import Optional, List
|
|
from pathlib import Path
|
|
import shutil
|
|
|
|
def run(cmd: subprocess._CMD) -> subprocess.CompletedProcess[str]:
|
|
"""
|
|
Run a subprocess command.
|
|
|
|
Args:
|
|
cmd: Command and arguments as a list.
|
|
|
|
Raises:
|
|
subprocess.CalledProcessError: If the command exits with a non-zero status.
|
|
"""
|
|
try:
|
|
result = subprocess.run(
|
|
cmd,
|
|
check=True,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True
|
|
)
|
|
return result
|
|
except subprocess.CalledProcessError as e:
|
|
print(e.stdout)
|
|
raise
|
|
|
|
# def stick_together_audio(left_audio: Path, right_audio: Path, output_audio: Path) -> None:
|
|
# """
|
|
# Concatenate two audio files so that left_audio plays directly
|
|
# before right_audio in the output file.
|
|
# """
|
|
|
|
# left_audio = Path(left_audio)
|
|
# right_audio = Path(right_audio)
|
|
# output_audio = Path(output_audio)
|
|
|
|
# # Use stem instead of prefix to create a temporary file
|
|
# output_audio_tmp = output_audio.parent / (output_audio.stem + "_temp" + output_audio.suffix)
|
|
|
|
# run([
|
|
# "ffmpeg",
|
|
# "-y",
|
|
# "-i", str(left_audio),
|
|
# "-i", str(right_audio),
|
|
# "-filter_complex", "[0:a][1:a]concat=n=2:v=0:a=1[a]",
|
|
# "-map", "[a]",
|
|
# "-c:a", "opus", # or "aac" if you prefer
|
|
# "-strict", "-2",
|
|
# str(output_audio_tmp)
|
|
# ])
|
|
|
|
# shutil.move(output_audio_tmp, output_audio)
|
|
import tempfile
|
|
|
|
def stick_together_audio(left_audio: Path, right_audio: Path, output_audio: Path) -> None:
|
|
left_audio = Path(left_audio)
|
|
right_audio = Path(right_audio)
|
|
output_audio = Path(output_audio)
|
|
|
|
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".txt") as f:
|
|
f.write(f"file '{left_audio.resolve()}'\n")
|
|
f.write(f"file '{right_audio.resolve()}'\n")
|
|
list_file = f.name
|
|
|
|
run([
|
|
"ffmpeg",
|
|
"-y",
|
|
"-f", "concat",
|
|
"-safe", "0",
|
|
"-i", list_file,
|
|
"-c", "copy", # no re-encode
|
|
str(output_audio)
|
|
])
|
|
|
|
file1 = Path("/home/honney/mount/Media/tmp/serien/ready/Clannad (2007) [tmdb-24835]/Season 02/Clannad Season 02 E01_german_audio.mka")
|
|
file2 = Path("/home/honney/mount/Media/tmp/serien/ready/Clannad (2007) [tmdb-24835]/Season 02/Clannad Season 02 E01_original_audio2.mka")
|
|
out = Path("/home/honney/mount/Media/tmp/serien/ready/Clannad (2007) [tmdb-24835]/Season 02/Clannad Season 02 E01_german_audio2.mka")
|
|
|
|
stick_together_audio(file1, file2, out) |