Node fluent-ffmpeg killing process kills server - How to start and stop recorder? - node.js

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.

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.

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 :)

Related

Youtube-dl module, getting part file instead of mp4 file when interrupting

When I interrupt the download process (by pressing ctrl + c), instead of getting an mp4 file, I get a part file. I also get the following messages :
Exiting normally, received signal 2.
ERROR: ffmpeg exited with code 255
This is the part of the code :
self.ydl_opts = {}
with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
ydl.download([self.download_url])
Also I forgot to mention that if change the filename from filename.mp4.part to finemame.mp4 I can watch the video normally.
Any help is much appreciated!
That behaviour is wanted since you should be able to restart the download. It will or at least should then continue downloading from where you stopped.
You could try to use
--no-part when using on command line
or set nopart to True when using embedded youtube-dl
This will write the output directly.
I just figured it out, i had to use os.rename(old_title,new_title) where, old_title = video_title+'.part' and new_title = video_title+'.mp4' But still, this is not normal, when I do the same thing via cmd the program returns an mp4, anyway, that works for me!

Node.js - write CSV file creates empty file in production, while OK in Mocha testing

This gist shows a code snippet that dumps an object into a CSV file.
File writing is done using module csv-write-stream and it returns a promise.
This code works flawlessly in all the Mocha tests that I have made.
When the code is invoked by the main nodejs app (a server-side REPL application involving raw process.stdin and console.log invocations as interaction with the user), the CSV file is created, but it's always empty and no error/warning seems to be thrown.
I have debugged extensively the REPL code with node-debug and the Chrome dev tools: I am sure that the event handlers of the WriteStream are working properly: no 'error', all 'data' seems to be handled, 'close' runs, the promise resolves as expected.
Nevertheless, in the latter case the file is always 0 bytes. I have checked several Q&A's and cannot find anything as specific as this.
My questions are:
can I be missing some errors? how can I be sure to track all communications about the file write?
in which direction could I intensify my investigation? which other setup could help me isolate the problem?
since the problem may be due to the presence of process.stdin in the equation, what is a way to create a simple, light-weight interaction with the user without having to write a webapp?
I am working on Windows 7. Node 6.9.4, npm 3.5.3, csv-write-stream 2.0.0.
I managed to fix this issue in two ways, either by:
resolving the promise upon the 'finish' event of the FileWriteStream rather than on the 'end' event of the CSVWriteStream
removing the process.exit() I was using at the end of my operations with process.stdin (this implies that this tutorial page may be in need of some corrections)

after 10M ffmpeg stops saving video on nodejs

I have a nodejs server running using express (express 4). I want to save the video from a few IP cameras on a lab on request. Everything works, but if the video is too long it doesn't get saved (the limit seems to be 11M).
I tried using only the command line:
ffmpeg -i rtsp://192.168.1.189:554/ch01_sub.264 -strict -2 -vcodec
copy -vcodec copy test.mp4
and this works. But I get into trouble as soon as I use node (note that this is in the node parser, no code in express. I get the same error when running the server) :
var child_process = require('child_process');
tmpProcess = child_process.spawn('ffmpeg',['-i','rtsp://192.168.1.189:554/ch01_sub.264','-strict','-2','-vcodec','copy','-vcodec','copy',"test.mp4"],{maxBuffer: 10000});
this runs until test.mp4 is around 11M. tmpProcess is not killed, it keeps running. But after test.mp4 is of certain size I cannot play it back. I get an error saying that "This file contains no playable streams." (this is from Totem, but VLC doesn't work either).
Changing maxBuffer doesn't help. I'm trying to understand what buffer am I overflooding but I cannot get much info from the manual on the api of node.
If ffmpeg produces a lot of (debugging) output on stdout/stderr, you need to make sure that it gets read, or that you call child_process.spawn() with the option { stdio : 'ignore' }.
Otherwise, Node will keep collecting the data and at some point will block (at least, I think it will) the spawned process until all data has been read.
To read the data, attach data event listeners to both tmpProcess.stdout and tmpProcess.stderr. Or use the aforementioned ignore to have Node ignore all the output (which in your case is probably the best solution as you're not interested in the console output).

Trying to launch my node.js program with npm but nothing happens

I am trying to make this slack bot run : https://github.com/lmammino/norrisbot
I am not very skilled with npm and node yet, but I follow his instructions and try to run the bot with the help of the npm start command.
Here's the output I get :
F:\norrisbot>npm start
> norrisbot#1.0.5 start F:\norrisbot
> node bin/bot.js
F:\norrisbot>
No error, but nothing happens either in the console or the slack general channel...
By the way I set up my BOT_API_KEY variable correctly (with the token.js method)
By your command prompt it's clear you're running in Windows. The operations for running Node properly in Windows are different in several ways from Mac/Linux, and a LOT (most?) of developers don't address these because they're on Mac/Linux themselves. Path formats, file locations, how you expose environment variables, and all sorts of things are different in Win.
Try hand-editing bin/bot.js in your locally cloned copy of the repo. Find this line at the end of the file:
norrisbot.run();
Change it to read as follows:
console.log('Running Norris Bot');
norrisbot.run();
console.log('Ran Norris Bot');
I bet you will find that either NEITHER of these lines gets printed, or only one does.
If NEITHER line gets printed, the issue is with the npm command improperly formatting the path to the executable script for Windows users. In that case, try running it as (make sure NodeJS is in your PATH):
node bin/bot.js
If only the FIRST line gets printed, there is almost certainly a bug elsewhere in the module itself. I didn't evaluate all of its code, and I'm not on Windows myself at the moment - I just use it often enough to be aware of its differences. But either way it will get you started on finding the issue, and if it's truly a bug, you can pursue the bug report I see you've already filed in Github.

Jplayer 'play' command after 'pause' with time

Hello i am using jplayer for my solution. I load the media and on ready i use ('pause', time) command.
After that if i use a ('play') command my media starts playing from the beggining.
Is there something i do wrong as on jplayer's dev guide it is said on the play method:
"Open media will play from where the play-head was when previously paused using jPlayer("pause", [time])."
I encountered similar problem, when dealing with lost connection. jPlayer resumed playback from the beginning when a .play() method was called. So, at some point (in my case it was the error handler) I was executing $("#jquery_jplayer_1").jPlayer("play", event.jPlayer.status.currentTime);. It gets current position and orders jPlayer to play from that exact position. Works flawlessly for me.
But, please, provide some code, maybe your problem is completely different.

Resources