Setup Nginx-RTMP on Ubuntu 14.04

RTMP is great for serving live content. When RTMP is paired with FFmpeg, streams can be converted into various qualities. Vultr is great for these applications as they provide fast and dedicated CPU performance. Vultr’s global network also ensures that you can deliver high quality live content with minimal delay. Lets get started!

Installing Nginx and Nginx-RTMP

Install the tools required to compile Nginx and Nginx-RTMP from source.
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
Make a working directory and switch to it.
mkdir ~/working
cd ~/working
Download the Nginx and Nginx-RTMP source.
wget http://nginx.org/download/nginx-1.7.5.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
Install the Unzip package.
sudo apt-get install unzip
Extract the Nginx and Nginx-RTMP source.
tar -zxvf nginx-1.7.5.tar.gz
unzip master.zip
Switch to the Nginx directory.
cd nginx-1.7.5
Add modules that Nginx will be compiled with. Nginx-RTMP is included.
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
Compile and install Nginx with Nginx-RTMP.
make
sudo make install
Install the Nginx init scripts.
sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults
Start and stop Nginx to generate configuration files.
sudo service nginx start
sudo service nginx stop

Installing FFmpeg

Add the FFmpeg PPA.
sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next
Update the package lists.
sudo apt-get update
Install FFmpeg.
sudo apt-get install ffmpeg
Note: The apt-add-repository command may not be installed in some cases. To install it run sudo apt-get install software-properties-common.

Configuring Nginx-RTMP and FFmpeg

Open the Nginx configuration file.
sudo nano /usr/local/nginx/conf/nginx.conf
Append the following.
rtmp {
    server {
            listen 1935;
            chunk_size 4096;

            application live {
                    live on;
                    record off;
                    exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
            }
            application live360p {
                    live on;
                    record off;
        }
    }
}
After you've added the above, you can customize settings such a video bitrate, audio bitrate and resolution. These changes will only be applied to the lower quality stream. To add more qualities, copy and paste the exec ffmpeg line and change the settings. You'll also need to create a new application. You can do this by copying and pasting the live360 example that has been included. Don't forget to update the exec ffmpeg line with the address of the new application. You can do this by changing the final RTMP address in the exec ffmpeg line.
Note: Changing the value after -b:v will change the video bitrate. This is measured in kilobits per second. Changing the value after -b:a will change the audio bitrate. This is measured in kilobits per second. Changing the value after -s will change the resolution.
Save the file by pressing Control and X together. Restart Nginx.
sudo service nginx restart
Note: For best performance, each stream being converted should have its own CPU core. For example two qualities, 360P and 480P are being created from a 720P stream. A Vultr instance with at least two CPU cores should be used.

Security Note

If you're using a firewall, you'll need to make sure TCP 1935 is allowed.
The current configuartion allows anyone to stream to your server. We can fix this by only allowing certain IP addresses the publish permission. Open the Nginx configuration.
sudo nano /usr/local/nginx/conf/nginx.conf
Look for the following lines.
live on;
                record off;
Add the following to each set of the above lines. Change 0.0.0.0 to your IP address.
allow publish 127.0.0.1;
                allow publish 0.0.0.0;
                deny publish all;
The configuration should now look something like this.
rtmp {
    server {
            listen 1935;
            chunk_size 4096;

            application live {
                    live on;
                    record off;
                    allow publish 127.0.0.1;
                    allow publish 0.0.0.0;
                    deny publish all;
                    exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;
            }
            application live360p {
                    live on;
                    record off;
                    allow publish 127.0.0.1;
                    allow publish 0.0.0.0;
                    deny publish all;
        }
    }
}
Save the file by pressing Control and X together. Restart Nginx.
sudo service nginx restart

Configuring Software to Work with Nginx-RTMP

Streaming applications typically have two fields for connection information. The first field is usually for the server information and the second field is usually for the stream name or key. The information that you should place into each field is listed. The stream name or key can be set to anything.
Field 1: rtmp://your.vultr.ip/live/
Field 2: stream-key-you-set
To view streams open the following links in a player supporting RTMP.
rtmp://your.vultr.ip/live/stream-key-you-set
rtmp://your.vultr.ip/live360p/stream-key-you-set
Setting up a player to display live video on a website is beyond the scope of this guide. Searching for the term 'RTMP web player' might assist you.

