使用Nginx+ffmpeg搭建直播點播服務器
阿新 • • 發佈:2017-08-16
splay page 媒體 ast 音視頻 spa inf index rtmp直播
- 背景
最近做一個直播平臺項目,調研了一些開源的或者商用的流媒體服務器,包括Live555,Red5,Wowza,ffmpeg,EasyDarwin,FMS等;經過對比考量,因為本次需求相對比較簡單,流協議基於RTMP,因此最終決定選型Nginx+ffmpeg。
- 選型
Nginx + nginx-rtmp-module + ffmpeg
Nginx本身是一個非常出色的HTTP服務器,ffmpeg也是目前最好用的音視頻解決方案,而這兩個框架通過一個nginx的模塊nginx-rtmp-module組合在一起既可以搭建一個相對完善的流媒體服務器。
- 支持的協議
RTMP(Adobe公司的協議,主要用在Flash/AIR平臺)
HLS(http live stream 蘋果公司的協議)
- 環境
系統環境:Centos 6.5
- 步驟
- 官網下載Nginx最新穩定版本 傳送門[http://nginx.org/]
- 下載nginx-rtmp-module 傳送門[https://github.com/arut/nginx-rtmp-module]
- 編譯安裝Nginx
# 創建下載目錄 mkdir /home/download # 進入目錄 cd /home/download # 下載Nginx wget http://nginx.org/download/nginx-1.12.1.tar.gz # 下載nginx-rtmp-module
- 安裝完之後,下一步就是配置nginx.conf文件,下面我貼出自己的配置文件以及重要的配置說明,供大家參考:
1 #user nobody; 2 worker_processes 8; 3 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 #pid logs/nginx.pid; 9 10 events { 11 use epoll; 12 worker_connections 65535; 13 } 14 15 http { 16 include mime.types; 17 default_type application/octet-stream; 18 19 #log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ 20 # ‘$status $body_bytes_sent "$http_referer" ‘ 21 # ‘"$http_user_agent" "$http_x_forwarded_for"‘; 22 23 #access_log logs/access.log main; 24 25 add_header Accept-Ranges bytes; 26 tcp_nopush on; 27 tcp_nodelay on; 28 sendfile on; 29 #tcp_nopush on; 30 31 #keepalive_timeout 0; 32 keepalive_timeout 65; 33 34 #gzip on; 35 36 server { 37 listen 80; 38 server_name localhost; 39 40 #charset koi8-r; 41 42 #access_log logs/host.access.log main; 43 44 location / { 45 root html; 46 index index.html index.htm; 47 } 48 49 ############ yyqailaopo start ############## 50 51 #配置nginx拉流服務 52 location /hls { 53 types { 54 application/vnd.apple.mpegurl m3u8; 55 video/mp2t ts; 56 } 57 root /yyq/hls/; 58 add_header Cache-Control no-cache; 59 } 60 61 ############ yyqailaopo end ############## 62 63 error_page 500 502 503 504 /50x.html; 64 location = /50x.html { 65 root html; 66 } 67 68 } 69 70 ############ yyqailaopo start ############## 71 #在http節點下面(也就是文件的尾部)加上rtmp配置: 72 rtmp { 73 server { 74 listen 1935; # rtmp端口 75 chunk_size 4000; 76 #開啟RTMP直播服務 77 application live { 78 live on; #打開直播 79 record off; #關閉錄制 80 } 81 82 #開啟hls直播服務 83 application hls { 84 live on; #打開直播 85 hls on; 86 hls_path /yyq/hls; #切片路徑 87 hls_fragment 5s; # 切片時間 88 } 89 #開啟RTMP點播服務 90 application vod { 91 play /Users/yyq; #點播資源 92 } 93 application vod_http { 94 play http://localhost:80/vod; 95 } 96 } 97 } 98 ############ yyqailaopo end ##############
View Code - 重啟nginx,服務器配置就完成了??
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
- 現在我們可以使用ffmpeg推送rtmp流到nginx來測試一下
# 推送多個流 ffmpeg -re -i /tmp/ffmpeg_test1.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/live/test1" ffmpeg -re -i /tmp/ffmpeg_test2.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/live/test2"
- 使用vlc播放 rtmp://localhost:1935/live/test1 rtmp://localhost:1935/live/test2
- 現在我們再來使用ffmpeg推送hls流到nginx來測試一下
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/hls/hlstest"
- 使用vlc播放 http://localhost/hls/hlstest.m3u8
??,打完收工了!希望會對大家有些幫助,如果有問題可以留言,盡量解答~~
使用Nginx+ffmpeg搭建直播點播服務器