Skip to main content
Glama

splitVideo

Split video files by duration, size, or segment count for easier sharing, editing, or storage management. Supports customizable encoding and naming patterns.

Instructions

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

Input Schema

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

Implementation Reference

  • Core implementation of the splitVideo tool. Calculates split segments based on criteria (duration, size, segments), then clips each segment serially using the clipVideo method, validates outputs, and returns ProcessResult with paths and any errors.
    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 : '未知错误' }; } }
  • MCP server handler for splitVideo tool call. Delegates execution to VideoEngine.splitVideo and formats response as MCP content.
    private async handleSplitVideo(args: MCPToolParams['splitVideo']) { const result = await this.videoEngine.splitVideo(args); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
  • Registration of the splitVideo tool in MCP server, including name, description, and detailed inputSchema 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'] }
  • Type definition mapping splitVideo tool params to SplitOptions interface.
    splitVideo: SplitOptions;
  • Type definition for splitVideo tool result as ProcessResult.
    splitVideo: ProcessResult;

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