转贴:https://www.vultr.com/docs/setup-nginx-rtmp-on-ubuntu-14-04
##################################################################
然后用ffmpeg推流到nginx:
第一个是推到了上面配置的myapp上:
ffmpeg -re -i "D:\download\film\aqgy\02.mp4" -vcodec libx264 -vprofile baseline -acodec aac  
 -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://server:1935/  
myapp/test1  
第二个推送到hls上:
ffmpeg -re -i "D:\download\film\aqgy\02.mp4" -vcodec libx264 -vprofile baseline -acodec aac  
 -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://ip:1935/  
hls/test2  

tom@tom:~$ ffmpeg -re -i "/home/tom/liaoning.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.10.60:1935/hls/test2
ffmpeg version 2.4.3-1ubuntu1~trusty6 Copyright (c) 2000-2014 the FFmpeg developers
  built on Nov 22 2014 17:07:19 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
  configuration: --prefix=/usr --extra-version='1ubuntu1~trusty6' --build-suffix=-ffmpeg --toolchain=hardened --extra-cflags= --extra-cxxflags= --libdir=/usr/lib/x86_64-linux-gnu --shlibdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --enable-shared --disable-stripping --enable-avresample --enable-avisynth --enable-fontconfig --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-opengl --enable-x11grab --enable-libxvid --enable-libx265 --enable-libdc1394 --enable-libiec61883 --enable-libzvbi --enable-libzmq --enable-frei0r --enable-libx264 --enable-libsoxr --enable-openal --enable-libopencv
  libavutil      54.  7.100 / 54.  7.100
  libavcodec     56.  1.100 / 56.  1.100
  libavformat    56.  4.101 / 56.  4.101
  libavdevice    56.  0.100 / 56.  0.100
  libavfilter     5.  1.100 /  5.  1.100
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  0.100 /  3.  0.100
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  0.100 / 53.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/tom/liaoning.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.20.100
  Duration: 00:03:13.96, start: 0.000000, bitrate: 2057 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 1926 kb/s, 24.99 fps, 25 tbr, 16k tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 125 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
[libx264 @ 0x1084f00] -qscale is ignored, -crf is recommended.
[libx264 @ 0x1084f00] using SAR=1/1
[libx264 @ 0x1084f00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX AVX2 FMA3 LZCNT BMI2
[libx264 @ 0x1084f00] profile Constrained Baseline, level 3.1
[libx264 @ 0x1084f00] 264 - core 142 r2389 956c8d8 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=0 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, flv, to 'rtmp://192.168.10.60:1935/hls/test2':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.4.101
    Stream #0:0(und): Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 1k tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc56.1.100 libx264
    Stream #0:1(und): Audio: aac ([10][0][0][0] / 0x000A), 44100 Hz, mono, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc56.1.100 aac
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[flv @ 0x10707c0] Failed to update header with correct duration.ate=1949.3kbits/s    
[flv @ 0x10707c0] Failed to update header with correct filesize.
frame= 4847 fps= 25 q=-1.0 Lsize=   46176kB time=00:03:14.00 bitrate=1949.9kbits/s    
video:42778kB audio:3164kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.508861%
[libx264 @ 0x1084f00] frame I:58    Avg QP:19.18  size: 63911
[libx264 @ 0x1084f00] frame P:4789  Avg QP:22.69  size:  8373
[libx264 @ 0x1084f00] mb I  I16..4: 34.6%  0.0% 65.4%
[libx264 @ 0x1084f00] mb P  I16..4:  4.8%  0.0%  0.6%  P16..4: 39.9% 10.4%  3.0%  0.0%  0.0%    skip:41.3%
[libx264 @ 0x1084f00] coded y,uvDC,uvAC intra: 17.6% 49.5% 11.5% inter: 8.4% 14.6% 0.2%
[libx264 @ 0x1084f00] i16 v,h,dc,p: 31% 43% 13% 13%
[libx264 @ 0x1084f00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 36% 16%  4%  5%  4%  7%  4%  5%
[libx264 @ 0x1084f00] i8c dc,h,v,p: 61% 25% 12%  3%
[libx264 @ 0x1084f00] ref P L0: 75.6% 15.2%  9.2%
[libx264 @ 0x1084f00] kb/s:1806.72
tom@tom:~$ 

Comentarios

Entradas más populares de este blog

Stalker / Ministra Middleware install script for Debian and Ubuntu Distros

IPTV CANALES DOMINICANOS

How To Setup Linux Media Server Using Jellyfin