clipVideo
Trim video clips with millisecond precision, set custom time segments, and adjust quality, video, and audio codecs for tailored output.
Instructions
剪辑视频片段,支持毫秒级精度的时间段裁剪
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| audioCodec | No | 音频编码格式 | |
| inputPath | Yes | 输入视频文件路径 | |
| outputPath | Yes | 输出视频文件路径 | |
| preserveMetadata | No | 是否保留元数据 | |
| quality | No | 视频质量预设 | |
| timeSegment | Yes | ||
| videoCodec | No | 视频编码格式 |
Implementation Reference
- src/mcp/server.ts:344-354 (handler)MCP tool handler for 'clipVideo' that delegates to VideoEngine.clipVideo and formats the response for MCP protocol.private async handleClipVideo(args: MCPToolParams['clipVideo']) { const result = await this.videoEngine.clipVideo(args); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- src/core/video-engine.ts:76-129 (handler)Core implementation of the video clipping functionality using FFmpeg, including file validation, time segment processing, encoding configuration, and error handling.public async clipVideo(options: ClipOptions): Promise<ProcessResult> { const startTime = Date.now(); const taskId = uuidv4(); try { // 验证输入文件 await this.validateInputFile(options.inputPath); // 确保输出目录存在 await this.ensureOutputDir(options.outputPath); // 验证时间段 const videoInfo = await this.getVideoInfo(options.inputPath); this.validateTimeSegment(options.timeSegment, videoInfo.duration); return new Promise((resolve, reject) => { const command = ffmpeg(options.inputPath) .seekInput(options.timeSegment.start / 1000) // 转换为秒 .duration((options.timeSegment.end - options.timeSegment.start) / 1000) .output(options.outputPath); // 设置编码参数 this.applyEncodingOptions(command, options); // 进度监听 command.on('progress', (progress: any) => { // 可以在这里添加进度回调 }); command.on('end', () => { resolve({ success: true, outputPaths: [options.outputPath], duration: Date.now() - startTime }); }); command.on('error', (err: any) => { reject(new Error(`视频剪辑失败: ${err.message}`)); }); this.processingTasks.set(taskId, command); command.run(); }); } catch (error) { return { success: false, outputPaths: [], duration: Date.now() - startTime, error: error instanceof Error ? error.message : '未知错误' }; } }
- src/mcp/server.ts:110-159 (registration)Registration of the 'clipVideo' MCP tool, defining its name, description, and comprehensive input schema for parameters like input/output paths, time segment, and encoding options.name: 'clipVideo', description: '剪辑视频片段,支持毫秒级精度的时间段裁剪', inputSchema: { type: 'object', properties: { inputPath: { type: 'string', description: '输入视频文件路径' }, outputPath: { type: 'string', description: '输出视频文件路径' }, timeSegment: { type: 'object', properties: { start: { type: 'number', description: '开始时间(毫秒)' }, end: { type: 'number', description: '结束时间(毫秒)' } }, required: ['start', 'end'] }, quality: { type: 'string', enum: Object.values(QualityPreset), description: '视频质量预设' }, videoCodec: { type: 'string', enum: Object.values(VideoCodec), description: '视频编码格式' }, audioCodec: { type: 'string', enum: Object.values(AudioCodec), description: '音频编码格式' }, preserveMetadata: { type: 'boolean', description: '是否保留元数据' } }, required: ['inputPath', 'outputPath', 'timeSegment'] } },
- src/types/mcp.ts:17-17 (schema)TypeScript type definition for 'clipVideo' tool input parameters, referencing ClipOptions interface.clipVideo: ClipOptions;
- src/types/mcp.ts:51-51 (schema)TypeScript type definition for 'clipVideo' tool output result, referencing ProcessResult interface.clipVideo: ProcessResult;