網頁全終端視頻流媒體播放器EasyPlayer之使用 nginx 和 rtmp 插件搭建視頻直播和點播服務器

使用 nginx 和 rtmp 模塊 ,可以很容易地搭建一個視頻直播和點播服務器出來。下面我們來看一下具體實施步驟:

1. 安裝 nginx 和 rtmp 模塊

有關 nginx 的編譯和安裝比較簡單,這裏就不介紹了,看參考文獻。這裏提示以下幾點:

(1) 安裝好 nginx 後,配置文件在這裏:

/usr/local/nginx/conf/nginx.conf
(2) 啓動 nginx 的命令:

$ sudo /usr/local/nginx/sbin/nginx -s stop
$ sudo /usr/local/nginx/sbin/nginx

2. 配置 nginx 視頻直播和點播服務

先看一下完整的 nginx 配置文件裏有關視頻點播和直播的配置:

rtmp {
	server {
		listen 1935;
		chunk_size 4096;
		application live {
			live on;
			record off;
		}
		application live2 {
			live on;
			record off;
		}
		# video on demand
		application vod {
			play /var/flvs;
		}
		application vod_http {
			play http://192.168.31.185/vod;
		}
		application hls {
			live on;
			hls on;
			hls_path /tmp/hls;
		}
	}
}
# HTTP can be used for accessing RTMP stats
http {
    server {
	listen      8080;
	# This URL provides RTMP statistics in XML
	location /stat {
	    rtmp_stat all;
	    # Use this stylesheet to view XML as web page
	    # in browser
	    rtmp_stat_stylesheet stat.xsl;
	}
	location /stat.xsl {
	    # XML stylesheet to view RTMP stats.
	    # Copy stat.xsl wherever you want
	    # and put the full directory path here
	    root /path/to/stat.xsl/;
	}
	location /hls {
	    # Serve HLS fragments
	    types {
		application/vnd.apple.mpegurl m3u8;
		video/mp2t ts;
	    }
	    root /tmp;
	    add_header Cache-Control no-cache;
	}
	location /dash {
	    # Serve DASH fragments
	    root /tmp;
	    add_header Cache-Control no-cache;
	}
    }
}

現在來解釋一下里面各行代碼的含義。對於視頻直播服務,如果需要支持多路流輸入的話,很簡單,在 nginx 配置文件裏多配幾個 Application 就只可以了,像下面這樣:

application live {
	live on;
	record off;
}
application live2 {
	live on;
	record off;
}

這樣就可以通過下面的地址來推送直播流,其它觀衆端也可以通過下面的地址來訪問直播流:

rtmp://192.168.31.185/live/test
rtmp://192.168.31.185/live2/test
後面緊跟的 test 關鍵字,可以隨便更換,只要你的推送流和訪問流的地址一樣就可以了。

rtmp 模塊也可以直接支持 VOD 這種視頻點播服務 ,只需要在配置文件裏添加如下內容即可:

# video on demand
application vod {
    play /var/flvs;
}

application vod_http {
    play http://myserver.com/vod;
}

然後把一個 mp4 或是 flv 文件扔到 /var/flvs 目錄下,對於 /var/flvs/dir/file.flv 這個視頻文件,就可以通過下面的網址來訪問了:

http://myserver.com/vod//dir/file.flv

這樣直接在瀏覽器裏就可以通過網頁觀看視頻。對於 mp4 文件,也可以實現 VOD 服務,不過需要的是採用 H.264 和 AAC 格式編碼的 mp4 文件。

3. HLS 直播流的配置

如果需要使用 HLS 來視頻直播,可以直接像配置文件那樣,寫上下面這一段:

application hls {
        live on;
        hls on;
        hls_path /tmp/hls;
}

同時把後面有關 http 訪問的內容寫上:

# HTTP can be used for accessing RTMP stats
http {
	server {
		listen	  8080;
		# This URL provides RTMP statistics in XML
		location /stat {
			rtmp_stat all;
			# Use this stylesheet to view XML as web page
			# in browser
			rtmp_stat_stylesheet stat.xsl;
		}
		location /stat.xsl {
			# XML stylesheet to view RTMP stats.
			# Copy stat.xsl wherever you want
			# and put the full directory path here
			root /path/to/stat.xsl/;
		}
		location /hls {
			# Serve HLS fragments
			types {
				application/vnd.apple.mpegurl m3u8;
				video/mp2t ts;
			}
			root /tmp;
			add_header Cache-Control no-cache;
		}
		location /dash {
			# Serve DASH fragments
			root /tmp;
			add_header Cache-Control no-cache;
		}
	}
}

配好以後,推流可以使用下面的地址:

rtmp://192.168.31.185/hls/movie
movie 關鍵字可以任何替換。對於觀衆端來說,可以有幾種播放方式:

(1) 用 rtmp:

rtmp://192.168.31.185/hls/movie
(2) 用 hls 播放:

http://192.168.31.185:8080/hls/movie.m3u8
這樣就可以看到主播端推出來的流。注意,如果使用 http 方式,則是監聽的 8080 端口,這個是在配置文件裏寫的。

4. 網頁播放器播放

在第二步裏,除了可以直接在瀏覽器裏打開網址來觀看視頻,還可以寫一個網頁,實現像優酷那樣的視頻點播業務。通過使用第三方的播放器,在網頁裏植入該播放器來實現這個功能,比如說使用 EasyPlayer播放器。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章