Live-streaming using a computer and a Ubuntu server to


I started live-streaming my gaming sessions a few months ago and have been using the NVENC encoding. I found that the quality wasn’t that great and I had a Ubuntu server around and I wanted to try to find out what I could do with it to help have better quality on my stream, and maybe record all my sessions with a higher quality.

I also didn’t want to buy a capture card or a Windows license for my server since I’m very comfortable with Ubuntu Server and didn’t have more usable hardware around.

The setup

Ubuntu server

Ubuntu Server 14.04 LTS.
On it is installed nginx-rtmp and FFmpeg.

Main computer

I use this computer for pretty much everything I do.
I use OBS with NVENC encoding.

Settings for the main computer

In OBS, I use NVENC encoding @ 50 Mbps bit rate and I use my monitor’s native resolution (1080p). I do not record on this computer, for some reasons OBS seems to slow down after a few hours while recording (When the recording file is about 100 GB).

Settings for the server

Installing FFmpeg

Notice: ffmpeg is back in Ubuntu 15.04. Therefore, if you have this version, you only need to run the “sudo apt-get install ffmpeg” part.

Ubuntu Server doesn’t come with FFmpeg by default in its repositories. So we need to add Jon Severinsson’s FFmpeg’s ppa.

$ sudo add-apt-repository ppa:jon-severinsson/ffmpeg
$ sudo apt-get update
$ sudo apt-get install ffmpeg

Compiling and installing nginx with the rtmp module

You need to install the prerequisite libraries

$ sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

After you need to download the latest nginx and the rtmp module and unpack them

$ wget
$ wget
$ tar -zxvf nginx-1.7.9.tar.gz
$ unzip
$ cd nginx-1.7.9

Then you need to build nginx including the nginx-rtmp module

$ ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
$ make
$ sudo make install

Configuring nginx

The default location for the configuration file is:

