extract_frames_from_video
Extract frames from a video at specified intervals or total count, saving them in PNG, JPG, or WebP formats. Ideal for creating still images, thumbnails, or analyzing video content.
Instructions
提取视频中的图像。
参数:
video_path(str) - 视频路径。
fps(int) - 每多少秒抽一帧,如果传0,代表全部都抽,传1,代表每一秒抽1帧。
output_folder(str) - 把图片输出到哪个目录
format(int) - 抽取的图片格式,0:代表png 1:jpg 2:webp
total_frames(int) - 最多抽取多少张,0代表不限制
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | ||
| fps | No | ||
| output_folder | No | ||
| total_frames | No | ||
| video_path | Yes |
Implementation Reference
- src/ffmpeg_mcp/server.py:134-146 (handler)MCP tool handler for 'extract_frames_from_video' decorated with @mcp.tool(). Includes input parameters schema in docstring and delegates to helper function in cut_video.py.@mcp.tool() def extract_frames_from_video(video_path,fps=0, output_folder=None, format=0, total_frames=0): """ 提取视频中的图像。 参数: video_path(str) - 视频路径。 fps(int) - 每多少秒抽一帧,如果传0,代表全部都抽,传1,代表每一秒抽1帧。 output_folder(str) - 把图片输出到哪个目录 format(int) - 抽取的图片格式,0:代表png 1:jpg 2:webp total_frames(int) - 最多抽取多少张,0代表不限制 """ return cut_video.extract_frames_from_video(video_path, fps, output_folder, format, total_frames)
- src/ffmpeg_mcp/cut_video.py:277-315 (helper)Core implementation of frame extraction using FFmpeg, handling parameters to construct the command for extracting frames at specified fps, format, and total_frames limit.def extract_frames_from_video(video_path,fps=0, output_folder=None, format=0, total_frames=0): """ 使用 FFmpeg 提取视频中的每一帧图像。 :param video_path: 视频文件的路径。 :param fps: 每多少秒抽一帧,如果传0,代表每一帧都抽 :param output_folder: 输出图像的文件夹路径。 :param format: 输出图像的图片格式 0:png 1:jpg 2:webp。 """ # 确保输出文件夹存在 if output_folder == None: output_folder = os.path.dirname(video_path) if not os.path.exists(output_folder): os.makedirs(output_folder) img_ext = "png" if (format == 0): img_ext = "png" elif (format == 1): img_ext = "jpg" else: img_ext = "webp" output_path = os.path.join(output_folder, f'frame_%04d.{img_ext}') try: cmd = f" -i {video_path}" # 执行 FFmpeg 命令 if fps > 0: cmd = f" {cmd} -vf 'fps=1/{fps}'" else: cmd = f" {cmd} -vsync 0" if (total_frames > 0): cmd = f" {cmd} -vframes {total_frames} " cmd = f" {cmd} -y {output_path}" status_code, log = ffmpeg.run_ffmpeg(cmd, timeout=1000) print(log) return {status_code, log, output_path} except Exception as e: print(f"抽取失败: {str(e)}") return {-1, str(e), ""}