import json import os import asyncio video_path = params_kw.get('video_path', '') subtitle_path = params_kw.get('subtitle_path', '') output_name = params_kw.get('output_name', 'subtitled_output') output_dir = os.path.join('/tmp/ffmpeg_output', output_name) os.makedirs(output_dir, exist_ok=True) output_path = os.path.join(output_dir, 'output.mp4') try: if not video_path or not subtitle_path: result = json.dumps({"status": "error", "error": "video_path and subtitle_path are required"}) elif not os.path.isfile(video_path): result = json.dumps({"status": "error", "error": f"Video file not found: {video_path}"}) elif not os.path.isfile(subtitle_path): result = json.dumps({"status": "error", "error": f"Subtitle file not found: {subtitle_path}"}) else: proc = await asyncio.create_subprocess_exec( 'ffmpeg', '-y', '-i', video_path, '-vf', f"ass={subtitle_path}", '-c:v', 'libx264', '-c:a', 'copy', output_path, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) stdout, stderr = await proc.communicate() if proc.returncode == 0: result = json.dumps({"status": "success", "output_path": output_path}) else: result = json.dumps({"status": "error", "error": stderr.decode()}) except Exception as e: result = json.dumps({"status": "error", "error": str(e)}) return result