trim-video
Cut video files to a specified duration and save them in a desired location using start and end timestamps. Ideal for precise editing and reducing file size.
Instructions
Trim video to specified duration
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| duration | Yes | Duration in format HH:MM:SS | |
| inputPath | Yes | Absolute path to input video file | |
| outputFilename | No | Output filename (only used if outputPath is not provided) | |
| outputPath | No | Optional absolute path for output file. If not provided, file will be saved in Downloads folder | |
| startTime | Yes | Start time in format HH:MM:SS |
Implementation Reference
- src/index.ts:236-269 (handler)The handler function that performs video trimming using fluent-ffmpeg. It resolves input/output paths, constructs the FFmpeg command with start time and duration, executes it, and returns a success or error message.async ({ inputPath, startTime, duration, outputPath, outputFilename }) => { try { const absoluteInputPath = await getAbsolutePath(inputPath); const inputFileName = absoluteInputPath.split('/').pop()?.split('.')[0] || 'output'; const defaultFilename = outputFilename || `${inputFileName}_trimmed.mp4`; const finalOutputPath = await getOutputPath(outputPath, defaultFilename); const command = ffmpeg(absoluteInputPath) .setStartTime(startTime) .setDuration(duration) .save(finalOutputPath); await executeFFmpeg(command); return { content: [ { type: "text", text: `Video successfully trimmed and saved to: ${finalOutputPath}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error trimming video: ${errorMessage}`, }, ], }; } }
- src/index.ts:229-235 (schema)Zod schema defining the input parameters for the trim-video tool.{ inputPath: z.string().describe("Absolute path to input video file"), startTime: z.string().describe("Start time in format HH:MM:SS"), duration: z.string().describe("Duration in format HH:MM:SS"), outputPath: z.string().optional().describe("Optional absolute path for output file. If not provided, file will be saved in Downloads folder"), outputFilename: z.string().optional().describe("Output filename (only used if outputPath is not provided)") },
- src/index.ts:226-270 (registration)Registration of the trim-video tool with the MCP server, specifying name, description, input schema, and handler function.server.tool( "trim-video", "Trim video to specified duration", { inputPath: z.string().describe("Absolute path to input video file"), startTime: z.string().describe("Start time in format HH:MM:SS"), duration: z.string().describe("Duration in format HH:MM:SS"), outputPath: z.string().optional().describe("Optional absolute path for output file. If not provided, file will be saved in Downloads folder"), outputFilename: z.string().optional().describe("Output filename (only used if outputPath is not provided)") }, async ({ inputPath, startTime, duration, outputPath, outputFilename }) => { try { const absoluteInputPath = await getAbsolutePath(inputPath); const inputFileName = absoluteInputPath.split('/').pop()?.split('.')[0] || 'output'; const defaultFilename = outputFilename || `${inputFileName}_trimmed.mp4`; const finalOutputPath = await getOutputPath(outputPath, defaultFilename); const command = ffmpeg(absoluteInputPath) .setStartTime(startTime) .setDuration(duration) .save(finalOutputPath); await executeFFmpeg(command); return { content: [ { type: "text", text: `Video successfully trimmed and saved to: ${finalOutputPath}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error trimming video: ${errorMessage}`, }, ], }; } } );
- src/index.ts:79-86 (helper)Helper function to execute FFmpeg commands as promises, used by trim-video and other video tools.const executeFFmpeg = (command: any): Promise<void> => { return new Promise((resolve, reject) => { command .on('end', () => resolve()) .on('error', (err: Error) => reject(err)) .run(); }); };
- src/index.ts:30-44 (helper)Helper function to resolve relative input paths to absolute paths and verify file existence, used in trim-video.async function getAbsolutePath(inputPath: string): Promise<string> { if (isAbsolute(inputPath)) { return inputPath; } // FIXME: But it's not working, because the server is running in a different directory const absolutePath = resolve(process.cwd(), inputPath); try { await fs.access(absolutePath); return absolutePath; } catch (error) { throw new Error(`Input file not found: ${inputPath}`); } }