nginx rtmp ffmpeg 組合模仿hls直播
前幾天老總說搞了一個局域網內的直播,想到了之前提到的rtmp模塊,抱着試試看的的心態 開幹了
系統環境:
[root@localhost html]# uname -a Linux localhost.localdomain 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:35 EDT 2010 i686 athlon i386 GNU/Linux [root@localhost html]# getconf LONG_BIT 32
NGINX環境:
1、nginx下載地址:
http://nginx.org/download/
本測試環境系統版本是1.4.7
2、nginx模塊rtmp下載地址
https://github.com/arut/nginx-rtmp-module/archive/master.zip
3、多媒體視頻處理工具→ffmpeg下載地址
http://ffmpeg.org/releases/ffmpeg-2.6.1.tar.bz2
首先解釋一些東西
rtmp:RTMP(Real Time Messaging Protocol)實時消息傳送協議是Adobe Systems公司爲Flash播放器和服務器之間音頻、視頻和數據傳輸 開發的開放協議。
然後nginx的rtmp模塊
戰鬥民族俄羅斯人民開發的一款NGINX的流媒體插件,除了直播發布音視頻流之外具備流媒體服務器的常見功能
比如推拉流媒體資源
基於HTTP的FLV/MP4 VOD點播
HLS (HTTP Live Streaming) M3U8的支持
基於http的操作(發佈、播放、錄製)
可以很好的協同現有的流媒體服務器以及播放器一起工作
在線調用ffmpeg對流媒體進行轉碼
H264/AAC音視頻編碼格式的支持
linux/BSD/MAC系統的支持
(來自於網絡)
安裝步驟、
nginx安裝
下載好rtmp模塊
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip unzip master.zip wget http://nginx.org/download/nginx-1.4.7.tar.gz tar xvf nginx-1.4.7.tar.gz cd nginx-1.4.7 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --add-module=/usr/local/src/nginx-rtmp-module-master --with-pcre=/usr/local/src/pcre-8.11 make;make install
ffmpeg安裝
wget http://ffmpeg.org/releases/ffmpeg-2.6.1.tar.bz2 tar xvf ffmpeg-2.6.1.tar.bz2 cd ffmpeg-2.6.1
在這一個編譯的話肯定是會報錯的 具體報什麼錯就是說缺乏東西,需要安裝yasm
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz tar xvf yasm-1.2.0.tar.gz cd yasm-1.2.0 ./configure make;make install cd ffmpeg-2.6.1 ./configure make; make instal
l
改裝的東西都裝好了,現在來改改配置文件
nginx的配置文件需要換一換了 nginx rtmp的模塊裏面有一個nginx的主配置文件
[
root@localhost test]# pwd /usr/local/src/nginx-rtmp-module-master/test [root@localhost test]# ll total 56 -rwxr-xr-x 1 root root 49 Mar 24 03:30 dump.sh -rwxr-xr-x 1 root root 84 Mar 24 03:30 ffstream.sh -rw-r--r-- 1 root root 1245 Mar 24 03:30 nginx.conf -rwxr-xr-x 1 root root 59 Mar 24 03:30 play.sh -rw-r--r-- 1 root root 499 Mar 24 03:30 README.md drwxr-xr-x 2 root root 4096 Mar 24 03:30 rtmp-publisher drwxr-xr-x 4 root root 4096 Mar 24 03:30 www
將上面的主配置文件拷貝到nginx目錄就好
nginx主配置文件如下
worker_processes 1; error_log logs/error.log debug; events { worker_connections 1024; } rtmp { server { listen 1936; application myapp { live on; #record keyframes; #record_path /tmp; #record_max_size 128K; #record_interval 30s; #record_suffix .this.is.flv; #on_publish http://localhost:8080/publish; #on_play http://localhost:8080/play; #on_record_done http://localhost:8080/record_done; } } } rtmp { server { listen 1935; chunk_size 4000; #HLS application hls { live on; hls on; hls_path /usr/local/nginx/html/hls; hls_fragment 5s; } } } http { server { listen 8080; location /hls { # Serve HLS fragments types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root html; expires -1; } } } http { server { listen 8081; location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /html/nginx-rtmp-module/; } location /control { rtmp_control all; } #location /publish { # return 201; #} #location /play { # return 202; #} #location /record_done { # return 203; #} #location /rtmp-publisher { # root /html/nginx-rtmp-module/test; # } location / { root /html/nginx-rtmp-module/test/www; } } }
上傳一個flv格式的文件到html目錄
執行
ffmpeg -re -i sample.flv -vcodec copy -acodec copy -f flv rtmp://192.168.3.105/hls/mystream
然後頁面觀看地址
http://192.168.3.105:8080/hls/mystream.m3u8