You are on page 1of 13

LeaseWeb labs

A blog about web hosting and software development

Streaming Video on Demand with nginx and
RTMP Module
In this tutorial we will show a simple way to stream different types of video files
using the Nginx server and RTMP modules.
Before starting the work, we will try to offer a small overview over the advantages
of using RTMP versus HTTP video delivery.
Real Time Messaging Protocol (RTMP) was initially developed by Macromedia, for
streaming on-demand and live media to Adobe Flash applications. It is a TCP-based
protocol which maintains persistent connections and is defined as a “stateful”
protocol. This means that from the first time a client connects until the time it
disconnects, the streaming server keeps track of the client’s actions. On the other
side, HTTP is a “stateless” application protocol used to deliver hypermedia
information across the Internet worldwide.
Advantages of HTTP over RTMP:
Less likely to be blocked by firewalls at different levels in the network; RTMP uses
default port 1935, which can be blocked sometimes, especially within corporate
firewalls
Supported by more CDNs (it supports easier mirroring and edge caching)
More expertise in customizing HTTP
Advantages of RTMP over HTTP:
Ability to provide multicast support
Security and IP protection, using TLS/SSL or RTMPE
Seeking: particularly advantageous for long-duration content because the viewer
doesn’t have to wait for the video file to load before jumping ahead, as is the case

4.bkp nano nginx. we will compile the Nginx source code together with RTMP modules on a Ubuntu 12. To achieve this. This configuration is suggested by the documentation of the RTMP module. Logged in as root: 1 2 3 cd ~ mkdir nginx cd nginx Install the necessary software and dependencies: 1 2 3 4 5 6 7 8 # for compiler and git apt­get install git gcc make   #for the HTTP rewrite module which requires the PCRE library apt­get install libpcre3­dev   # for SSL modules apt­get install libssl­dev Clone Nginx RTMP module source code from Github: 1 git clone https://github.3.04 box.3 Let’s compile Nginx: 1 2 3 . 1 2 3 4 5 worker_processes  1;   events {     worker_connections  1024; } . let’s start to build our streaming server. At the time when we are writing this blog post.4.conf and paste the configuration below.4.org/download/nginx­1.tar.gz cd nginx­1.3. the current version is 1.conf nginx. the buffer will begin filling to avoid any disruption in video or audio flow And now.for HTTP-delivered video Reconnect: if there is a network disruption.4.tar./configure ­­add­module=/root/nginx/nginx­rtmp­module/ ­­with­http_ssl_module ­­prefix=/usr/local make make install Now it is the time to create a configuration file for Nginx.3: 1 2 3 wget http://nginx.cong. Go to: 1 2 3 cd /usr/local/nginx­streaming/conf mv nginx.gz tar zxpvf nginx­1.com/arut/nginx­rtmp­module Download the Nginx source code and extract it from the archive. when the client reconnects. while the video continues to play from the buffer. the client can re-establish a connection.

