set_video_audio_track_codec
Modify the audio codec of a video's audio track while preserving the video stream. Specify input and output paths alongside the desired audio codec for processing.
Instructions
Sets the audio codec of a video's audio track, attempting to copy the video stream. Args: input_video_path: Path to the source video file. output_video_path: Path to save the video with the new audio codec. audio_codec: Target audio codec (e.g., 'aac', 'mp3'). Returns: A status message indicating success or failure.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| audio_codec | Yes | ||
| input_video_path | Yes | ||
| output_video_path | Yes |
Implementation Reference
- server.py:427-439 (handler)The handler function for the 'set_video_audio_track_codec' MCP tool. It uses FFmpeg to change the audio codec of the video's audio track, first trying to copy the video stream, falling back to re-encoding the video if necessary. The schema (input parameters and description) is defined in the function signature and docstring.@mcp.tool() def set_video_audio_track_codec(input_video_path: str, output_video_path: str, audio_codec: str) -> str: """Sets the audio codec of a video's audio track, attempting to copy the video stream. Args: input_video_path: Path to the source video file. output_video_path: Path to save the video with the new audio codec. audio_codec: Target audio codec (e.g., 'aac', 'mp3'). Returns: A status message indicating success or failure. """ primary_kwargs = {'acodec': audio_codec, 'vcodec': 'copy'} fallback_kwargs = {'acodec': audio_codec} # Re-encode video return _run_ffmpeg_with_fallback(input_video_path, output_video_path, primary_kwargs, fallback_kwargs)
- server.py:332-349 (helper)Supporting helper function called by set_video_audio_track_codec (and similar tools) to execute the FFmpeg command with a primary set of parameters (e.g., copying the video stream) and a fallback (full re-encode). Handles errors and provides status messages.def _run_ffmpeg_with_fallback(input_path: str, output_path: str, primary_kwargs: dict, fallback_kwargs: dict) -> str: """Helper to run ffmpeg command with primary kwargs, falling back to other kwargs on ffmpeg.Error.""" try: ffmpeg.input(input_path).output(output_path, **primary_kwargs).run(capture_stdout=True, capture_stderr=True) return f"Operation successful (primary method) and saved to {output_path}" except ffmpeg.Error as e_primary: try: ffmpeg.input(input_path).output(output_path, **fallback_kwargs).run(capture_stdout=True, capture_stderr=True) return f"Operation successful (fallback method) and saved to {output_path}" except ffmpeg.Error as e_fallback: err_primary_msg = e_primary.stderr.decode('utf8') if e_primary.stderr else str(e_primary) err_fallback_msg = e_fallback.stderr.decode('utf8') if e_fallback.stderr else str(e_fallback) return f"Error. Primary method failed: {err_primary_msg}. Fallback method also failed: {err_fallback_msg}" except FileNotFoundError: return f"Error: Input file not found at {input_path}" except Exception as e: return f"An unexpected error occurred: {str(e)}"
- server.py:10-10 (registration)Initialization of the FastMCP server instance where tools like set_video_audio_track_codec are registered via the @mcp.tool() decorator.mcp = FastMCP("VideoAudioServer")