I'm trying to merge a image to a video using ffmpeg.
This command works to me: ffmpeg -i merged-video.mp4 -loop 1 -i resized-image.png -shortest -c:a copy out.mp4
But it's not working when I do it in node.js, using fluent-ffmpeg.
My code is like this bellow:
const ffmpeg = require('fluent-ffmpeg');
ffmpeg()
.addInput(mergedVideo)
.loop(1)
.addInput(image)
.audioCodec('copy')
.outputOptions([
'-pix_fmt', 'yuv420p',
'-shortest'
])
.on('error', err => reject(err.message))
.on('end', resolve)
.save(finalResultVideo);
But it's getting error ffmpeg exited with code 1: Option loop not found.
Related
I wanted to find it here,but I found a similarity to this,but it wasn't for the node.js
how to mirror + add logo to video?
Here we can see a code:ffmpeg -i input.mp4 -i logo.png -filter_complex "hflip[flipped];[flipped]overlay=x=10:y=10" out.mp4
But i didn't understand how i can use it in Node.js
Please,help
you'll need to spawn FFMPEG from your node app:
var spawn = require('child_process').spawn;
var ffmpeg_process, feedStream=false;
var ops = [-i, input.mp4, -i, logo.png, -filter_complex, "hflip[flipped];[flipped]overlay=x=10:y=10", out.mp4];
ffmpeg_process=spawn('ffmpeg', ops);
//in this bit I was writing FFMPEG data to a socket, so I commented out those bits
ffmpeg_process.stderr.on('data',function(d){
//socket.emit('ffmpeg_stderr',''+d);
});
ffmpeg_process.on('error',function(e){
console.log('child process error'+e);
//socket.emit('fatal','ffmpeg error!'+e);
feedStream=false;
//socket.disconnect();
});
ffmpeg_process.on('exit',function(e){
console.log('child process exit'+e);
//socket.emit('fatal','ffmpeg exit!'+e);
//socket.disconnect();
});
a working example can be seen at livestream.a.video. GH: https://github.com/dougsillars/browserLiveStream
I have a lambda function to crop videos with ffmpeg
I am installing the layer this way
#!/bin/bash
mkdir -p layer
cd layer
rm -rf *
curl -O https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz
tar -xf ffmpeg-git-amd64-static.tar.xz
mv ffmpeg-git-*-amd64-static ffmpeg
rm ffmpeg-git-amd64-static.tar.xz
I do not really which version but should be recent as I did it today for the last time
Then my node js lambda function is running with the following nodejs module https://github.com/fluent-ffmpeg/node-fluent-ffmpeg
return new Promise((resolve, reject) => {
ffmpeg(inputFile.name)
.videoFilters(`crop=${width}:${height}:${x}:${y}`)
.format('mp4')
.on('error', reject)
.on('end', () => resolve(fs.readFileSync(outputFile.name)))
.save(outputFile.name);
with for example videoFilters('crop=500:500:20:20')
And I have the folowing error
ffmpeg exited with code 1: Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #0:0
Conversion failed!
On my local computer I am running the following command on the exact same image
ffmpeg -i in.mp4 -filter:v "crop=500:500:20:20" out.mp4
my version of ffmpeg is 4.2.2 and this is working great
I do not have the issue with all videos, here one video which is causing me the issue https://ajouve-util.s3.amazonaws.com/earth.mp4
Here is slightly modified code that works:
var ffmpeg = require('fluent-ffmpeg');
var reject = function(something) {
console.error("got error", something);
}
var resolve = function() {
console.info("Good, job done. Now read output file");
}
var width = 500;
var height = 500;
var x = 20;
var y = 20;
var filter_string = `crop=${width}:${height}:${x}:${y}`;
console.log("filter_string", filter_string);
var onStart = function(commandLine) {
console.log('Spawned Ffmpeg with command: ' + commandLine);
}
var command = ffmpeg('earth.mp4')
.videoFilters(filter_string)
.format('mp4')
.on('error', reject)
.on('end', resolve)
.on('start', onStart)
.output('earth_cropped.mp4');
console.log("running..");
command.run();
console.log("Done");
With earth.mp4 downloaded from your link in same dir output is:
node main.js
filter_string crop=500:500:20:20
running..
Done
Spawned Ffmpeg with command: ffmpeg -i earth.mp4 -y -filter:v crop=500:500:20:20 -f mp4 earth_cropped.mp4
Good, job done. Now read output file
And we get earth_cropped.mp4 file as expected.
I bet the error is in input params, so add .on('start', onStart) in your code and check logs to see exact command used to validate it ;)
I'm having quite the trouble understanding how fps output works.
I have a video workflow through node and ffmpeg that transform picture into scrolling videos, here is the command :
const ffmpeg = spawn('ffmpeg', ['-f', 'lavfi', '-i', 'color=s=1280x720', '-loop', '1', '-i', `${path}/${video.name}`, '-filter_complex', `[1:v]scale=1280:-2,format=yuv420p,fps=fps=60[fg]; [0:v][fg]overlay=y=-\'t*h*0.02\'[v]`, '-map', '[v]', '-t', `${clipDuration}`, `./${path}/${video.name}-wip.mp4`])
ffmpeg.stderr.on('data', (data) => {
console.log(`${data}`);
});
ffmpeg.on('close', (code) => {
const ffmpeg2 = spawn('ffmpeg', ['-i', `./${path}/${video.name}-wip.mp4`, '-vf', `tpad=stop_mode=clone:stop_duration=3,fade=type=in:duration=1,fade=type=out:duration=1:start_time=${clipDuration + 2}`, `./${path}/${video.name}.mp4`])
ffmpeg2.stderr.on('data', (data) => {
console.log(`${data}`);
});
ffmpeg2.on('close', (code) => {
resolve();
});
})
First ffmpeg command create a scrolling video from picture,
second ffmpeg command add a fade out transition and a pause to this video.
FPS output for this is 25. How can i increase it to 60 so that scrolling isn't stuttering anymore ?
Thanks for your time.
try this
const ffmpeg2 = spawn('ffmpeg', ['-i', `./${path}/${video.name}-wip.mp4`, '-vf', `framerate=fps=60,tpad=stop_mode=clone:stop_duration=3,fade=type=in:duration=1,fade=type=out:duration=1:start_time=${clipDuration + 2}`, `./${path}/${video.name}.mp4`])
Note this from https://superuser.com/questions/1265642/ffmpeg-slideshow-with-crossfade:
ffmpeg -i temp.mp4 -vf "framerate=fps=60" -codec:v mpeg4 out.mp4
In commmand , Use it
ffmpeg -i main.mp4 -vf "framerate=fps=60" -codec:v mpeg4 out.mp4
Version information
fluent-ffmpeg version: 2.1.2
ffmpeg version:4
OS:linux mint
Code to reproduce
var fluent_ffmpeg = require("fluent-ffmpeg");
var mergedVideo = fluent_ffmpeg();
mergedVideo
.mergeAdd('./Video1.mp4')
.mergeAdd('./Video2.mp4')
// .inputOptions(['-loglevel error','-hwaccel vdpau'])
// .outputOptions('-c:v h264_nvenc')
.on('error', function(err) {
console.log('Error ' + err.message);
})
.on('end', function() {
console.log('Finished!');
})
.mergeToFile('./mergedVideo8.mp4', '/tmp');
When I run this code, then I get conversion failed error.
Observed results
Error ffmpeg exited with code 1: Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #3:0
Conversion failed!
I have tried the same conversion using the command line:
ffmpeg -f concat -i textfile -c copy -fflags +genpts merged8.mp4
Where textfile has the following content-
file 'Video1.mp4'
file 'Video2.mp4'
And I was able to concatenate the video file. But I want to get the same result using fluent-ffmpeg.
I've been trying to capture a sequence of pngs from a website using phantomjs or slimerjs, then send the output to /dev/stdout and pipe it to ffmpeg to make a video.
Like so:
phantomjs test.js | ./ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -y -c:v png -f image2pipe -r 10 -i - -c:v libx264 -pix_fmt yuv420p -movflags +faststart test.mp4
test.js:
var page = require('webpage').create();
page.clipRect = { top: 0, left: 0, width: 900, height: 800};
page.viewportSize = { width: 900, height: 800};
var url = 'http://dl.dropbox.com/u/621993/voronoi/voronoi.html';
var frames = 50;
page.open(url, function(){
setInterval(function(){
page.render('/dev/stdout');
if( frames == 0 ) {
phantom.exit();
}
frames--;
}, 100);
});
But i don't get absolutely anything on stdout, tried this on latest ubuntu and debian wheezy with node.js from ports and node.js compiled from git.
phantomjs installed with npm install -g phantomjs
I'm guessing this is a bug, tried looking on their github issue tracker but there are 1000+ open issues, i would like to check before opening a ticket if i'm actually doing things right.
Thank you.
It seems that phantomjs has some troubles of acquiring the complex html/js on the page (very beautiful by the way), if you try other URL for ex. http://www.goodboydigital.com/pixijs/examples/12-2/ and slightly different command and recent ffmpeg it just works ;)
phantomjs test.js | ffmpeg-2.4.2-64bit-static/ffmpeg -y -c:v png -f image2pipe -r 25 -t 10 -i - -c:v libx264 -pix_fmt yuv420p -movflags +faststart test.mp4