Reputation: 31
I'm using fluent-ffmpeg in a node application. Recording from the screen/camera to an mp4 file. Would like a server request to start and another request to stop recording (links to a web interface - testing some tech with a view to making an Electron App later with it).
Starting is fine, but cannot figure out how to stop it.
This is the code to start (to run on MacOS):
recordingProcessVideo = ffmpeg(`${screenID}:none`)
.inputFormat('avfoundation')
.native()
.videoFilters(`crop=${width}:${height}:${x}:${y}`)
.save(filePath);
This is what I thought would stop it from documentation and reading around the subject:
recordingProcessVideo.kill('SIGINT');
However, when I call this command, the server quits with the following ambiguous message:
code ELIFECYCLE
errno 1
Also, the video file produced will not open as if it quit before it completed. Can't seem to work it out, as from the docs and what people have written, to start and stop the recorder should be to make the process, then kill it when ready. Anyone know the correct way - been looking for ages but can't find any answers.
Using Node v10.15.2 and Ffmpeg version V92718-g092cb17983 running on MacOS 10.14.3.
Thanks for any help.
Upvotes: 2
Views: 4413
Reputation: 1
ffmpeg(inputUrl)
.outputOptions('-threads 6') // Limit to 2 threads
.videoCodec('libx264')
.output(outputPath)
.on('start', (commandLine) => {
console.log('FFmpeg process started with command: ' + commandLine);
})
.on('progress', (progress) => {
console.log('Processing: % done',progress);
})
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('end', () => {
console.log('Processing finished');
})
.run();
Upvotes: 0
Reputation: 19
Maybe it will help someone in the future.
To complete the ffmpeg conversion process
You need to run ffmpeg conversion like this as follows:
recordingProcessVideo = ffmpeg(`${screenID}:none`)
.inputFormat('avfoundation')
.native()
.videoFilters(`crop=${width}:${height}:${x}:${y}`)
.save(filePath);
recordingProcessVideo.run();
And then you can disable your conversion command ffmpeg:
recordingProcessVideo.kill();
The key point is the launch method .run() and you need to run it when the template has already been passed to the variable recordingProcessVideo
After such a launch recordingProcessVideo.run();
You will be able to disable recordingProcessVideo.kill();
The bottom line is that, ffmpeg only passes the template to your variable recordingProcessVideo and if you run .run() immediately when creating a template example:
ffmpeg(`${screenID}:none`)
.inputFormat('avfoundation')
.save(filePath);
.run();
Then the variable recordingProcessVideo will be empty.
This is my first comment on this site, do not scold much for mistakes :)
Upvotes: 0
Reputation: 31
I have solved the issue through tracing out all the messages FFMpeg issued in the terminal. For some unknown reason, my installation of FFMpeg throws an error when completing the video and does not correctly close the file. This is happening in the terminal as well, though the error doesn't really display, and ends up with an MP4 that actually works in all video players - even the browser - with the exception of Quicktime, which is what I was using on this occasion. To prevent the error from crashing my Node application, I just needed to add an error handler to the video call. Indeed, I was adding the handler in my original code, but I was adding it to the process and NOT the original call to FFMPeg. So the code which works looks like this (I catch all of the end events and log them in this example).
recordingProcessVideo = ffmpeg(`${screenID}:none`)
.inputFormat('avfoundation')
.videoFilters(`crop=${width}:${height}:${x}:${y}`)
.native()
.on('error', error => console.log(`Encoding Error: ${error.message}`))
.on('exit', () => console.log('Video recorder exited'))
.on('close', () => console.log('Video recorder closed'))
.on('end', () => console.log('Video Transcoding succeeded !'))
.save(file.video);
I have two version of FFMpeg on my laptop and both fail. The official downloaded release installed on my computer (V4.1.1) and the Node packaged version my app is using, which will make distribution via Electron easier, as it won't have the dependency of installing FFMpeg on the local machine running the app (@ffmpeg-installer/ffmpeg). So the reason the video fails to export is some magical reason to do with my laptop, which I have to figure out, but importantly, my code works now and is resilient to this failing now.
Upvotes: 1