feat: refactor: extract.py to accept multiple input files and improve output directory handling

This commit is contained in:
Hannes committed 2026-09-13 21:23:15 +02:00
1 parent db4531dd24
commit 2bfe696f2d
1 file changed
+18 -11
+18 -11
View File
@@ -13,18 +13,25 @@ import sys
import shutil import shutil
# ─── Hardcoded input (relative to this script's directory) ─── # ─── Hardcoded input (relative to this script's directory) ───
# INPUT_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "/path/to/movie_b.mp4") inputs = [
INPUT_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "/path/to/movie_a.mkv") "/path/to/movie_b.mp4",
"/path/to/movie_a.mkv"
]
INPUT_FILES = []
for input_file in inputs:
INPUT_FILES.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), input_file))
DURATION = 90 # 1.5 minutes in seconds DURATION = 90 # 1.5 minutes in seconds
OUT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "extracted") OUT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "extracted")
# ─── Check ffmpeg ─── # ─── Check ffmpeg ───
if shutil.which("ffmpeg") is None: if shutil.which(f"ffmpeg") is None:
sys.exit("ERROR: 'ffmpeg' not found on PATH. Install it and try again.") sys.exit(f"ERROR: 'ffmpeg' not found on PATH. Install it and try again.")
# ─── Prepare output folder ─── # ─── Prepare output folder ───
os.makedirs(OUT_DIR, exist_ok=True) os.makedirs(OUT_DIR, exist_ok=True)
for INPUT_FILE in INPUT_FILES:
# ─── Derive output filenames ─── # ─── Derive output filenames ───
stem = os.path.splitext(os.path.basename(INPUT_FILE))[0] stem = os.path.splitext(os.path.basename(INPUT_FILE))[0]
video_out = os.path.join(OUT_DIR, f"{stem}_video.mkv") video_out = os.path.join(OUT_DIR, f"{stem}_video.mkv")
@@ -33,7 +40,7 @@ audio_out = os.path.join(OUT_DIR, f"{stem}_audio.mp3")
print(f"Input : {INPUT_FILE}") print(f"Input : {INPUT_FILE}")
print(f"Video → {video_out}") print(f"Video → {video_out}")
print(f"Audio → {audio_out}") print(f"Audio → {audio_out}")
print("-" * 50) print(f"-" * 50)
# ─── Extract video-only (first 90s, no audio) ─── # ─── Extract video-only (first 90s, no audio) ───
cmd_video = [ cmd_video = [
@@ -45,11 +52,11 @@ cmd_video = [
"-c:v", "copy", # copy video stream (no re-encode = fast + lossless) "-c:v", "copy", # copy video stream (no re-encode = fast + lossless)
video_out video_out
] ]
print("[video] running ffmpeg…") print(f"[video: {stem}] running ffmpeg…")
r = subprocess.run(cmd_video, capture_output=True, text=True) r = subprocess.run(cmd_video, capture_output=True, text=True)
if r.returncode != 0: if r.returncode != 0:
sys.exit(f"ffmpeg (video) failed:\n{r.stderr}") sys.exit(f"ffmpeg (video) failed:\n{r.stderr}")
print("[video] done ✔") print(f"[video: {stem}] done ✔")
# ─── Extract audio-only (first 90s, no video) ─── # ─── Extract audio-only (first 90s, no video) ───
cmd_audio = [ cmd_audio = [
@@ -62,11 +69,11 @@ cmd_audio = [
"-b:a", "192k", # 192 kbps "-b:a", "192k", # 192 kbps
audio_out audio_out
] ]
print("[audio] running ffmpeg…") print(f"[audio: {stem}] running ffmpeg…")
r = subprocess.run(cmd_audio, capture_output=True, text=True) r = subprocess.run(cmd_audio, capture_output=True, text=True)
if r.returncode != 0: if r.returncode != 0:
sys.exit(f"ffmpeg (audio) failed:\n{r.stderr}") sys.exit(f"ffmpeg (audio) failed:\n{r.stderr}")
print("[audio] done ✔") print(f"[audio: {stem}] done ✔")
print("-" * 50) print(f"-" * 50)
print("All done. Files are in:", OUT_DIR) print(f"All done. Files are in:", OUT_DIR)