-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Getting started with nginx rtmp
- Download, build and install
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
- Set up live streaming
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
- Statistics
- Publishing with ffmpeg
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
ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an -f flv rtmp://localhost/myapp/mystream
- Playing with ffplay
ffplay rtmp://localhost/myapp/mystream
- Publishing and playing with flash