compress-video
Reduce video file size while maintaining quality by compressing videos using a customizable quality setting. Save output to a specified path or default folder with the MCP Media Processing Server.
Instructions
Compress video file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| 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 | |
| quality | No | Compression quality (1-51, lower is better quality but larger file) |
Implementation Reference
- src/index.ts:190-223 (handler)The handler function that implements the core logic for compressing a video file using fluent-ffmpeg with libx264 codec and CRF quality control.async ({ inputPath, quality, outputPath, outputFilename }) => { try { const absoluteInputPath = await getAbsolutePath(inputPath); const inputFileName = absoluteInputPath.split('/').pop()?.split('.')[0] || 'output'; const defaultFilename = outputFilename || `${inputFileName}_compressed.mp4`; const finalOutputPath = await getOutputPath(outputPath, defaultFilename); const command = ffmpeg(absoluteInputPath) .videoCodec('libx264') .addOption('-crf', quality.toString()) .save(finalOutputPath); await executeFFmpeg(command); return { content: [ { type: "text", text: `Video successfully compressed and saved to: ${finalOutputPath}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error compressing video: ${errorMessage}`, }, ], }; } }
- src/index.ts:184-189 (schema)Zod schema defining the input parameters and validation for the compress-video tool.{ inputPath: z.string().describe("Absolute path to input video file"), quality: z.number().min(1).max(51).default(23).describe("Compression quality (1-51, lower is better quality but larger file)"), 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:181-224 (registration)The server.tool call that registers the compress-video tool with the MCP server, including name, description, schema, and handler.server.tool( "compress-video", "Compress video file", { inputPath: z.string().describe("Absolute path to input video file"), quality: z.number().min(1).max(51).default(23).describe("Compression quality (1-51, lower is better quality but larger file)"), 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, quality, outputPath, outputFilename }) => { try { const absoluteInputPath = await getAbsolutePath(inputPath); const inputFileName = absoluteInputPath.split('/').pop()?.split('.')[0] || 'output'; const defaultFilename = outputFilename || `${inputFileName}_compressed.mp4`; const finalOutputPath = await getOutputPath(outputPath, defaultFilename); const command = ffmpeg(absoluteInputPath) .videoCodec('libx264') .addOption('-crf', quality.toString()) .save(finalOutputPath); await executeFFmpeg(command); return { content: [ { type: "text", text: `Video successfully compressed and saved to: ${finalOutputPath}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error compressing video: ${errorMessage}`, }, ], }; } } );
- src/index.ts:79-86 (helper)Utility function to execute FFmpeg commands as a Promise, used by compress-video and other video processing 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 to resolve relative input paths to absolute paths and verify file existence, used in compress-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}`); } }