Skip to main content
Glama

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
NameRequiredDescriptionDefault
durationYesDuration in format HH:MM:SS
inputPathYesAbsolute path to input video file
outputFilenameNoOutput filename (only used if outputPath is not provided)
outputPathNoOptional absolute path for output file. If not provided, file will be saved in Downloads folder
startTimeYesStart time in format HH:MM:SS

Implementation Reference

  • 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}`, }, ], }; } }
  • 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}`, }, ], }; } } );
  • 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(); }); };
  • 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}`); } }

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/maoxiaoke/mcp-media-processor'

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