Skip to content

Getting started with nginx rtmp

Avital Yachin edited this page Jul 11, 2017 · 4 revisions
    1. Download, build and install
Build Utilities
    sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

On Amazon Linux:

    sudo yum install git gcc make pcre-devel openssl-devel

Make and CD to build directory (home)

    sudo mkdir ~/build && cd ~/build

Download & unpack latest nginx-rtmp (you can also use http)

    sudo git clone git://github.com/arut/nginx-rtmp-module.git

Download & unpack nginx (you can also use svn)

    sudo wget http://nginx.org/download/nginx-1.12.0.tar.gz
    sudo tar xzf nginx-1.12.0.tar.gz
    cd nginx-1.12.0

Build nginx with nginx-rtmp

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

Start nginx Server

    sudo /usr/local/nginx/sbin/nginx
    1. Set up live streaming
To set up RTMP support you need to add `rtmp{}` section to `nginx.conf` (can be found in PREFIX/conf/nginx.conf). Stock `nginx.conf` contains only `http{}` section.
    sudo nano /usr/local/nginx/conf/nginx.conf

Use this `nginx.conf` instead of stock config:

    error_log  logs/error.log debug;
    
    events {
        worker_connections  1024;
    }
        sendfile        on;
        keepalive_timeout  65;
            # sample handlers
            #location /on_play {
            #    if ($arg_pageUrl ~* localhost) {
            #        return 201;
            #    }
            #    return 202;
            #}
            #location /on_publish {
            #    return 201;
            #}
    
            #location /vod {
            #    alias /var/myvideos;
            #}
    
            # rtmp stat
            location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
            }
            location /stat.xsl {
                # you can move stat.xsl to a different location
                root /usr/build/nginx-rtmp-module;
            }
    
            # rtmp control
            location /control {
                rtmp_control all;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
            application myapp {
                live on;
    
                # sample play/publish handlers
                #on_play http://localhost:8080/on_play;
                #on_publish http://localhost:8080/on_publish;
    
                # sample recorder
                #recorder rec1 {
                #    record all;
                #    record_interval 30s;
                #    record_path /tmp;
                #    record_unique on;
                #}
                # sample HLS
                #hls on;
                #hls_path /tmp/hls;
                #hls_sync 100ms;
            }
            # Video on demand
            #application vod {
            #    play /var/Videos;
            #}
            # Video on demand over HTTP
            #application vod_http {
            #    play http://localhost:8080/vod/;
            #}
        }
    }

Restart nginx with:

    sudo /usr/local/nginx/sbin/nginx -s stop
    sudo /usr/local/nginx/sbin/nginx
    1. Statistics
Navigate your browser to `http://localhost:8080/stat` to see current streaming statistics, connected clients, bandwidth etc.
    1. Publishing with ffmpeg
The easiest way to publish live video stream is using ffmpeg (or avconv). It's already installed on most systems and easy to install on others.

RTMP supports only a limited number of codecs. The most popular RTMP video codecs are H264, Sorenson-H263 (aka flv) and audio codecs AAC, MP3, Nellymoser, Speex. If your video is encoded with these codecs (the most common pair is H264/AAC) then you do not need any conversion. Otherwise you need to convert video to one of supported codecs.

We'll stream test file `/var/videos/test.mp4` to server with ffmpeg.

Streaming without conversion (given `test.mp4` codecs are compatible with RTMP)

    ffmpeg -re -i /var/Videos/test.mp4 -c copy -f flv rtmp://localhost/myapp/mystream

Streaming and encoding audio (AAC) and video (H264), need `libx264` and `libfaac`

    ffmpeg -re -i /var/Videos/test.mp4 -c:v libx264 -c:a libfaac -ar 44100 -ac 1 -f flv rtmp://localhost/myapp/mystream

Streaming and encoding audio (MP3) and video (H264), need `libx264` and `libmp3lame`

    ffmpeg -re -i /var/Videos/test.mp4 -c:v libx264 -c:a libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost/myapp/mystream

Streaming and encoding audio (Nellymoser) and video (Sorenson H263)

    ffmpeg -re -i /var/Videos/test.mp4 -c:v flv -c:a nellymoser -ar 44100 -ac 1 -f flv rtmp://localhost/myapp/mystream
    1. Publishing video from webcam
    ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an -f flv rtmp://localhost/myapp/mystream
    1. Playing with ffplay
    ffplay rtmp://localhost/myapp/mystream
    1. Publishing and playing with flash
See `test/rtmp-publisher` directory for test flash applets and html.
Clone this wiki locally