At the end of the file, add the following:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application livein {
            live on;

            exec ffmpeg -i "rtmp://" -vb 1800k -minrate 1800k -maxrate 1800k -bufsize 1800k -s 1280x720 -c:v libx264 -preset faster -r 60 -g 120 -keyint_min 60 -x264opts "keyint=120:min-keyint=120:no-scenecut" -sws_flags lanczos -tune film -pix_fmt yuv420p -c:a copy -f flv -threads 8 -strict normal "rtmp://";

            record_path /your/path;

            recorder allin {
                record all;
                record_suffix -allin-%F-%H-%M-%S.flv;
                record_interval 15m;

        application liveout {
            live on;
            record off;

            push rtmp://TWITCH_ADDRESS;

Basically what will happen here is that you will stream to the livein application, it will execute the FFmpeg command (That we will see in details further down the page) that re-encodes the stream using libx264 with good quality, constant bit rate and the right key interval for Twitch. (Those settings gives an “Excellent” rating by Twitch)

The entry stream will also be recording everything with video files that lasts 15 minutes each (To get highlights it’s usually better to work with smaller files).

You can find a list of’s ingest servers here.

Also, please note that this configuration is for 60 FPS streaming. If you want to lower it to 30 FPS you will need to keep all the key-frame settings in par.

General rtmp settings explained

All possible directives explained

  • record_path: Set this to the path at which you want to record everything.
  • recorder: This is a recorder set
  • record: This is set to all so we get both audio and video into the files.
  • record_suffix: I set it like that so I can easily identify the files using the hour of the day.
  • record_interval: This is set to 15 minutes, it will restart recording each times this interval is reached.

You may want to play with the Access settings (Allow/Deny) to only allow your local network to have access to your “In” stream. You can read more in the nginx-rtmp wiki here.

FFmpeg settings explained

  • -i: This is the input (Your stream, using the right STREAM_KEY)
  • -vb, -minrate, -maxrate, -bufsize: I found out that my stream was a lot more stable when all of those where the same size. -vb is the video bit rate, -minrate and -maxrate needs to be the same for Twitch (Constant bit rate).
  • -s: This is the output resolution you want. Here I transcode from 1080p to 720p.
  • -c:v libx264: The encoder you want to use (I strongly recommend to use libx264)
  • -preset: You can play around with this setting depending on your server. I use the faster preset and it works fine. You could try fastest (To use less CPU) or fast (To use more CPU).
  • -r, -g, -keyint_min 60, -x264opts: -r is your FPS. -g needs to be double your FPS (To have 2 seconds key-frame interval as requested by Twitch). -keyint_min is set to the same as my FPS. The content of -x264opts is to do the same thing about the key-frame interval.
  • -sws_flags: I use this to have a better down-scale quality.
  • -tune: I played around a few of this settings, and had the best experience with film you might need to play around this setting, but film is a safe choice.
  • -pix_fmt: This is the picture format, yuv420p seems to be the most supported/popular one.
  • -c:a: The audio codec is set as copy so it won’t re-encode the stream’s audio.
  • -threads: The number of CPU cores/threads you want to use. I have 8 on my server.

The output needs to be your liveout application with the right STREAM_KEY.

Running the server

You can start the nginx server by running the following command:

$ sudo /usr/local/nginx/sbin/nginx

Or you can add it in your services.

$ sudo wget -O /etc/init.d/nginx
$ sudo chmod +x /etc/init.d/nginx
$ sudo update-rc.d -f nginx defaults
$ sudo service nginx start

Setting up the main computer

Using OBS, in your broadcast settings you need to use the “Custom” streaming service. The FMS URL will be something like rtmp:// (Use your own server’s IP). Your Stream Key needs to be the same you used in your nginx configuration.

In the Encoding part I use Nvidia NVENC (Since it doesn’t use a lot of CPU). I do not have CBR checked and the quality balance is set to 10. My Max bit rate is set to 50000 Kb/s (Since it’s only going to communicate on my local network). You may need to adjust your max bit rate according to your needs and capabilities.

As for Audio encoding, I use the AAC codec, 160 bit rate and a Format of 48 Khz.

Additional read and resources

Bonus script

I have written a script that converts all the flv videos from the recordings into mp4 file format for use in Adobe Premiere.




echo "===================================";
echo "FLV to MP4 mass converter by Manhim";
echo "===================================";

echo "Processing files...";

for f in $FILES

    b=`basename "${f%.*}"`;

    if [ ! -e $t ]
        echo -n "$b: Processing... ";
        echo -e "$b: Processing... \n" >> convert_logs.log;
        ffmpeg -i "$f" -c copy -threads "$THREADS" "$t" >> convert_logs.log 2>&1;
        echo -e "\nDone.\n" >> convert_logs.log;
        echo "Done.";
        echo "$b: Already processed.";


echo "Finished processing files.";

This script will not delete any files and it won’t reconvert files that where already converted (It’s only using file names to compare, but it should be enough). You may delete the flv files yourself after converting them. (I usually like to confirm that the mp4 files are working correctly before deleting the source files)


I hope that I covered everything that was required for this setup to work. If you feel like I forgot anything, please add a comment.

If you have any questions, feel free to add a comment on this blog post, I will be reading and answering them.

  • Agent Colorado

    I cant get ffmpeg, it says that it failed to fetch ffmpeg 404 not found after doing apt-get update

    • Manhim

      I just redid the process and it worked. What version of Ubuntu Server are you using? Also, it’s possible that it was temporarly down, can you retry now?

      • Agent Colorado

        after reading more into it i dont neet the requirements. Atm im using a old quad core 3.0ghz on 14.10 ubuntu server.

        • Actually, this guide was written for 14.04 LTS. I don’t know of there is executables of ffmpeg on 14.10. You could try to compile it manually, or you could try with avconv which comes in the default repositories, although I don’t know much about avconv.

        • You could probably mess around with the settings to make it work. Like using 30fps rather than 60 and running on the fastest preset.

          • Agent Colorado

            Ill give it a try. eventually ill have a much faster rig that ill be setting up for it. (8gb ram 3.8ghz)


  • It is worth noting that in Ubuntu 14, they have replaced ffmpeg with libav (and the command-line tool “avconv”). In Ubuntu 15, ffmpeg is back.

    • This is quite good news. I’ve had problems trying to make the same thing work with avconv, which is why I was getting ffmpeg manually. I’ll add a notice to the guide. Thanks!

  • Pingback: Insufficient permissions trying to edit nginx.conf – Internet and Tecnnology Answers for Geeks()

  • caseyjv

    This is an AMAZING article and helped me to set up my RTMP server. One quick question, how do you use the NVENC encoder (GPU) instead of the x264 encoder (CPU)? I’d like to use the GPU to put out a lower quality stream to Facebook since they only allow 720p 30fps anyway. Then I can reserve the CPU for my higher quality streams

    • Here to get more information:
      I never used it, but it should work, just have to edit the ffmpeg configurations you use for Facebook after the installation process.

      • caseyjv

        I’m a pretty big Linux noob, so it took a while to get this figured out. But, I’ll share my experience for others looking to do this.
        – On the page reference by Marc, go to the section that says “Getting Started with FFmpeg/libav using NVIDIA GPUs”
        – Install the latest FFmpeg
        – Download and install the latest Nvidia driver if you don’t have it
        – Download and install the CUDA toolkit (I’m still a bit confused on this part. You may or may not need it as it sounds like it is already part of the latest FFmpeg version)
        – Nothing more is needed on this page so you can ignore the rest.
        – You can now input your settings (in my case this is for Facebook Live): exec ffmpeg -i “rtmp://” -vb 4000k -minrate 4000k -maxrate 4000k -bufsize 4000k -s 1280×720 -c:v h264_nvenc -preset slow -r 30 -g 60 -keyint_min 30 -x264opts “keyint=60:min-keyint=60:no-scenecut” -sws_flags lanczos -pix_fmt yuv420p -c:a copy -f flv -threads 8 -strict normal “rtmp:”;

        Hopefully this helps someone before they spend as much time as I did trying to figure this out.