I’m scaling a video to 920×692.
Then I want to resize an image and put it in the button corner of the video.
Here is my code, using nodejs with the ffmpeg fluent library import.
I’m having issue with the complex filters.
// load imports
const ffmpeg = require("fluent-ffmpeg");
const sizeOf = require("image-size");
// load files
const inputFile = "./test.mp4";
const outputFile = "./output.mp4";
const watermarkFile = "./watermark.png";
// load output video size/res
const outputW = 920;
const outputH = 692;
// load output options
const outputOptions = [
"-movflags faststart",
"-c:v libx264",
"-crf 21",
"-preset medium",
"-r 30",
"-c:a aac",
"-ac 2",
"-strict -2",
"-f mp4",
"-af volume=6.0",
"-map [out]",
"-map 1:a",
]
// get the watermark image size
const watermarkFileSize = sizeOf(watermarkFile);
const watermark_w = watermarkFileSize.width;
const watermark_h = watermarkFileSize.height;
// calculate new image size based on height being 64px
const newWatermark_n_h = 64;
const newWatermark_n_w = Math.floor(
(watermark_w * newWatermark_n_h) / watermark_h
);
// calculate overlay offset position (to bottom right corner)
const watermark_x_offset = Math.floor(outputH - newWatermark_n_w);
const watermark_y_offset = Math.floor(
outputW - newWatermark_n_h
);
// define complex filters (these seem to not work)
const scaleVideo = "[1]scale=920:692[inner];[0][inner]overlay=0:0:[out]";
const applyOverlay = "[2]scale=" + newWatermark_n_w + ":" + newWatermark_n_h + "[inner];[out][inner]overlay=" + watermark_x_offset + ":" + watermark_y_offset + ":[out]",
// apply inputs, complex filters and output file
ffmpeg()
.input(inputFile)
.input(watermarkFile)
.complexFilter([
scaleVideo,
applyOverlay
]).outputOptions(outputOptions)
.saveToFile(outputFile);