frameforge-v1 / ffmpeg_runner.py
kajalan's picture
Update ffmpeg_runner.py (#13)
fe4a131
Raw History Blame Contribute Delete
21.7 kB
import asyncio
import json
import re
import shlex
import subprocess
from pathlib import Path
from typing import Callable, Optional
import httpx
import aiofiles
from urllib.parse import urlparse
import os
from models import FFmpegCommandRequest
from progress import ProgressSnapshot
# ── MIME β†’ file extension map ─────────────────────────────────────────────────
_MIME_TO_EXT: dict[str, str] = {
"image/jpeg": ".jpg",
"image/jpg": ".jpg",
"image/png": ".png",
"image/webp": ".webp",
"image/gif": ".gif",
"image/bmp": ".bmp",
"image/tiff": ".tiff",
"image/svg+xml": ".svg",
"video/mp4": ".mp4",
"video/webm": ".webm",
"video/quicktime": ".mov",
"video/x-msvideo": ".avi",
"video/x-matroska": ".mkv",
"video/mpeg": ".mpeg",
"audio/mpeg": ".mp3",
"audio/mp3": ".mp3",
"audio/wav": ".wav",
"audio/x-wav": ".wav",
"audio/aac": ".aac",
"audio/ogg": ".ogg",
"audio/flac": ".flac",
"audio/webm": ".webm",
}
def _ext_from_content_type(content_type: str) -> str:
"""Return file extension (e.g. '.jpg') from a Content-Type header, or ''."""
ct = content_type.split(";")[0].strip().lower()
return _MIME_TO_EXT.get(ct, "")
def _ext_from_content_disposition(cd: str) -> str:
"""Return file extension from Content-Disposition filename, or ''."""
m = re.search(r'filename[^;=\n]*=\s*["\']?([^"\';\n]+)["\']?', cd)
if m:
_, ext = os.path.splitext(m.group(1).strip())
return ext
return ""
# ── Security: block dangerous shell patterns ──────────────────────────────────
# NOTE: \>\> matches literal >> (append redirection), not >= (comparison)
_BLOCKED_RE = re.compile(
r"""(
\brm\b | \bmv\b | \bcp\b | \bchmod\b | \bchown\b |
\bkill\b | \bsudo\b | \bsu\b | \bwget\b | \bcurl\b |
\bpython | \bbash\b | \bsh\b | \benv\b | \bexec\b |
; | && | \|\| | \` | \$\( |
\bdd\b | /dev/ | \beval\b | \bnc\b |
\>\> | 2> | >\s*/
)""",
re.VERBOSE | re.IGNORECASE,
)
MAX_FILE_SIZE_BYTES = 500 * 1024 * 1024 # 500 MB
def sanitize_ffmpeg_args(args: str) -> str:
"""Raise ValueError if args contain blocked shell patterns outside of quotes."""
# Strip double-quoted and single-quoted strings before checking.
# This allows FFmpeg filter_complex to use ; and other shell-like
# characters inside quoted filter expressions.
stripped = re.sub(r'"[^"]*"', '""', args)
stripped = re.sub(r"'[^']*'", "''", stripped)
if _BLOCKED_RE.search(stripped):
raise ValueError(
"Blocked pattern detected in ffmpeg_args. "
"Shell operators, file deletion, and system commands are not allowed."
)
return args
async def run_ffmpeg_from_url(
request: FFmpegCommandRequest,
work_dir: Path,
) -> Path:
"""Download input from URL, run FFmpeg, return output path."""
sanitize_ffmpeg_args(request.ffmpeg_args)
output_name = request.output_filename or f"output.{request.output_format}"
output_path = work_dir / output_name
cmd = [
"ffmpeg", "-y",
"-i", request.input_url,
*shlex.split(request.ffmpeg_args),
str(output_path),
]
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=str(work_dir),
)
try:
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=300)
except asyncio.TimeoutError:
proc.kill()
raise RuntimeError("FFmpeg job timed out after 5 minutes.")
if proc.returncode != 0:
err = stderr.decode(errors="replace")[-2000:] # Last 2000 chars of stderr
raise RuntimeError(f"FFmpeg exited with code {proc.returncode}:\n{err}")
if not output_path.exists():
raise RuntimeError("FFmpeg completed but output file was not created.")
return output_path
async def run_ffmpeg_from_file(
ffmpeg_args: str,
input_path: Path,
output_format: str,
work_dir: Path,
output_filename: Optional[str] = None,
) -> Path:
"""Run FFmpeg on a local uploaded file."""
sanitize_ffmpeg_args(ffmpeg_args)
output_name = output_filename or f"output.{output_format}"
output_path = work_dir / output_name
cmd = [
"ffmpeg", "-y",
"-i", str(input_path),
*shlex.split(ffmpeg_args),
str(output_path),
]
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=str(work_dir),
)
try:
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=300)
except asyncio.TimeoutError:
proc.kill()
raise RuntimeError("FFmpeg job timed out after 5 minutes.")
if proc.returncode != 0:
err = stderr.decode(errors="replace")[-2000:]
raise RuntimeError(f"FFmpeg exited with code {proc.returncode}:\n{err}")
if not output_path.exists():
raise RuntimeError("FFmpeg completed but output file was not created.")
return output_path
def _extract_gdrive_id(url: str) -> Optional[str]:
"""Extract file ID from any Google Drive URL format."""
import re as _re
m = _re.search(r'/file/d/([a-zA-Z0-9_\-]+)', url)
if m:
return m.group(1)
m = _re.search(r'[?&]id=([a-zA-Z0-9_\-]+)', url)
if m:
return m.group(1)
return None
def _is_gdrive_url(url: str) -> bool:
return any(h in url for h in (
"drive.google.com",
"drive.usercontent.google.com",
"docs.google.com",
))
def _gdrive_download_keyed(url: str, dest_dir: Path, key: str) -> Path:
"""
Download a GDrive URL using gdown, renamed to `key + original_ext`.
Uses gdown's id= parameter (most reliable path for large files with virus-scan warning).
"""
import gdown
file_id = _extract_gdrive_id(url)
if not file_id:
raise RuntimeError(
f"Could not extract a Google Drive file ID from URL: {url!r}. "
"Expected a /file/d/ID or ?id=ID format."
)
# Use id= kwarg β€” bypasses the virus-scan confirmation page reliably in gdown 4.x+
output = gdown.download(
id=file_id,
output=str(dest_dir) + "/",
quiet=True,
fuzzy=True,
use_cookies=False, # avoids stale cookie issues in server environments
)
if not output:
raise RuntimeError(
"gdown failed to download the Google Drive file. "
"Ensure it is publicly shared (Anyone with the link β†’ Viewer)."
)
downloaded = Path(output)
# Guard: gdown sometimes saves the HTML warning page instead of the file
if not downloaded.exists() or downloaded.stat().st_size == 0:
downloaded.unlink(missing_ok=True)
raise RuntimeError("gdown returned an empty file β€” check Drive sharing permissions.")
if downloaded.stat().st_size < 1024:
# Likely got the HTML "confirm download" page
content = downloaded.read_bytes()
downloaded.unlink(missing_ok=True)
if b"<html" in content.lower() or b"<!doctype" in content.lower():
raise RuntimeError(
"gdown received an HTML page instead of the media file. "
"The file may not be publicly shared, or it requires sign-in. "
"Set sharing to 'Anyone with the link β†’ Viewer'."
)
# Rename to key + extension so {{placeholder}} resolves correctly
ext = downloaded.suffix or ".mp4"
final_path = dest_dir / f"{key}{ext}"
if downloaded != final_path:
downloaded.rename(final_path)
return final_path
def _gdrive_download_sync(url: str, dest_dir: Path) -> Path:
"""
Download a GDrive URL to a temp file using gdown (used by ffprobe).
Uses id= parameter to bypass virus-scan confirmation for large files.
"""
import tempfile
import gdown
file_id = _extract_gdrive_id(url)
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".tmp", dir=str(dest_dir))
tmp.close()
tmp_path = Path(tmp.name)
if file_id:
result = gdown.download(
id=file_id,
output=str(tmp_path),
quiet=True,
fuzzy=True,
use_cookies=False,
)
else:
result = gdown.download(url, str(tmp_path), quiet=True, fuzzy=True, use_cookies=False)
if not result or not tmp_path.exists() or tmp_path.stat().st_size == 0:
tmp_path.unlink(missing_ok=True)
raise RuntimeError(
"gdown failed to download the file. "
"Ensure it is publicly shared (Anyone with the link β†’ Viewer)."
)
return tmp_path
async def run_ffprobe(input_url: str) -> dict:
"""
Return full ffprobe JSON for a URL or local path.
Google Drive URLs are pre-downloaded via gdown before probing.
"""
from config import TEMP_DIR
tmp_path: Optional[Path] = None
probe_target = input_url
try:
if _is_gdrive_url(input_url):
loop = asyncio.get_event_loop()
tmp_path = await loop.run_in_executor(
None, _gdrive_download_sync, input_url, TEMP_DIR
)
probe_target = str(tmp_path)
cmd = [
"ffprobe",
"-v", "error",
"-print_format", "json",
"-show_streams",
"-show_format",
"-show_error",
probe_target,
]
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
try:
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=120)
except asyncio.TimeoutError:
proc.kill()
raise RuntimeError("ffprobe timed out after 120s.")
stdout_str = stdout.decode(errors="replace").strip()
stderr_str = stderr.decode(errors="replace").strip()
if proc.returncode != 0 or not stdout_str:
raise RuntimeError(
f"ffprobe failed (exit {proc.returncode}): "
f"{stderr_str or 'no error output'}"
)
return json.loads(stdout_str)
finally:
if tmp_path and tmp_path.exists():
try:
tmp_path.unlink()
except Exception:
pass
def get_ffmpeg_version() -> str:
"""Get installed FFmpeg version string."""
try:
result = subprocess.run(
["ffmpeg", "-version"],
capture_output=True, text=True, timeout=5
)
first_line = result.stdout.splitlines()[0]
return first_line # e.g. "ffmpeg version 6.0 ..."
except Exception:
return "unknown"
# ── Progress parsing ──────────────────────────────────────────────────────────
_TIME_RE = re.compile(r"time=(\d+):(\d+):(\d+)\.(\d+)")
_SPEED_RE = re.compile(r"speed=\s*([\d.]+)x")
_FPS_RE = re.compile(r"fps=\s*([\d.]+)")
_BITRATE_RE = re.compile(r"bitrate=\s*([\d.]+)kb/s")
_FRAME_RE = re.compile(r"frame=\s*(\d+)")
def _parse_progress(line: str, duration_s: float = 0) -> ProgressSnapshot:
snap = ProgressSnapshot()
if m := _FRAME_RE.search(line):
snap.frame = int(m.group(1))
if m := _TIME_RE.search(line):
h, mi, s, ms = int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))
snap.time_elapsed = h * 3600 + mi * 60 + s + ms / 100
if duration_s > 0:
snap.percent = min(100.0, round(snap.time_elapsed / duration_s * 100, 1))
if m := _SPEED_RE.search(line):
snap.speed = m.group(1)
if m := _FPS_RE.search(line):
snap.fps = m.group(1)
if m := _BITRATE_RE.search(line):
snap.bitrate = m.group(1)
return snap
async def _ffmpeg_progress(
cmd: list[str],
work_dir: Path,
progress_cb: Optional[Callable[[ProgressSnapshot], None]] = None,
duration_s: float = 0,
timeout: int = 300,
) -> Path:
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=str(work_dir),
)
stderr_lines: list[bytes] = []
assert proc.stderr is not None
while True:
line = await proc.stderr.readline()
if not line:
break
stderr_lines.append(line)
text = line.decode(errors="replace").strip()
if progress_cb and ("time=" in text or "frame=" in text):
progress_cb(_parse_progress(text, duration_s))
await proc.wait()
if proc.returncode != 0:
err = b"".join(stderr_lines).decode(errors="replace")[-2000:]
raise RuntimeError(f"FFmpeg exited with code {proc.returncode}:\n{err}")
output_path = cmd[-1]
p = Path(output_path)
if not p.exists():
raise RuntimeError("FFmpeg completed but output file was not created.")
return p
# ── Public API (aliases matching main.py expectations) ────────────────────────
async def probe(input_url: str) -> dict:
return await run_ffprobe(input_url)
async def run_from_url(
request: FFmpegCommandRequest,
work_dir: Path,
job_id: str = "",
progress_cb: Optional[Callable[[ProgressSnapshot], None]] = None,
) -> Path:
sanitize_ffmpeg_args(request.ffmpeg_args)
output_name = request.output_filename or f"output.{request.output_format}"
output_path = work_dir / output_name
cmd = [
"ffmpeg", "-y",
"-i", request.input_url,
*shlex.split(request.ffmpeg_args),
str(output_path),
]
return await _ffmpeg_progress(cmd, work_dir, progress_cb)
async def run_from_url_multi(
request: FFmpegCommandRequest,
extra_urls: list[str],
work_dir: Path,
job_id: str = "",
progress_cb: Optional[Callable[[ProgressSnapshot], None]] = None,
) -> Path:
sanitize_ffmpeg_args(request.ffmpeg_args)
output_name = request.output_filename or f"output.{request.output_format}"
output_path = work_dir / output_name
cmd = ["ffmpeg", "-y"]
cmd += ["-i", request.input_url]
for url in extra_urls:
cmd += ["-i", url]
cmd += shlex.split(request.ffmpeg_args)
cmd.append(str(output_path))
return await _ffmpeg_progress(cmd, work_dir, progress_cb)
async def download_input_files(input_files: dict[str, str], work_dir: Path) -> dict[str, Path]:
"""
Downloads all input files in parallel to the work directory.
Extension is resolved in priority order:
1. URL path extension (e.g. /file/video.mp4)
2. Content-Disposition filename from response headers
3. Content-Type response header (e.g. image/png β†’ .png)
4. Google Drive β€” handled via gdown (preserves Drive filename extension)
Returns a mapping of reference key β†’ local Path.
"""
downloaded_paths: dict[str, Path] = {}
async def download_one(key: str, url: str) -> None:
# ── Google Drive: delegate to gdown in a thread ───────────────────────
if _is_gdrive_url(url):
loop = asyncio.get_event_loop()
path = await loop.run_in_executor(
None, _gdrive_download_keyed, url, work_dir, key
)
downloaded_paths[key] = path
return
# ── Regular HTTP download ─────────────────────────────────────────────
parsed = urlparse(url)
_, url_ext = os.path.splitext(parsed.path)
async with httpx.AsyncClient(follow_redirects=True, timeout=60.0) as client:
async with client.stream("GET", url) as response:
if response.status_code != 200:
raise RuntimeError(
f"Failed to download {key} from {url}: HTTP {response.status_code}"
)
# Resolve extension: URL β†’ Content-Disposition β†’ Content-Type
ext = url_ext
if not ext:
ext = _ext_from_content_disposition(
response.headers.get("content-disposition", "")
)
if not ext:
ext = _ext_from_content_type(
response.headers.get("content-type", "")
)
if not ext:
ext = ".bin" # last resort β€” FFmpeg will likely fail; logged below
if ext == ".bin":
print(
f"[download_input_files] WARNING: could not detect extension for key={key!r} "
f"url={url!r}. Content-Type={response.headers.get('content-type')!r}. "
"FFmpeg may fail β€” ensure the URL points directly to a media file."
)
dest_path = work_dir / f"{key}{ext}"
async with aiofiles.open(dest_path, "wb") as f:
async for chunk in response.aiter_bytes(chunk_size=8192):
await f.write(chunk)
downloaded_paths[key] = dest_path
# Run all downloads in parallel
tasks = [download_one(key, url) for key, url in input_files.items()]
await asyncio.gather(*tasks)
return downloaded_paths
def prepare_rendi_command(
ffmpeg_command: str,
downloaded_inputs: dict[str, Path],
output_files: dict[str, str],
work_dir: Path
) -> tuple[list[str], dict[str, Path]]:
"""
Replaces placeholders in ffmpeg_command and returns the command list and output paths.
"""
cmd_str = ffmpeg_command
# Replace inputs
for key, local_path in downloaded_inputs.items():
placeholder = f"{{{{{key}}}}}"
# Use forward slashes for FFmpeg compatibility on Windows
cmd_str = cmd_str.replace(placeholder, str(local_path).replace("\\", "/"))
# Replace outputs
resolved_outputs = {}
for key, filename in output_files.items():
placeholder = f"{{{{{key}}}}}"
out_path = work_dir / filename
# Use forward slashes for FFmpeg compatibility on Windows
cmd_str = cmd_str.replace(placeholder, str(out_path).replace("\\", "/"))
resolved_outputs[key] = out_path
# Split the command string into a list of arguments
args = shlex.split(cmd_str)
if args and args[0] == "ffmpeg":
cmd = args
# Ensure -y is present
if "-y" not in cmd:
cmd.insert(1, "-y")
else:
cmd = ["ffmpeg", "-y"] + args
return cmd, resolved_outputs
async def run_rendi_command(
input_files: dict[str, str],
output_files: dict[str, str],
ffmpeg_command: str,
work_dir: Path,
job_id: str = "",
progress_cb: Optional[Callable[[ProgressSnapshot], None]] = None,
) -> dict[str, str]:
"""
Downloads input files, replaces placeholders, runs FFmpeg with progress tracking,
and returns a dictionary of output reference keys to their local file paths.
"""
# 1. Sanitize command
sanitize_ffmpeg_args(ffmpeg_command)
# 2. Download input files in parallel
downloaded_inputs = await download_input_files(input_files, work_dir)
# 3. Prepare command and resolve outputs
cmd, resolved_outputs = prepare_rendi_command(
ffmpeg_command, downloaded_inputs, output_files, work_dir
)
# 4. Run FFmpeg with progress
await _ffmpeg_progress(cmd, work_dir, progress_cb)
# 5. Verify all outputs exist
for key, out_path in resolved_outputs.items():
if not out_path.exists():
raise RuntimeError(f"FFmpeg completed but output file '{key}' ({out_path.name}) was not created.")
# Return mapping of output keys to their filenames
return {key: out_path.name for key, out_path in resolved_outputs.items()}
async def run_from_file(
ffmpeg_args: str,
input_path: Path,
output_format: str,
work_dir: Path,
job_id: str = "",
progress_cb: Optional[Callable[[ProgressSnapshot], None]] = None,
output_filename: Optional[str] = None,
) -> Path:
sanitize_ffmpeg_args(ffmpeg_args)
output_name = output_filename or f"output.{output_format}"
output_path = work_dir / output_name
cmd = [
"ffmpeg", "-y",
"-i", str(input_path),
*shlex.split(ffmpeg_args),
str(output_path),
]
return await _ffmpeg_progress(cmd, work_dir, progress_cb)
async def run_from_file_multi(
ffmpeg_args: str,
input_path: Path,
extra_paths: list[Path],
output_format: str,
work_dir: Path,
job_id: str = "",
progress_cb: Optional[Callable[[ProgressSnapshot], None]] = None,
output_filename: Optional[str] = None,
) -> Path:
sanitize_ffmpeg_args(ffmpeg_args)
output_name = output_filename or f"output.{output_format}"
output_path = work_dir / output_name
cmd = ["ffmpeg", "-y"]
cmd += ["-i", str(input_path)]
for p in extra_paths:
cmd += ["-i", str(p)]
cmd += shlex.split(ffmpeg_args)
cmd.append(str(output_path))
return await _ffmpeg_progress(cmd, work_dir, progress_cb)