Skip to main content
Glama

splitVideo

Split video files by duration, size, or segment count using MCP integration. Specify input path, output directory, and splitting method for precise video file segmentation.

Instructions

分割视频文件,支持按时长、大小或段数分割

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
audioCodecNo音频编码格式
durationNo按时长分割(秒)
inputPathYes输入视频文件路径
maxSizeNo按大小分割(MB)
namePatternNo文件命名模式,支持 {name}、{index}、{ext} 占位符
outputDirYes输出目录路径
qualityNo视频质量预设
segmentCountNo分割段数
splitByYes分割方式
videoCodecNo视频编码格式

Implementation Reference

  • Core handler implementing splitVideo tool logic: validates input, calculates split segments, iteratively clips segments using FFmpeg, handles errors and returns results.
    public async splitVideo(options: SplitOptions): Promise<ProcessResult> { const startTime = Date.now(); try { // 验证输入文件 await this.validateInputFile(options.inputPath); // 确保输出目录存在 await fs.mkdir(options.outputDir, { recursive: true }); const videoInfo = await this.getVideoInfo(options.inputPath); const segments = this.calculateSplitSegments(videoInfo, options); const outputPaths: string[] = []; const errors: string[] = []; // 串行处理分割任务,避免并发导致的FFmpeg异常 for (let i = 0; i < segments.length; i++) { const segment = segments[i]; const outputPath = path.join( options.outputDir, this.generateSplitFileName(options.inputPath, i, options.namePattern) ); try { const clipOptions: ClipOptions = { inputPath: options.inputPath, outputPath, timeSegment: segment, quality: options.quality, videoCodec: options.videoCodec, audioCodec: options.audioCodec }; const result = await this.clipVideo(clipOptions); if (result.success && result.outputPaths.length > 0) { outputPaths.push(outputPath); // 验证生成的文件是否有效 try { const stats = await fs.stat(outputPath); if (stats.size === 0) { errors.push(`分割文件 ${i + 1} 大小为0`); } } catch (statError) { errors.push(`无法验证分割文件 ${i + 1}: ${statError}`); } } else { errors.push(`分割任务 ${i + 1} 失败: ${result.error || '未知错误'}`); } } catch (segmentError) { errors.push(`分割任务 ${i + 1} 异常: ${segmentError instanceof Error ? segmentError.message : '未知错误'}`); } // 添加短暂延迟,避免FFmpeg进程冲突 if (i < segments.length - 1) { await new Promise(resolve => setTimeout(resolve, 100)); } } const success = outputPaths.length > 0; const errorMessage = errors.length > 0 ? `部分任务失败: ${errors.join('; ')}` : undefined; return { success, outputPaths, duration: Date.now() - startTime, error: success ? errorMessage : (errorMessage || '所有分割任务都失败了') }; } catch (error) { return { success: false, outputPaths: [], duration: Date.now() - startTime, error: error instanceof Error ? error.message : '未知错误' }; } }
  • Registration of the splitVideo tool in MCP server, defining name, description, and input schema for validation.
    { name: 'splitVideo', description: '分割视频文件,支持按时长、大小或段数分割', inputSchema: { type: 'object', properties: { inputPath: { type: 'string', description: '输入视频文件路径' }, outputDir: { type: 'string', description: '输出目录路径' }, splitBy: { type: 'string', enum: ['duration', 'size', 'segments'], description: '分割方式' }, duration: { type: 'number', description: '按时长分割(秒)' }, maxSize: { type: 'number', description: '按大小分割(MB)' }, segmentCount: { type: 'number', description: '分割段数' }, quality: { type: 'string', enum: Object.values(QualityPreset), description: '视频质量预设' }, videoCodec: { type: 'string', enum: Object.values(VideoCodec), description: '视频编码格式' }, audioCodec: { type: 'string', enum: Object.values(AudioCodec), description: '音频编码格式' }, namePattern: { type: 'string', description: '文件命名模式,支持 {name}、{index}、{ext} 占位符' } }, required: ['inputPath', 'outputDir', 'splitBy'] }
  • MCP server dispatch handler for splitVideo tool calls, delegates execution to VideoEngine and formats JSON response.
    private async handleSplitVideo(args: MCPToolParams['splitVideo']) { const result = await this.videoEngine.splitVideo(args); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], };
  • Type definitions for splitVideo tool input (SplitOptions) and output (ProcessResult) parameters.
    splitVideo: SplitOptions; // 获取视频信息工具参数 getVideoInfo: { filePath: string; }; // 批量处理工具参数 batchProcess: { tasks: Omit<BatchTask, 'id' | 'status' | 'createdAt'>[]; }; // 获取支持格式工具参数 getSupportedFormats: Record<string, never>; // 取消任务工具参数 cancelTask: { taskId: string; }; // 获取任务状态工具参数 getTaskStatus: { taskId: string; }; } // MCP工具返回值类型 export interface MCPToolResults { clipVideo: ProcessResult; mergeVideos: ProcessResult; splitVideo: ProcessResult;

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pickstar-2002/video-clip-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server