Knowledge Base

Record your stream and auto-convert to mp4 with FFmpeg after stream stopped

By default Nginx will record in .flv format. If you record often, converting these flv's to mp4 manually each time will become a tedious job. Ain't nobody got time for that! With this one line of code, you'll instruct Ffmpeg to start converting the recorded flv file automatically directly after you stop the stream. It also works for when you manually started a recording and manually stopped it. The output file will be a decently encoded mp4 file.

The line shown below must be added in your application block that handles the recording. Please change the output folder location to anything you prefer. I've chosen to use /tmp/rec as it doesn't require me to set any user rights for the output folder. But if you prefer a different location, please make sure you give read+write rights to www-data user with this command: chown -R www-data:www-data /your/output/folder

exec_record_done ffmpeg -i $path -f mp4 /tmp/rec/$basename.mp4;

Here is an example of a recording block that would go in your nginx.conf with the auto-record line added.

#= RECORDING OPTIONS =
application recorder {	
    live on;
	recorder all {		
	    record all;			# Off / All
	        record_suffix all-%d-%b-%y-%T.flv;
		    record_path /tmp/rec;
		    # record_max_size 4096M;
		    record_unique on;
		    record_append off;
		    record_lock on;
		    }
	exec_record_done ffmpeg -i $path -f mp4 /tmp/rec/$basename.mp4;
}

One things is very important to know. Encoding takes a LOT of processing power. So don't be surprised your server will become a bit slow or even unresponsive during the encoding. If you open task manager or server monitoring you'll notice that it's using 100% CPU for the whole duration of the encoding process. How long this will take will mostly depend on how big the recorded file is, but also on how fast the CPU is and how many cores it has. Just don't be aware of this. So if you decide to stream for 4 hours, you should probably not try to stream again for at least an hour. Best way to monitor the encoding process is just to keep an eye on the CPU usage. There are millions of ways to do this, but what I always do is open Webmin for the server, as it shows a neat little graph that updates in real time. Another idea is to use Netdata.

Webmin dashboard

In case you don't have Netdata or Webmin installed, you can always monitor the server from the command line. Just type in your terminal: top . Use ctrl+c to exit top. See screenshot.

Top
Table of Contents