import json import os import asyncio video_path = params_kw.get('video_path', '') vocals_path = params_kw.get('vocals_path', '') accompaniment_path = params_kw.get('accompaniment_path', '') subtitle_path = params_kw.get('subtitle_path', '') output_name = params_kw.get('output_name', 'ktv_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.mkv') try: missing = [] if not video_path: missing.append('video_path') if not vocals_path: missing.append('vocals_path') if not accompaniment_path: missing.append('accompaniment_path') if not subtitle_path: missing.append('subtitle_path') if missing: result = json.dumps({"status": "error", "error": f"Missing required params: {', '.join(missing)}"}) 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(vocals_path): result = json.dumps({"status": "error", "error": f"Vocals file not found: {vocals_path}"}) elif not os.path.isfile(accompaniment_path): result = json.dumps({"status": "error", "error": f"Accompaniment file not found: {accompaniment_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, '-i', vocals_path, '-i', accompaniment_path, '-vf', f"ass={subtitle_path}", '-map', '0:v', '-map', '1:a', '-map', '2:a', '-c:v', 'libx264', '-c:a', 'aac', 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