the server can find some mp4 files in /var/mp4s or flv files in /var/flvs. right click and save it in /var/mp4s with the name sample.xsl /var/www/ And (re)start the Nginx server with: 1 /usr/local/nginx­streaming/sbin/nginx It’s time to play! As we defined in the Nginx configuration.mp4.xsl {             # XML stylesheet to view RTMP stats.apple.log;       server {         # in case we have another web server on port 80         listen      8080;           # This URL provides RTMP statistics in XML         location /stat {             rtmp_stat all;             rtmp_stat_stylesheet stat.xsl;         }           location /stat. .xsl wherever you want             # and put the full directory path here             root /var/www/;         }           location /hls {             # Serve HLS fragments             types {                 application/vnd. You can download an mp4 file here.xml in the folder specified in the configuration above. we must to copy stats. 1 2 mkdir /var/www cp /root/nginx/nginx­rtmp­module/stat.mpegurl m3u8;                 video/mp2t ts;             }             alias /tmp/app;             expires ­1;         }     } } To be able to see some nice statistics about the streaming.log;     error_log /var/log/nginx/error­streaming.             # Copy stat.6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57   rtmp {     server {         listen 1935;           chunk_size 4000;           # video on demand for flv files         application vod {             play /var/flvs;         }           # video on demand for mp4 files         application vod2 {             play /var/mp4s;         }     } }   # HTTP can be used for accessing RTMP stats http {     access_log /var/log/nginx/access­streaming.

The html code below shows how to implement RTMP using Flowplayer. Below we will show how to use Flowplayer to achieve this.mp4 Where <IP_OF_THE_SERVER> is the IP or the hostname of the server where we installed Nginx. Media->Open Network Stream.12.png. you can use a flash player as Flowplayer or JW Player.2. From the VLC menu.rtmp-3. First we need to download flowplayer. In the URL field we input: 1 rtmp://<IP_OF_THE_SERVER>:1935/vod2/sample. And click play.swf (see the download link at the bottom of the page). if you have a video player which supports RTMP protocol (eg: VLC). You should just replace the /path/to/background. If you want to show this video on a website. the <IP_OF_THE_SERVER> and the .Now. you can play it directly there.

2.org/js/flowplayer­3.flowplayer./path/to/flowplayer. rtmp.org/swf/flowplayer­3.2. {     clip: {         url: 'sample.16. nginx. video 30 thoughts on “Streaming Video on Demand with nginx and RTMP Module” Pingback: How to do VoD with nginx | Răzvan Tudorică Yoga Darma .       plugins: {         hddn: {             url: "/path/to/flowplayer.         provider: 'hddn'     }.               // netConnectionUrl defines where the streams are found             netConnectionUrl: 'rtmp://<IP_OF_THE_SERVER>:1935/vod2'         }     }.mp4'. streaming.12.12. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <html> <head>     <!­­ flowplayer javascript component ­­>     <script src="http://releases. Of course the solution presented in the post above is minimalist and if you need to scale globally with your Video on Demand product.js"></script> </head>   <body> <div id="player" style="width:644px;height:276px;margin:0 auto;text­align:center">     <img src="/path/to/background.min.png" height="260" width="489" /></div> <script>   $f("player".         scaling: 'fit'.          November 20.     canvas: {         backgroundGradient: 'none'     } }); </script> </body> </html> That’s all. probably you need a CDN solution. "http://releases. Tools  cdn.rtmp-3.flowplayer.12.2.rtmp­3.2.swf. 2013  Razvan Tudorica (Innovation Engineer)  System Administration.swf".swf".

It’s possible to secure the streaming? Maybe with htaccess or whatever… inan . nice tutorial – great job. that’s make me curious. so my configuration will be the same as this tutorial’s configuration. 2013 at 9:20 am @Maurits i only follow this tutorial. i’m trying to make streaming VOD that running on web. 2013 at 9:29 am @Razvan Tudorica : maybe i should try the HLS streaming. 2013 at 10:11 am @Yoga: Please share some more information about you Nginx configuration. 2013 at 9:14 am hi. thank you so much inan January 12. Pingback: HLS Video on Demand streaming | Răzvan Tudorică Yoga Darma December 6. can you please help me? sorry i can’t speak English well. without that it is hard to help you. can you tell me what is the difference? and why my HTML code is not working? i doing all your instruction in this tutorial. 2014 at 10:28 pm Hey.December 4. i’m using your HTML code but it doesn’t want to running the video. but when i using vlc it’s working. am i doing something wrong ??if yes can you tell me what it is?? thank you so much Yoga Darma December 6. thank you Maurits van der Schee (Innovation Engineer) December 4.

wordpress. i solved the Problem.January 12. 2014 at 8:54 am Hello.com/2013/06/09/secure-links-in-nginx-rtmp/ written by the author of the rtmp modules. david February 27. needed by `default’. 2014 at 10:49 pm Hey me again When i’m typing the command make i get a error like this: make: *** No rule to make target `build’. I was in the wrong directory It’s possible to restream from my Live stream from my VLC Server? razvan January 22. 2014 at 10:33 pm . 2014 at 10:55 am Hi inan. 2014 at 6:26 pm When I stream from OBS to my nginx rtmp VPS server occasionally it’ll lose contact with nginx. Jamie M. You can secure your stream as described in this blog post http://rarut. OBS reconnects automatically and starts streaming again like a champ. Can you let me know any specific way to test and see what’s going on with nginx? -Jamie M. Stop How can i solve this problem? inan January 13. The only way to get it to connect back to nginx is to “sudo killall -9 nginx” then “sudo /usr/local/nginx/sbin/nginx” and tada. February 17.

Great tutorial thanks. nginx: [emerg] unknown directive “worker_processes ” in /usr/local/nginxstreaming//conf/nginx. I have setup nginx with the rtmp-module on an Ubuntu machine..Hi. how to make nginx start on startup? without making it manually.. Pingback: ggkf Pingback: Links interesantes del mes! | Gustavo Alejandro Pilla Pingback: How to do VoD with nginx | Razvan Tudorica Pingback: HLS Video on Demand streaming | Razvan Tudorica WowzaAdmirer October 14. only problem I have is when I try to start the NGINX server I get the following error.conf:1 Any ideas why. I’ve been using WMSPanel which is a reporting panel for media servers and now I see that they support reporting for nginx-rtmp-module. I hope this will be useful for you guys: https://wmspanel. 2014 at 7:03 pm Hey there. 2014 at 11:55 am Hi . Right now my rtmp section looks like this and works perfect: .com/nginx Tim Beyer December 14.? Mazen April 16. 2014 at 10:55 am Thanks for a post.

If the streamer disconnects it plays again randomly files from the above folder. to play randomly local files from the server. application live { live on. } } } The thing is I don’t want it to drop the stream when someone disconnects with OBS. 2014 at 1:29 am . Tim Beyer December 16. Instead I want to hold it at least with a black screen. 2014 at 9:37 am @Tim: Thanks for your comment. It seems like you have a good setup already. record off. Now I have for testing purposes a . image or better. Thanks in advance! Best.rtmp { server { listen 1935. Tim Maurits van der Schee (Innovation Engineer) December 15. but haven’t been able yet to make it working. Hopefully you’ve an idea how to do this. What player are you using? Can’t you implement a smart player that detects the stream (un)availability and gets another playlist on failure? That is probably the way that I would go. Could you maybe help me out on this one? In general I want to make it stream 24/7 – if someone connects with a livestream it stops the videofile playing and let’s the live-stream through.mp4 uploaded to var/mp4s. chunk_size 4096. push rtmp://server/app/key.

 2015 at 6:27 am @Ioannis: Thank you for this link. a single command and you are done Maurits van der Schee (Innovation Engineer) July 26. 2014 at 8:47 pm How can I configure the rtmp to rtmps? Maurits van der Schee (Innovation Engineer) December 30. 2015 at 4:41 am Hello sir. I know FMS and Red5 support RTMPS. but I am not aware of a solution for Nginx. I’m going to have a look at it! Rizal July 30.Hi. Maurits – I’m streaming the content to Twitch. Keep us posted on the solution! Arjun April 22. 2015 at 8:47 pm Hi. how to run video streaming rtmp on xbmc/kodi ? thx .GR-MEDIA-SERVER Easier. 2015 at 10:54 am Do anyone know how to get the published stream name in config file? Ioannis July 25.com/upggr/UPG. Try my project for this https://github. 2014 at 1:07 am @Robin: Thank you for your comment. I suppose just terminating the SSL in a reverse proxy setup won’t work or did you not try that (yet)? Sorry that I can’t help you better. So unfortunately I have no access to the player… :/ Robin December 29.

neither through VLC nor web player. Nginx server is working fine as it is showing me default Nginx page on localhost.Thor September 17.log. worker_processes 1. user www-data www-data. } . events { worker_connections 1024. Anyone end up finding out how to make the application automatically play files from /var/mp4s when there a live feed isn’t active? Harpreet November 6.log info. I followed the instructions and setup Nginx-rtmp on my ubuntu 14.log notice.pid. error_log /var/log/nginx/nginx_error. #pid logs/nginx.04 system. #error_log logs/error. But RTMP is not working. 2015 at 9:10 pm I’m trying to figure out what @Tim was trying to do. Following is my config : #user nobody. chunk_size 4000. } rtmp { server { listen 1935. #error_log logs/error. # video on demand for flv files application vod { play /var/flvs. 2015 at 9:08 pm Hi. Thanks for such details information.

log.log. } alias /tmp/app. } } } When I try to stream it through VLC it gives me following error : Your input can’t be opened: . error_log /var/log/nginx/http_error. # This URL provides RTMP statistics in XML location /stat { rtmp_stat all.apple.mpegurl m3u8. server { # in case we have another web server on port 80 listen 8000. video/mp2t ts.xsl wherever you want # and put the full directory path here root /var/www/.xsl. } } } # HTTP can be used for accessing RTMP stats http { access_log /var/log/nginx/http_access. # Copy stat.# video on demand for mp4 files application vod2 { play /var/mp4s.xsl { # XML stylesheet to view RTMP stats. } location /hls { # Serve HLS fragments types { application/vnd. } location /stat. expires -1. rtmp_stat_stylesheet stat.

30.0. thanks . Please help me to figure it out. 2015 at 5:43 am Hi. Check the log for details. but i have a question if you might can help me.mp4’. suppose if a user open a vlc Player and wants to Play the VIdeo rtmp://10. I am looking for a way to deal with MXF Files to first convert into MP4 and then Play within VLC Player. this is really a good tutorial about the VOD on Demand. Thanks Harpreet Gill kunal November 22.345:1935/vod/video-ID01 can we do that ? any help will be really Helpful.VLC is unable to open the MRL ‘rtmp://localhost:1935/vod2/sample.