File size: 1,611 Bytes
1425afc | 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 | 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"
)
# --------------------------------------------------
# Write SRT file
# --------------------------------------------------
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}")
# --------------------------------------------------
# FFmpeg Subtitle Burn
# --------------------------------------------------
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 |