trim_video
Remove unwanted sections from a video by specifying exact start and end times, and save the trimmed output to a designated file path. Ideal for precise video editing.
Instructions
Trims a video to the specified start and end times.
Args: video_path: The path to the input video file. output_video_path: The path to save the trimmed video file. start_time: The start time for trimming (HH:MM:SS or seconds). end_time: The end time for trimming (HH:MM:SS or seconds). Returns: A status message indicating success or failure.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end_time | Yes | ||
| output_video_path | Yes | ||
| start_time | Yes | ||
| video_path | Yes |
Implementation Reference
- server.py:42-75 (handler)The main handler function for the 'trim_video' MCP tool. It uses FFmpeg to trim the video from start_time to end_time, first trying to copy codecs without re-encoding, falling back to re-encoding if necessary. Returns a success or error message.@mcp.tool() def trim_video(video_path: str, output_video_path: str, start_time: str, end_time: str) -> str: """Trims a video to the specified start and end times. Args: video_path: The path to the input video file. output_video_path: The path to save the trimmed video file. start_time: The start time for trimming (HH:MM:SS or seconds). end_time: The end time for trimming (HH:MM:SS or seconds). Returns: A status message indicating success or failure. """ try: input_stream = ffmpeg.input(video_path, ss=start_time, to=end_time) # Attempt to copy codecs to avoid re-encoding if possible output_stream = input_stream.output(output_video_path, c='copy') output_stream.run(capture_stdout=True, capture_stderr=True) return f"Video trimmed successfully (codec copy) to {output_video_path}" except ffmpeg.Error as e: error_message_copy = e.stderr.decode('utf8') if e.stderr else str(e) try: # Fallback to re-encoding if codec copy fails input_stream_recode = ffmpeg.input(video_path, ss=start_time, to=end_time) output_stream_recode = input_stream_recode.output(output_video_path) output_stream_recode.run(capture_stdout=True, capture_stderr=True) return f"Video trimmed successfully (re-encoded) to {output_video_path}" except ffmpeg.Error as e_recode: error_message_recode = e_recode.stderr.decode('utf8') if e_recode.stderr else str(e_recode) return f"Error trimming video. Copy attempt: {error_message_copy}. Re-encode attempt: {error_message_recode}" except FileNotFoundError: return f"Error: Input video file not found at {video_path}" except Exception as e: return f"An unexpected error occurred: {str(e)}"
- server.py:42-42 (registration)The @mcp.tool() decorator registers the trim_video function as an MCP tool.@mcp.tool()
- server.py:43-52 (schema)The function signature and docstring define the input schema (video_path, output_video_path, start_time, end_time as strings) and output (str status message).def trim_video(video_path: str, output_video_path: str, start_time: str, end_time: str) -> str: """Trims a video to the specified start and end times. Args: video_path: The path to the input video file. output_video_path: The path to save the trimmed video file. start_time: The start time for trimming (HH:MM:SS or seconds). end_time: The end time for trimming (HH:MM:SS or seconds). Returns: A status message indicating success or failure.