n8n-nodes-mediafx
Version:
N8N custom nodes for video editing and media processing
147 lines (146 loc) • 6.25 kB
JavaScript
;
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.executeStampImage = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const path = __importStar(require("path"));
const ffmpeg = require("fluent-ffmpeg");
const utils_1 = require("../utils");
const fs = __importStar(require("fs-extra"));
async function executeStampImage(videoPath, imagePath, options, itemIndex) {
var _a;
const outputPath = (0, utils_1.getTempFile)(path.extname(videoPath));
// Basic stamp options
const width = options.width || 150;
const height = options.height || -1;
const x = options.x || '10';
const y = options.y || '10';
const rotationDegrees = options.rotation || 0;
const opacity = (_a = options.opacity) !== null && _a !== void 0 ? _a : 1.0;
// Time control options
const enableTimeControl = options.enableTimeControl;
const startTime = options.startTime || 0;
const endTime = options.endTime || 5;
try {
// Get video duration for calculations
// const videoDuration = await getDuration(videoPath);
// console.log('=== STAMP IMAGE DEBUG ===');
// console.log('Video path:', videoPath);
// console.log('Image path:', imagePath);
// console.log('Video duration:', videoDuration);
// console.log('Stamp options:', {
// width, height, x, y, rotationDegrees, opacity,
// enableTimeControl, startTime, endTime
// });
// Start with the simplest possible filter
let complexFilter = '';
// Check if we need any processing
const needsScaling = width !== -1 || height !== -1;
const needsRotation = rotationDegrees !== 0;
const needsOpacity = opacity < 1.0;
const needsProcessing = needsScaling || needsRotation || needsOpacity;
if (!needsProcessing && !enableTimeControl) {
// Simplest case: direct overlay
complexFilter = '[0:v][1:v]overlay=x=' + x + ':y=' + y;
}
else if (!enableTimeControl) {
// Process image but no time control
let imageFilter = '[1:v]';
if (needsScaling) {
imageFilter += 'scale=' + width + ':' + height;
}
if (needsRotation) {
if (!needsScaling) {
imageFilter += 'rotate=' + (rotationDegrees * Math.PI / 180) + ':fillcolor=none';
}
else {
imageFilter += ',rotate=' + (rotationDegrees * Math.PI / 180) + ':fillcolor=none';
}
}
if (needsOpacity) {
if (!needsScaling && !needsRotation) {
imageFilter += 'colorchannelmixer=aa=' + opacity;
}
else {
imageFilter += ',colorchannelmixer=aa=' + opacity;
}
}
imageFilter += '[processed]';
complexFilter = imageFilter + ';[0:v][processed]overlay=x=' + x + ':y=' + y;
}
else {
// With time control
let imageFilter = '[1:v]';
if (needsScaling) {
imageFilter += 'scale=' + width + ':' + height;
}
if (needsRotation) {
if (!needsScaling) {
imageFilter += 'rotate=' + (rotationDegrees * Math.PI / 180) + ':fillcolor=none';
}
else {
imageFilter += ',rotate=' + (rotationDegrees * Math.PI / 180) + ':fillcolor=none';
}
}
if (needsOpacity) {
if (!needsScaling && !needsRotation) {
imageFilter += 'colorchannelmixer=aa=' + opacity;
}
else {
imageFilter += ',colorchannelmixer=aa=' + opacity;
}
}
if (needsProcessing) {
imageFilter += '[processed]';
complexFilter = imageFilter + ';[0:v][processed]overlay=x=' + x + ':y=' + y;
}
else {
complexFilter = '[0:v][1:v]overlay=x=' + x + ':y=' + y;
}
// Add time control
if (enableTimeControl) {
complexFilter += ':enable=\'between(t,' + startTime + ',' + endTime + ')\'';
}
}
console.log('Generated FFmpeg filter:', complexFilter);
console.log('========================');
const command = ffmpeg()
.input(videoPath)
.input(imagePath)
.complexFilter(complexFilter)
.output(outputPath);
await (0, utils_1.runFfmpeg)(command);
return outputPath;
}
catch (error) {
// Clean up output file if creation failed
await fs.remove(outputPath).catch(() => { });
console.error('=== STAMP IMAGE ERROR ===');
console.error('Error details:', error);
console.error('========================');
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to stamp image: ${error instanceof Error ? error.message : 'Unknown error'}`, { itemIndex });
}
}
exports.executeStampImage = executeStampImage;