feat: refactor: extract.py to accept multiple input files and improve output directory handling
This commit is contained in:
1 parent
cd5a090ca5
commit
0ba475ac87
1 file changed
+52
-45
+52
-45
@@ -13,60 +13,67 @@ 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__)), "/home/honney/Videos/Drachenzähmen/Drachenzaehmen.mp4")
|
inputs = [
|
||||||
INPUT_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "/home/honney/Videos/Drachenzähmen/How to Train Your Dragon.mkv")
|
"/home/honney/Videos/Drachenzähmen/Drachenzaehmen.mp4",
|
||||||
|
"/home/honney/Videos/Drachenzähmen/How to Train Your Dragon.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)
|
||||||
|
|
||||||
# ─── Derive output filenames ───
|
for INPUT_FILE in INPUT_FILES:
|
||||||
stem = os.path.splitext(os.path.basename(INPUT_FILE))[0]
|
|
||||||
video_out = os.path.join(OUT_DIR, f"{stem}_video.mkv")
|
|
||||||
audio_out = os.path.join(OUT_DIR, f"{stem}_audio.mp3")
|
|
||||||
|
|
||||||
print(f"Input : {INPUT_FILE}")
|
# ─── Derive output filenames ───
|
||||||
print(f"Video → {video_out}")
|
stem = os.path.splitext(os.path.basename(INPUT_FILE))[0]
|
||||||
print(f"Audio → {audio_out}")
|
video_out = os.path.join(OUT_DIR, f"{stem}_video.mkv")
|
||||||
print("-" * 50)
|
audio_out = os.path.join(OUT_DIR, f"{stem}_audio.mp3")
|
||||||
|
|
||||||
# ─── Extract video-only (first 90s, no audio) ───
|
print(f"Input : {INPUT_FILE}")
|
||||||
cmd_video = [
|
print(f"Video → {video_out}")
|
||||||
"ffmpeg",
|
print(f"Audio → {audio_out}")
|
||||||
"-y", # overwrite output
|
print(f"-" * 50)
|
||||||
"-i", INPUT_FILE,
|
|
||||||
"-t", str(DURATION),
|
|
||||||
"-an", # no audio
|
|
||||||
"-c:v", "copy", # copy video stream (no re-encode = fast + lossless)
|
|
||||||
video_out
|
|
||||||
]
|
|
||||||
print("[video] running ffmpeg…")
|
|
||||||
r = subprocess.run(cmd_video, capture_output=True, text=True)
|
|
||||||
if r.returncode != 0:
|
|
||||||
sys.exit(f"ffmpeg (video) failed:\n{r.stderr}")
|
|
||||||
print("[video] done ✔")
|
|
||||||
|
|
||||||
# ─── Extract audio-only (first 90s, no video) ───
|
# ─── Extract video-only (first 90s, no audio) ───
|
||||||
cmd_audio = [
|
cmd_video = [
|
||||||
"ffmpeg",
|
"ffmpeg",
|
||||||
"-y",
|
"-y", # overwrite output
|
||||||
"-i", INPUT_FILE,
|
"-i", INPUT_FILE,
|
||||||
"-t", str(DURATION),
|
"-t", str(DURATION),
|
||||||
"-vn", # no video
|
"-an", # no audio
|
||||||
"-c:a", "libmp3lame",
|
"-c:v", "copy", # copy video stream (no re-encode = fast + lossless)
|
||||||
"-b:a", "192k", # 192 kbps
|
video_out
|
||||||
audio_out
|
]
|
||||||
]
|
print(f"[video: {stem}] running ffmpeg…")
|
||||||
print("[audio] running ffmpeg…")
|
r = subprocess.run(cmd_video, 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 (video) failed:\n{r.stderr}")
|
||||||
sys.exit(f"ffmpeg (audio) failed:\n{r.stderr}")
|
print(f"[video: {stem}] done ✔")
|
||||||
print("[audio] done ✔")
|
|
||||||
|
|
||||||
print("-" * 50)
|
# ─── Extract audio-only (first 90s, no video) ───
|
||||||
print("All done. Files are in:", OUT_DIR)
|
cmd_audio = [
|
||||||
|
"ffmpeg",
|
||||||
|
"-y",
|
||||||
|
"-i", INPUT_FILE,
|
||||||
|
"-t", str(DURATION),
|
||||||
|
"-vn", # no video
|
||||||
|
"-c:a", "libmp3lame",
|
||||||
|
"-b:a", "192k", # 192 kbps
|
||||||
|
audio_out
|
||||||
|
]
|
||||||
|
print(f"[audio: {stem}] running ffmpeg…")
|
||||||
|
r = subprocess.run(cmd_audio, capture_output=True, text=True)
|
||||||
|
if r.returncode != 0:
|
||||||
|
sys.exit(f"ffmpeg (audio) failed:\n{r.stderr}")
|
||||||
|
print(f"[audio: {stem}] done ✔")
|
||||||
|
|
||||||
|
print(f"-" * 50)
|
||||||
|
print(f"All done. Files are in:", OUT_DIR)
|
||||||
Reference in new issue
Block a user