UNPKG

n8n-nodes-mediafx

Version:

N8N custom nodes for video editing and media processing

79 lines (78 loc) 3.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeSeparateAudio = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ffmpeg = require("fluent-ffmpeg"); const utils_1 = require("../utils"); const fs = __importStar(require("fs-extra")); /** * Separates audio from video, returning both a muted video and the extracted audio track. * * @param input - Path to the input video file * @param videoFormat - Output format for the muted video (e.g., 'mp4', 'mov') * @param audioFormat - Output format for the extracted audio (e.g., 'mp3', 'aac', 'wav') * @param audioCodec - Audio codec to use (e.g., 'copy', 'libmp3lame', 'aac') * @param audioBitrate - Audio bitrate (e.g., '192k', '320k') * @param itemIndex - Current item index for error handling * @returns Object containing paths to both the muted video and extracted audio */ async function executeSeparateAudio(input, videoFormat, audioFormat, audioCodec, audioBitrate, itemIndex) { const mutedVideoPath = (0, utils_1.getTempFile)(`.${videoFormat}`); const audioOutputPath = (0, utils_1.getTempFile)(`.${audioFormat}`); let finalAudioCodec = audioCodec; // If stream copy ('copy') is selected for a format that requires a specific // codec (like mp3), we must override it to prevent an FFmpeg error. if (finalAudioCodec === 'copy' && audioFormat === 'mp3') { finalAudioCodec = 'libmp3lame'; } try { // Step 1: Create muted video (remove audio track) const mutedVideoCommand = ffmpeg(input) .output(mutedVideoPath) .noAudio() .videoCodec('copy'); // Copy video stream without re-encoding await (0, utils_1.runFfmpeg)(mutedVideoCommand); // Step 2: Extract audio track const audioCommand = ffmpeg(input) .output(audioOutputPath) .noVideo() .audioCodec(finalAudioCodec) .audioBitrate(audioBitrate) .outputOptions('-map', '0:a:0'); // Select first audio stream await (0, utils_1.runFfmpeg)(audioCommand); return { videoPath: mutedVideoPath, audioPath: audioOutputPath, }; } catch (error) { // Clean up any created files on error await fs.remove(mutedVideoPath).catch(() => { }); await fs.remove(audioOutputPath).catch(() => { }); throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Error separating audio from video. Please ensure the source video contains both video and audio tracks. FFmpeg error: ${error.message}`, { itemIndex }); } } exports.executeSeparateAudio = executeSeparateAudio;