首頁  >  文章  >  後端開發  >  【轉載】利用Nginx搭建http和rtmp協定的串流伺服器

【轉載】利用Nginx搭建http和rtmp協定的串流伺服器

WBOY
WBOY原創
2016-08-08 09:28:561595瀏覽

源自//http://blog.chedushi.com/archives/6532?utm_source=tuicool

利用Nginx搭建http和rtmp協定的串流伺服器 
 
實驗目的:
讓Nginx支援flv和mp4格式文件,同時支援Rtmp協定;同時開啟rtmp的hls功能
資料:
HTTP Live Streaming(縮寫是HLSLS)是HLSLS)是HLSLS)是HLSLS)是一個由蘋果公司提出的基於HTTP的串流網路傳輸協定。
HLS只請求基本的HTTP封包,與即時傳輸協定(RTP)不同,HLS可以穿過任何允許HTTP資料通過的防火牆或代理伺服器。它也很容易使用內容分發網路來傳輸媒體串流。
使用ffmpeg來完成對flv、mp4、mp3等格式的轉換(點播實驗暫時不測試)
一、準備工作
模組:nginx_mod_h264_streaming(支援h264編碼的視訊)
模組:http_flv_module 支援flv_module 支援
模組:nginx-rtmp-module支援rtmp //2015-3-5 add
下載位址:
http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.ta​​r.gz
http:/ /nginx.org
https://github.com/arut/nginx-rtmp-module
1、安裝依賴套件:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64
2、安裝git工具:
#mkdir soft-source
#cd soft-source
#wget http://www.codemonkey.org. git-snapshots/git/git-latest.tar.gz 
#tar xzvf git-latest.tar.gz
#cd git-2013-02-04
#autoconf
#./configure
make &instad make git –version
git version 1.8.1.GIT
#cd ..
3、安裝ffmpeg及其相依性套件:
++++++++Yasm+++++++++++
#wget http://www.tortall.net/projects+++++++++++
#wget http://www.tortall.net/projects+++++++++++
#wget http://www.tortall.net/projects+++++++++++
#wget http://www.tortall.net/projects+++++++++++
#wget http://www.tortall.net/projects /yasm/releases/yasm-1.2.0.tar.gz 
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
#./configure
#make
#make install
# ..
++++++++x264+++++++++++
#git clone git://git.videolan.org/x264 
#cd x264
#./configure –enable-shared
#make
make install# ..
++++++++LAME+++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz 
#tar xzvf lame-3.99. 5.tar.gz
#cd lame-3.99.5
#./configure –enable-nasm
#make
#make install
#cd ..
++++++++libogg+++++++++++ /downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz 
#tar xzvf libogg-1.3.0.tar.gz
#cd libogg-1.3.0
#./configure
#make
#make
#make install
#cd ..
++++++++libvorbis+++++++++++++
#cd ..
++++++++libvorbis+++++++++++++
#cd ..
++++++++libvorbis+++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz 
#tar xzvf libvor-5 1.3.3.tar.gz
#cd libvorbis-1.3.3
#./configure
#make
#make install
#cd ..
++++++++libvpx+++++++++
#gitclone http:// git.chromium.org/webm/libvpx.git 
#cd libvpx
#./configure –enable-shared
#make
#make install
#cd ..
++++++FAAD22++++++++ ://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.ta​​r.gz 
#tar zxvf faad2-2.7.ta​​r.gz
#cd faad2-2.7
.
#make
#make install
#cd ..
++++++++FAAC+++++++++++++
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac.net/project/faac/faac-src/faac-1.28/faac-1. .tar.gz 
#tar zxvf faac-1.28.tar.gz
#cd faac-1.28
#./configure
#make
#make ++ll
#++++++++++++++++++++++++++ wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz 
#tar zxvf xvidcore-1.3.2.tar.gz
#cd xvidcore/build/generic
#./configure
#cd xvidcore/build/generic
#./configure
# make
#make install
cd ..
#git clone git://source.ffmpeg.org/ffmpeg 
#cd ffmpeg
#./configure –prefix=/opt/ffmpeg/ –enable-version3 –enable-libvx –mpeg enable-libfaac –enable-libmp3lame –enable-libvorbis –enable-libx264 –enable-libxvid –enable-shared –enable-gpl –enable-postproc –enable-nonfree –enable-avfilter –enable-pthreads
#make&ake
#cd ..
修改/etc/ld.so.conf如下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local /lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfig
二、安裝Nginx相關模組
#tar zxvf nginx_mod_h264_streaming-2.2.7.ta​​r.gzgit /arut/nginx-rtmp-module.git
#tar zxvf pcre-8.12.tar.gz
#cd pcre-8.12
#./configure
#make && make install
#tar zxvvf nginx-1.2. gz
#cd nginx-1.2.6🎜#./ -http_mp4_module –add-module=../nginx-rtmp-module –add-module=../nginx-rtmp-module/hls –with-cc-opt=-I/opt/ffmpeg/include –with-ld-opt='-L/opt/ffmpeg/lib -Wl,-rpath=/opt/ffmpeg/lib'🎜#make && make install 🎜三、修改nginx主設定檔,設定虛擬主機(這裡我們暫時只設定和測試點播,直播請看借鏡位址):🎜user nobody nobody;🎜worker_processes 4;🎜error_log logs/nginx_error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
#rtmp_auto_push on;
rtmp {
server {
listen 1935;
application vod {
play /opt/media/nginxrtmp/flv;
}
}
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m ;
limit_conn_zone $binary_remote_addr zone=perip:256k;
limit_conn_log_level notice;
sendfile on;
tcp_nopush on;
keepalive_timeout 6000;#测试并发临时调大
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#log format
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
#支持flv
server
{
listen 8081;
server_name 192.168.0.33;
root /opt/pub/media/nginx; #http协议时候,flv视频位置
location ~ .*.(flv|swf|mp4|wma|wmv)$ {
valid_referers none blocked *.xxxx.com http://localhost;
if ($invalid_referer) {
return 403;
}
}
location ~ \.flv$ {
flv;
limit_conn one 20;#限制客户端并发连接数
limit_rate 200k;#限制每客户端最大带宽
}
location ~ \.mp4$ {
flv;
limit_conn one 20;
limit_rate 200k;
}
access_log logs/nginxflv_access.log main;
}
server
{
listen 8082;
server_name 192.168.0.33;
index index.html;
location / {
root /opt/pub/media/nginx-rtmp;
}
access_log logs/nginxrtmpflv_access.log main;
}
server {
listen 8080;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /opt/pub/soft/nginx-rtmp-module;
}
location / {
root /opt/pub/soft/nginx-rtmp-module/test/rtmp-publisher;
}
}
http://192.168.0.33:8080/stats 查看rtmp客户请求信息
http://192.168.0.33:8081/index.html 查看nginx http协议时候的flv视频
http://192.168.0.33:8082/index.html 查看nginx rtmp协议时候的flv视频
四、基本功能测试:
播放器采用开源播放器jwplayer,将播放器放到index.html同级目录下
NGINX-Http测试页面index.html(切记http的时候需要给视频添加关键帧)




test of nginx-http




=”p player.swf” />


value=”6.0.65.0″ />
”player.swf” 寬度=”480″ 高度=”360″ 質量=”高” PLUGINSPAGE=”http://www.macromedia.com/shockwave/download/ i
ndex.cgi?P1_Prod_Version=ShockwaveFlash” flashwaveFlash” flashvars =”type=http&file=http://192.168.0.33:8081/2.flv&autostart=true” wmode=”opaq
ue”/>
ue” />

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn