Getting started with nginx rtmp

ericwikman edited this page Nov 8, 2018 · 16 revisions

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 zlib1g-dev

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.14.1.tar.gz
sudo tar xzf nginx-1.14.1.tar.gz
cd nginx-1.14.1

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

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:

#user  nobody;
worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  localhost;

        # 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;
        }
    }
}

rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;

        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

Statistics

Navigate your browser to http://localhost:8080/stat to see current streaming statistics, connected clients, bandwidth etc.

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

Publishing video from webcam (Linux)

ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an -f flv rtmp://localhost/myapp/mystream

Publishing video from webcam (MacOS)

ffmpeg -f avfoundation -framerate 30 -i "0" -c:v libx264 -an -f flv rtmp://localhost/myapp/mystream

Playing with ffplay

ffplay rtmp://localhost/myapp/mystream

Publishing and playing with flash

See test/rtmp-publisher directory for test flash applets and html.

Publishing with OBS (Open Broadcast Software)

From OBS settings on the Stream tab change the type to Custom Streaming Server and the URL to rtmp://localhost/myapp and the steam key to your stream name like "mystream"

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.