Nginx: Simple Video on Demand

From OnnoWiki
Jump to navigation Jump to search

Sumber: http://www.helping-squad.com/setup-a-simple-video-on-demand-server-with-nginx-rtmp/


Setup a simple Video on Demand Server with nginx-rtmp

by RobinJack0r · April 1, 2014

The nginx server has many useful options to offer and the rtmp-module extends these even further. Today I will quickly explain how to configure your nginx to supply your viewers with a video on demand system. We will be using flv video files (I had problems getting mp4’s to work correctly, but you might have more luck with that) and I will show you how to setup a simple website that shows you a webplayer with a playlist.

First of all we need to configure the location where our videos are saved, so nginx can find them, lets take a look at an example config file:

rtmp {
    server {
        listen 1935;  
	chunk_size 8192;
		
        application vod {
            play D:\Capture;
        }
    }
}

In this example my vod’s will be saved on my “D:” drive (Windows nginx) in the folder “Capture”, for linux users you will probably want to use something like “/var/users/vod”, or whatever folder you wish to use. Thats all we need from the nginx server part, any flv/mp4 file in the folder can now be viewed using your rtmp servers address: rtmp://yourserverip/application/filename.flv

The configuration sets the application name to vod and yourserverip depends if you want to access the server inside your local network or from the internet. So an example address for the local network would look like this: rtmp://192.168.100.75/vod/video.flv

Again, I had problems using mp4 files with JWPlayer and all local media players I tried, so I would recommend to use flv files. Arut also recommends that they should be indexed with an external program, for seeking abilities. One more thing to notice, if you want to load video files that are located in a sub-folder you have to set the address slightly different:

rtmp://yourserverip/application/subfolder/video.flv 

does NOT work, but

rtmp://yourserverip/application//subfolder/video.flv4

does! And to playback a vod using FFplay for example, you have to specify the app and playpath independent:

ffplay “rtmp://yourserverip app=vod playpath=subfolder/video.flv”

Sub-Sub-Folders are of course also possible:

rtmp://yourserverip/application//subfolder/subfolder2/video.flv

or

ffplay “rtmp://yourserverip app=vod playpath=subfolder/subfolder2/video.flv”

This is a little specialty which I was pointed at by a friendly user and which Arut luckily clarified a bit more, thanks for that! But now let us take a look at an example JWPlayer setup with a quick playlist. You can of course use a different player but the setup might differ in this case, for JWPlayer its pretty simple:

<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>VoD Example</title>
	<script type="text/javascript" src="http://jwpsrv.com/library/YourKeyHere.js"> </script>
	</head>

<body>
Loading the player ...
	<script type="text/javascript">
		jwplayer("myElement").setup({
			height: 360,
			listbar: {
				position: 'right',
				size: 320
			},
			width: 960,
			playlist: [{
				image: "img/test1.jpg",
				file: "rtmp://your.server.ip/vod/flv:test1.flv",
				title: "Test Trailer 1"
			},{
				image: "img/test2.jpg",
				file: "rtmp://your.server.ip/vod/flv:subfolder/test2.flv",
				title: "Test Trailer 2"
			},{
				image: "img/test3.jpg",
				file: "rtmp://your.server.ip/vod/flv:subfolder/subfolder/ test3.flv", 
				title: "Test Trailer 3"
			}]
		});
	</script>
</body>
</html>

We just have to setup a image (not needed, works without), the file location, which is our server address as mentioned earlier and a title we want to show in the player. This page can be hosted by nginx with its http server “module”. The result would look like this: jwplayer

JWPlayer can also use RSS playlists, so you might be able to automate the process so you don’t have to edit the playlist every time you add a new video. In an earlier tutorial, I already showed you in a few steps, how to get started with hls/dash and the nginx-rtmp module. With a combination of these two tutorials you should be able to setup a HLS driven VoD-Server for your Smartphones.




Referensi