Files
Kronic-Krusaders/ffmpeg_worker.py
T
2026-11-25 20:21:22 -06:00

60 lines
2.0 KiB
Python

import subprocess
from pathlib import Path
from library_index import update_transcoded_media
from transcode_planner import sanitize_filename
def safe_transcode_output(src_path, transcode_root):
src = Path(src_path)
dest = transcode_root / src.name
# If filename already ends with _dup.ext, don't double it
if dest.stem.endswith("_dup"):
base = dest.stem # keep the _dup
suffix = dest.suffix
dest_file = dest.parent / f"{base}{suffix}"
else:
dest_file = dest
# Check if file exists and append _dup if needed
while dest_file.exists():
dest_file = dest_file.with_name(dest_file.stem + "_dup" + dest_file.suffix)
return dest_file
def run_transcode(plan, transcode_root):
input_path = Path(plan.input_path)
output_name = sanitize_filename(input_path.name)
output_path = Path(transcode_root) / output_name
output_path.parent.mkdir(parents=True, exist_ok=True)
# Fixed scale filter: ensure dimensions are divisible by 2
ffmpeg_cmd = [
'ffmpeg', '-i', str(input_path),
'-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2', # Simplified and reliable
'-c:v', 'libx264', '-c:a', 'aac', str(output_path)
]
print(f"Running FFmpeg: {' '.join(ffmpeg_cmd)}")
try:
# Run FFmpeg with subprocess and capture output for debugging
result = subprocess.run(
ffmpeg_cmd, check=True, capture_output=True, text=True
)
print("FFmpeg output:", result.stdout)
print("FFmpeg error:", result.stderr)
except subprocess.CalledProcessError as e:
print(f"Error during transcoding: {e}")
print(f"FFmpeg failed with error code: {e.returncode}")
print(f"FFmpeg output: {e.output}")
print(f"FFmpeg stderr: {e.stderr}")
raise
update_transcoded_media({
"source_path": str(input_path),
"output_path": str(output_path),
"codec_video": plan.video_codec,
"codec_audio": plan.audio_codec,
"resolution": None
})