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

22 January 2021

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

Pick a star to rate this
Table of Contents

Leave a Reply

Your email address will not be published. Required fields are marked *

Unrelated Content

26 June 2025
Why Self-Hosting Your Livestream Is Safer, Cheaper, and More Flexible Than Commercial Platforms

With livestreaming more popular than ever, platforms like YouTube Live, Twitch, Vimeo, and Facebook Live have become household names. But are they really the best option for everyone? If you're a tech-savvy creator, a privacy-conscious broadcaster, or someone who just wants full control, self-hosting your own livestream server might be a better choice. In this […]

READ MORE
27 November 2020
Motivation behind this website

For every service that is offered for a lot of money on the internet, there's always a free or much cheaper alternative, especially if you embrace open-source software. For live-streaming this is also very much the case. We love that,

READ MORE
23 January 2021
Introduction to the new how-to article in the Knowledge Base.

I wrote a new guide on setting up your own live-stream server. Mostly due to your requests. I took the opportunity to simplify and shorten the guide a bit. If you follow this guide step by step, and use your brain a tiny bit, you'll be fine. This guide has so far been tested on […]

READ MORE
25 January 2021
Optimizing First-Frame Bitrate for HLS

When responding to an HLS request, the streaming server determines which video quality (i.e., ts file) the client will attempt to play before switching to a lower or higher quality video. This switch depends on available bandwidth and device type. Switching to the wrong quality first degrades the user experience considerably. For instance, sending a […]

READ MORE
25 January 2021
Adaptive Bit-rate Streaming

“What is adaptive streaming?” This question often leads to answers filled with technical terms like “HLS”, “MPEG-DASH”, “segments” and “codec“. This article is written for people who need to understand the principles, but don’t need to actually implement the technology. If you are looking for specific technical information on adaptive streaming then I recommend you read up on it on this website and elsewhere.

READ MORE
2 February 2021
About Bradmax Video Player WordPress plugin

Let's talk about our favorite video player plugin for WordPress. Here are some reasons why Bradmax Player is our favorite. And below that are detailed instructions on how to set it up. Setting it up may at first seem a bit awkward, but it really isn't. First you must obviously install the Bradmax Player plugin […]

READ MORE
Copyright © 2025. 
All rights reserved.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram