| import os |
| import subprocess |
| import uuid |
| from utils.logger import logger |
|
|
| OUTPUT_DIR = "jobs" |
| os.makedirs(OUTPUT_DIR, exist_ok=True) |
|
|
|
|
| def render_subtitles( |
| video_path: str, |
| srt_text: str, |
| output_path: str | None = None, |
| ): |
| """ |
| Universal subtitle renderer for V7. |
| |
| Supports: |
| - API render |
| - UI render |
| - Batch jobs |
| - Worker queue |
| """ |
|
|
| if output_path is None: |
| output_path = os.path.join( |
| OUTPUT_DIR, |
| f"{uuid.uuid4()}_render.mp4" |
| ) |
|
|
| |
| |
| |
|
|
| srt_path = output_path.replace(".mp4", ".srt") |
|
|
| with open(srt_path, "w", encoding="utf-8") as f: |
| f.write(srt_text) |
|
|
| logger.info(f"[RENDER] SRT saved → {srt_path}") |
|
|
| |
| |
| |
|
|
| cmd = [ |
| "ffmpeg", |
| "-y", |
| "-i", video_path, |
| "-vf", f"subtitles={srt_path}", |
| "-c:a", "copy", |
| output_path, |
| ] |
|
|
| logger.info("[RENDER] Running ffmpeg render") |
|
|
| process = subprocess.run( |
| cmd, |
| stdout=subprocess.PIPE, |
| stderr=subprocess.PIPE, |
| text=True |
| ) |
|
|
| if process.returncode != 0: |
| logger.error(process.stderr) |
| raise Exception("FFmpeg render failed") |
|
|
| if not os.path.exists(output_path): |
| raise Exception("Rendered file missing") |
|
|
| logger.info(f"[RENDER] Output → {output_path}") |
|
|
| return output_path |