trim_audio
Cut audio files to specified start and end times or durations using FFmpeg. Input and output paths, time formats, and audio formats are customizable for precise trimming.
Instructions
Trim an audio file to a specific duration
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| duration | No | Duration (format: HH:MM:SS.mmm or seconds) | |
| endTime | No | End time (format: HH:MM:SS.mmm or seconds) | |
| format | No | Audio format for output (mp3, aac, etc.) | |
| inputPath | Yes | Path to the input audio file | |
| outputPath | Yes | Path for the output audio file | |
| startTime | No | Start time (format: HH:MM:SS.mmm or seconds) |
Implementation Reference
- src/tools/handlers.ts:149-186 (handler)The handler function for the 'trim_audio' tool within the switch statement in handleToolCall. It validates inputs, builds an FFmpeg command to trim the audio file based on start time, duration or end time, optionally re-encodes in a specified format, and returns the result.case "trim_audio": { const inputPath = validatePath(String(args?.inputPath), true); const outputPath = validatePath(String(args?.outputPath)); const startTime = String(args?.startTime || "0"); const duration = String(args?.duration || ""); const endTime = String(args?.endTime || ""); const format = String(args?.format || ""); await ensureDirectoryExists(outputPath); // Build the FFmpeg command let command = `-i "${inputPath}" -ss ${startTime}`; // Add duration or end time if provided if (duration) { command += ` -t ${duration}`; } else if (endTime) { command += ` -to ${endTime}`; } // Add format if specified, otherwise use copy codec if (format) { command += ` -acodec ${format}`; } else { command += ` -acodec copy`; } command += ` "${outputPath}" -y`; const result = await runFFmpegCommand(command); return { content: [{ type: "text", text: `Audio trimming completed: ${inputPath} → ${outputPath}\n\n${result}` }] }; }
- src/tools/definitions.ts:158-191 (schema)The tool definition object including name, description, and input schema for the 'trim_audio' tool, used for registration and validation.{ name: "trim_audio", description: "Trim an audio file to a specific duration", inputSchema: { type: "object", properties: { inputPath: { type: "string", description: "Path to the input audio file" }, outputPath: { type: "string", description: "Path for the output audio file" }, startTime: { type: "string", description: "Start time (format: HH:MM:SS.mmm or seconds)" }, duration: { type: "string", description: "Duration (format: HH:MM:SS.mmm or seconds)" }, endTime: { type: "string", description: "End time (format: HH:MM:SS.mmm or seconds)" }, format: { type: "string", description: "Audio format for output (mp3, aac, etc.)" } }, required: ["inputPath", "outputPath"] } },
- src/index.ts:46-50 (registration)Registration of all tools including 'trim_audio' via the ListToolsRequestSchema handler, which returns the toolDefinitions array containing the trim_audio schema.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: toolDefinitions }; });
- src/index.ts:56-68 (registration)Registration of the tool call handler that routes calls to 'trim_audio' (and others) to the handleToolCall function based on the tool name.server.setRequestHandler(CallToolRequestSchema, async (request) => { try { return await handleToolCall(request.params.name, request.params.arguments); } catch (error: any) { console.error("Tool execution error:", error.message); return { content: [{ type: "text", text: `Error: ${error.message}` }] }; } });