"""Async wrapper around ``ffmpeg`` used to merge separate audio/video files.""" from __future__ import annotations import asyncio class FfmpegError(RuntimeError): """Raised when ffmpeg fails to merge the audio and video streams.""" async def merge(video_path: str, audio_path: str, destination: str) -> None: """Run ``ffmpeg -i video_path -i audio_path -c copy destination``.""" process = await asyncio.create_subprocess_exec( "ffmpeg", "-y", "-i", video_path, "-i", audio_path, "-c", "copy", destination, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT, ) stdout, _ = await process.communicate() return_code = await process.wait() if return_code != 0: message = stdout.decode("utf-8", errors="replace").strip() tail = "\n".join(message.splitlines()[-10:]) raise FfmpegError(tail or f"ffmpeg exited with status {return_code}.")