My ffmpeg function thats running as a shell command isnt working. I think its because 'ffmpeg' isnt really referring to anything. I have the ffmpeg node module in my bundle, but i dont know the execFile command is referring to it here.
Im following aws-lambda-ffmpeg as an example of how to call this particular function. They are referring to 'ffmpeg' as a 64-bit linux build they created from John Vansickle's static FFMPEG builds in their gulp function.
I want to know how to replace 'ffmpeg' with something that will just recognize it like a node_module without having to do the whole gulp static build process. To my understanding the only reason they are doing that is to get the latest build which i really dont need.
If I am wrong and a static build using gulp is needed for another reason please let me know.
function ffmpegProcess(description, cb) {
console.log('Starting FFmpeg');
child_process.execFile(
'ffmpeg',
[
'-y',
'-loglevel', 'warning',
'-i', 'download',
'-c:a', 'copy',
'-vf', scaleFilter,
'-movflags', '+faststart',
'-metadata', 'description=' + description,
'out.' + config.format.video.extension,
'-vf', 'thumbnail',
'-vf', scaleFilter,
'-vframes', '1',
'out.' + config.format.image.extension
],
{
cwd: tempDir
},
function(err, stdout, stderr) {
console.log('FFmpeg done.');
return cb(err, 'FFmpeg finished:' + JSON.stringify({ stdout: stdout, stderr: stderr}));
}
);
}
Related
When my ffmpeg command is built by my node.js application it does not run.
error
Unrecognized option 'ss 3.2 -t 1.9 -i videoplayback.mp4 -vf fps=15,scale=240:-1:flags=lanczos,palettegen palette.png'.
command
ffmpeg -ss 3.2 -t 1.9 -i videoplayback.mp4 -vf \ fps=15,scale=240:-1:flags=lanczos,palettegen palette.png
this is my code
var child_process = require('child_process')
function recordVideo() {
var spawn = child_process.spawn;
var args = [
'-y',
'-ss', '3.2',
'-t', '1.9',
'-i', '../getback/bin/videos/videoplayback.mp4',
'-vf', ' \\ ',
'fps=', '15',
'scale=', '320:-1',
'flags=','lanczos,palettegen palette.png',
];
var ffmpeg = spawn('ffmpeg', args);
ffmpeg.stdout.on('data', function (data) {
console.log(data);
});
ffmpeg.stderr.on('data', function (data) {
console.log('grep stderr: ' + data);
});
ffmpeg.on('close', (code) => {
console.log('child process exited with code ' + code);
});
};
recordVideo();
what is this error?
I think.. 'fps=', '15', In the following code
'=' options a problem.
I am still learning English.
I'm sorry if it was hard to understand.
Node will put a space between every argument in the array you give to child_process.spawn function, as the second argument, when it's building your command. Try replacing the argument pairs like "fps=" and "15" with a single argument: "fps=15".
For future reference one can view the spawned arguments by reading the spawnargs property from the created child process.
I want to create video from image files. So I have installed videoshow module. And configured the same as per the documentaion.
var videoOptions = {
fps: 25,
loop: 5, // seconds
transition: true,
transitionDuration: 1, // seconds
videoBitrate: 1024,
videoCodec: 'libx264',
size: '640x?',
audioBitrate: '128k',
audioChannels: 2,
format: 'mp4',
pixelFormat: 'yuv420p'
}
var images = [
"D:/PROJECTS/Video/storage/1.jpg",
"D:/PROJECTS/Video/storage/2.jpg"
];
app.get("/video", function () {
videoshow(images, videoOptions)
// .audio('song.mp3')
.save('video.mp4')
.on('start', function (command) {
console.log('ffmpeg process started:', command)
})
.on('error', function (err, stdout, stderr) {
console.error('Error:', err)
console.error('ffmpeg stderr:', stderr)
})
.on('end', function (output) {
console.error('Video created in:', output)
})
});
But When I run it shows the error on server
Error: Error: Cannot find ffmpeg
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\fluent-ffmpeg\lib\processor.js:136:22
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\fluent-ffmpeg\lib\capabilities.js:123:9
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\async\dist\async.js:473:16
at next (D:\PROJECTS\Video\node_modules\videoshow\node_modules\async\dist\async.js:5315:29)
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\async\dist\async.js:958:16
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\fluent-ffmpeg\lib\capabilities.js:116:11
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\fluent-ffmpeg\lib\utils.js:223:16
at F (D:\PROJECTS\Video\node_modules\videoshow\node_modules\which\which.js:68:16)
at E (D:\PROJECTS\Video\node_modules\videoshow\node_modules\which\which.js:80:29)
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\which\which.js:89:16
Then I installed ffmpeg using
npm install ffmpeg --save
but not worked. So I tried installing at the global level using
npm install ffmpeg -g
Even installing on my window machine and setting the path of its bin folder in environment variables did not work?
What could be the issue?
Actually, I need to instal the ffmpeg in my system and set it to the environment path variable.
And you don't need to install in nodejs.
npm i videoshow
Just use this command
I'm trying to do a little application with node.js that would run on mac and execute some commands.
I've successfully used spawn to run command lines such as xcodebuild, but xcrun doesn't seems to work when I try to open the iOS Simulator.
I can open on terminal by typing:
xcrun instruments -w 'iPhone 5s (9.2)' -t <template>
But if I use node and try to use spawn like this:
var args = ['instruments', '-w', `iPhone 5s (9.2)`, '-t', 'noTemp'];
var xcrun = spawn('xcrun', args);
So it got me thinking that maybe it had some limitation opening apps? I tried to run:
var args = ['/Applications/Spotify.app'];
var xcrun = spawn('open', args);
And nothing happens. I couldn't find anything related to that. My question is: is there anyway to open apps using node.js spawn? If there is, does someone know what's the problem with my code?
Here's the full code if needed:
var args = ['instruments', '-w', `${fullDevice}`, '-t', 'noTemp'];
var xcrun = spawn('xcrun', args);
xcrun.stdout.on('data', (data)=>{
console.log(data.toString('utf8'));
})
xcrun.on('close', (code) => {
socket.emit({
time: commands.getCurrentTime(),
type: 'success',
log: 'Device booted...'
});
callback();
if (code !== 0) {
console.log(`open process exited with code ${code}`);
}
});
OBS: if I run this piece of code the application doesn't terminate, the program doesn't continue and nothing happens.
EDIT: Changed:
xcrun.on('data', (data)=>{
To:
xcrun.stdout.on('data', (data)=>{
Spawned processes have two separate streams for stdout and stderr, so you will need to listen for data on those objects and not the spawned process object itself:
xcrun.stdout.on('data', function(data) {
console.log('stdout: ' + data.toString());
});
xcrun.stderr.on('data', function(data) {
console.log('stderr: ' + data.toString());
});
The problem was one line above. Not sure why, but there's a socket.emit call that is wrong and actually hold the program's execution.
I need blender command-line access though spawn but keep running into an ENOENT error and it is hard to pinpoint what is missing. The command-line being passed in the sample application works in terminal.
Below are some details about the environment and the sample script I am using.
Environment (OSX El Capitan)
Installed Blender 2.76b with:
brew install Caskroom/cask/blender
Then add alias to bash_profile for terminal access:
alias blender="/Applications/blender/Blender.app/Contents/MacOS/blender"
Test Code
#!/usr/bin/env node
var child_process = require('child_process');
var arguments = [
'-b',
'recipe.blend',
'-o', 'test-#',
'-f', 0
];
console.log("values: ", arguments);
var child = child_process.spawn('blender', arguments);
child.stdout.on('data', function(data) {
console.log('data out: ', data.toString());
});
child.stderr.on('data', function(data) {
console.error('error out: ',data);
});
child.on('close', function(code) {
console.log('closing code: ' + code);
});
// Raw command-line for terminal. (PASS)
// blender -b recipe.blend -o test-# -f 0
Try changing the child_process.spawn('blender', arguments); line to child_process.spawn('/Applications/blender/Blender.app/Contents/MacOS/blender', arguments);. NodeJS won't be using your bash aliases, so unless the blender executable is in its PATH it won't be able to find the executable and will throw an ENOENT.
The other option is adjusting Node's PATH to include the path to the blender executable. You could also pass the fullpath to the blender executable in as an environment variable.
Cheers!
I'm new to Node.js and have figured out how to utilize child.spawn to launch an instance of FFMPEG that is being used to capture live video and send it over to Adobe Media Server via rtmp.
Every example I've seen of FFMPEG being used in conjunction with Node.js has been with a time limited sample, so the child process closes once FFMPEG reaches the end of the file it is converting.
In this case, there is no "end of file".
If I instantiate:
var ffmpeg = child.spawn('ffmpeg.exe', [args]);
it creates the live feed.
I have tried immediately shutting the child process down with a:
setTimeout(function() {
ffmpeg.stdin.resume();
ffmpeg.stdin.write('insert command to echo q to close FFMPEG');
ffmpeg.stdin.end();
});
However, that does not seem to work. I continue to see my rtmp feed on my test box.
Is there any way to pass FFMPEG a shut down command via stdin in Node.js?
Thanks in advance!
Rick
The following code is loaded by my main app.js as a module using the child_process.fork() method:
var spawn = require('child_process').spawn;
var ffmpeg = spawn('C:\\Program Files (x86)\\ffmpeg\\bin\\ffmpeg.exe', ['-y', '-threads', '-0', '-re', '-rtbufsize', '204800000', '-probesize', '4096', '-vsync', '2', '-async', '30', '-f', 'dshow', '-s', '320x240', '-i', 'video=Integrated Webcam:audio=Microphone Array (IDT High Defi', '-c:a', 'libvo_aacenc', '-ab', '48000', '-ar', '22050', '-ac', '2', '-c:v', 'libx264', '-s', '400x300', '-g', '96', '-x264opts', 'bitrate=1200', '-preset', 'ultrafast', '-profile:v', 'baseline', '-pix_fmt', 'yuv420p', '-aspect', '4:3', '-f', 'flv', 'rtmp://server']);
setTimeout(function() {
ffmpeg.stderr.on('data', function() {
ffmpeg.stdin.setEncoding('utf8');
ffmpeg.stdin.write('q');
process.exit();
});
}, 10000);
It was far less complicated than I was making it. The main app.js is a basic HTML page that is served up and uses socket.io to receive an event and its corresponding data. In this case, a 'true' event loads the module.js file which launches a live capture session of FFMPEG, feeds it into a RTMP server, and gracefully shuts down FFMPEG on a timeout of 10 seconds.
My next task is to shut it down via an event triggered from a web interface as opposed to the current testing method of a timeout.
Looking at the task manager in Windows, the FFMPEG process closes as does the secondary node process.
The reason for this is that none of the node-ffmpeg modules that I found supported live streaming via capture input. They appear to be primarily for transcoding existing content. The final outcome of this will ideally be a web based interface that can start and stop FFMPEG. Our use case will be replacing Adobe Flash Media Live Encoder as the source for our Adobe Media Server due to its inability to save standard mp4 files.
You can simply kill it.
ffmpeg.kill(SIGHUB)
or any other kill signal you wish, see http://en.wikipedia.org/wiki/Unix_signal
If I understand your example correctly, you pass all args of the node process to ffmpeg including the stream. In order to get your ffmeg.end() to work you would have to stream directly from your node process. I think that ffmpeg does not stop when it continuously receives data from your camera.
What follows is more or less a final solution to the problem of launching and closing a live FFMPEG session using Node.js:
var spawn = require('child_process').spawn
, fs = require('fs');
function ffmpeg(cmd, opts, callback) {
var p;
//console.log(callback());
if(p == undefined) {
var p = spawn(cmd, opts);
p.stderr.on('data', function(data) {
/*p.stdin.setEncoding('utf8');
p.stdin.write('q');
process.exit()
*/
fs.readFile(__dirname + '/server-state.json', function(error, data) {
if(error) {
console.log(error);
} else {
content = JSON.parse(data);
console.log(content['State']);
if(content['State'] == 'false') {
p.stdin.setEncoding('utf8');
p.stdin.write('q');
process.exit()
}
}
});
});
return p;
}
}
ffmpeg_var = ffmpeg('C:\\Program Files (x86)\\ffmpeg\\bin\\ffmpeg.exe', ['-y', '-threads', '-0', '-re', '-rtbufsize', '204800000', '-probesize', '4096', '-vsync', '2', '-async', '30', '-f', 'dshow', '-s', '320x240', '-i', 'video=Integrated Webcam:audio=Microphone Array (IDT High Defi', '-c:a', 'libvo_aacenc', '-ab', '48000', '-ar', '22050', '-ac', '2', '-c:v', 'libx264', '-s', '400x300', '-g', '96', '-x264opts', 'bitrate=1200', '-preset', 'ultrafast', '-profile:v', 'baseline', '-pix_fmt', 'yuv420p', '-aspect', '4:3', '-f', 'mp4', __dirname + '/IntegrityTest.mp4'], function() {
});
This code is encapsulated in a "module.js" file that is instantiated via a child_process.fork() in the root application.js file. It reads a text file where the 'state' is stored. This state is toggled via a write/read method in the root application. In the on('data') event, it reads the file and if it detects that the state has changed to false, it then shuts down FFMPEG by writing the 'q' command to the stdin.
Outside of refining it by using a database if I were to implement it on a larger scale, I am more than open to feedback regarding a more elegant way to code this.
you need to send EOF after the file is done streaming to ffmpeg. then ffmpeg will finish and shutdown correctly