clip_video
Extract specific segments from videos using start and end times or duration. Supports multiple time formats, enables custom output paths, and integrates with FFmpeg for precise video trimming.
Instructions
智能视频剪辑函数
参数:
video_path : str - 源视频文件路径
start : int/float/str - 开始时间(支持秒数、MM:SS、HH:MM:SS格式,默认为视频开头,如果不传该参数,或者该参数为负数,从视频结尾往前剪辑)
end : int/float/str - 结束时间(同上,默认为视频结尾)
duration: int/float/str - 裁剪时长,end和duration必须有一个
output_path: str - 裁剪后视频输出路径,如果不传入,会有一个默认的输出路径
time_out: int - 命令行执行超时时间,默认为300s
返回:
error - 错误码
str - ffmpeg执行过程中所有日志
str - 生成的剪辑文件路径
示例:
clip_video("input.mp4", "00:01:30", "02:30")
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| duration | No | ||
| end | No | ||
| output_path | No | ||
| start | No | ||
| time_out | No | ||
| video_path | Yes |
Implementation Reference
- src/ffmpeg_mcp/server.py:40-60 (handler)MCP tool handler and registration for 'clip_video'. This is the entry point decorated with @mcp.tool() that delegates to the core implementation.@mcp.tool() def clip_video(video_path, start=None, end=None,duration = None, output_path=None,time_out=300): """ 智能视频剪辑函数 参数: video_path : str - 源视频文件路径 start : int/float/str - 开始时间(支持秒数、MM:SS、HH:MM:SS格式,默认为视频开头,如果不传该参数,或者该参数为负数,从视频结尾往前剪辑) end : int/float/str - 结束时间(同上,默认为视频结尾) duration: int/float/str - 裁剪时长,end和duration必须有一个 output_path: str - 裁剪后视频输出路径,如果不传入,会有一个默认的输出路径 time_out: int - 命令行执行超时时间,默认为300s 返回: error - 错误码 str - ffmpeg执行过程中所有日志 str - 生成的剪辑文件路径 示例: clip_video("input.mp4", "00:01:30", "02:30") """ return cut_video.clip_video_ffmpeg(video_path,start=start,end=end,duration=duration, output_path=output_path,time_out=time_out)
- src/ffmpeg_mcp/cut_video.py:7-46 (helper)Core helper function implementing the video clipping logic using FFmpeg commands.def clip_video_ffmpeg(video_path, start = None, end = None, duration=None, output_path = None, time_out = 30): """ 智能视频剪辑函数 参数: video_path : str - 源视频文件路径 start : int/float/str - 开始时间(支持秒数、MM:SS、HH:MM:SS格式,默认为视频开头) end : int/float/str - 结束时间(同上,默认为视频结尾) duration: int/float/str - 裁剪时长,end和duration必须有一个 output_path: str - 裁剪后视频输出路径,如果不传入,会有一个默认的输出路径 time_out: int - 命令行执行超时时间,默认为30s 返回: error - 错误码 str - ffmpeg执行过程中所有日志 str - 生成的剪辑文件路径 示例: clip_video("input.mp4", "00:01:30", "02:30") """ try: base, ext = os.path.splitext(video_path) if (output_path == None): output_path = f"{base}_clip{ext}" cmd = f"-i {video_path} " if (start != None): start_sec = utils.convert_to_seconds(start) cmd = f"{cmd} -ss {start_sec}" if (end == None and duration is not None): end = start_sec + utils.convert_to_seconds(duration) if (end != None): end_sec = utils.convert_to_seconds(end) cmd = f"{cmd} -to {end_sec}" cmd = f"{cmd} -y {output_path}" print(cmd) status_code, log = ffmpeg.run_ffmpeg(cmd, timeout=time_out) print(log) return {status_code, log, output_path} except Exception as e: print(f"剪辑失败: {str(e)}") return {-1, str(e), ""}