Spaces:
Runtime error
Runtime error
File size: 21,726 Bytes
9aad47c 241b96d cf22b5c 9aad47c 241b96d 9aad47c f4df4a3 9aad47c e6e4692 9aad47c e6e4692 9aad47c d7e78df 9aad47c 21826ea f3831c6 f4df4a3 fe4a131 f4df4a3 fe4a131 f4df4a3 fe4a131 f4df4a3 fe4a131 f4df4a3 fe4a131 f4df4a3 fe4a131 f4df4a3 fe4a131 f4df4a3 21826ea f3831c6 fe4a131 f3831c6 21826ea f3831c6 21826ea f3831c6 fe4a131 21826ea fe4a131 21826ea f3831c6 9aad47c f3831c6 21826ea f3831c6 9aad47c f3831c6 9aad47c 21826ea f3831c6 21826ea f3831c6 21826ea f3831c6 9aad47c f3831c6 9aad47c f3831c6 9aad47c 241b96d cf22b5c f4df4a3 cf22b5c f4df4a3 cf22b5c f4df4a3 cf22b5c f4df4a3 cf22b5c f4df4a3 cf22b5c f4df4a3 cf22b5c f4df4a3 cf22b5c 241b96d f3831c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | 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) |