nginx+ffmpeg搭建rtmp轉播rtsp流的flash服務器
本文概要:
nginx是非常優秀的開源服務器,用它來做hls或者rtmp流媒體服務器是非常不錯的選擇。本文介紹了一種簡易方法快速搭建rtmp流媒體服務器,也叫rtsp轉播,數據源不是讀取文件,而是采用獲取rtspNal流后使用ffmpeg轉播。csdn固本培元:leoluopy@gmail.com
開發環境:centos6.4 (主流的linux服務器選擇,30%以上的市場占有率)
需要這幾個包。
版本號:nginx 1.7.3 openssl 1.0.2 rtmp-module(版本未知)
資源:這些都是公網下載的資源,有需要的朋友,可以給我email或者評論留言。email:leoluopy@gmail.com
注意:先編譯openssl 然后編譯nginx
編譯選項:
./configure --add-module=../nginx-rtmp-module-master --without-http_rewrite_module --with-openssl=../openssl-1.0.2-beta2
一路通過
啟動時可能出現錯誤:
Starting nginx: nginx: [emerg] unknown directive "rtmp" in /etc/nginx/nginx.conf:9
沒有識別rtmp模塊。
另外一種可能,原有centos從源安裝了nginx,它不帶rtmp模塊,需要卸載。
安裝編譯好的nginx后:
進入:
/usr/local/nginx/sbin
./nginx -c /usr/local/nginx/conf/nginx.conf
(注意:后面的需要是絕對地址)
ffmpeg推流 (如果沒有flv文件,可以用264,ts這些文件轉換,ffmpeg的常用命令本博客另一篇文章也有簡要介紹)
ffmpeg -re -i IpCam.flv -f flv rtmp://localhost/myapp/test1
ffmpeg -re -i IpCam.flv -f flv -b 20000000 rtmp://localhost/myapp/test1
實時rtsp轉rtmp直播流(其實轉了rtmp之后會多幾秒鐘的延遲,不同客戶端還不一定一致,我們這里測試的是JWplayer 3秒左右(后面用了個flex的方案,時間延遲提升到2s),穩定時可以在1秒左右,vlc播放器更長了10秒 ~_~! ,客戶端的話還是就用rtsp吧,這樣很快,一般延遲可以忽略不計,rtmp本來就是為web端設計的)
rtsp流的獲取方法可以參考另外一篇文章:http://blog.csdn.net/gubenpeiyuan/article/details/19072223
ffmpeg -re -i RealStream.fifo -f flv -b 20000000 rtmp://localhost/myapp/test1
RealStream.fifo 是創建的fifo。 命令: mkfifo RealStream.fifo
啟動rtsp獲取Nal添加sps和pps位,之后打開fifo實時寫入即可。
使用vlc測試如下:
此后可以將實時流推送到rmp了。
我的方案是live555接收流,串流方式推送至fifo,做rtmp的轉發。可能有些延遲,哪位朋友有更好的方案,歡迎交流評論。
其框架如下:非常簡單,并容易集成入大流媒體框架。
Flex方案截圖:
附錄:
RTMP配置如下:
[plain] view plain copy
- #user nobody;
- worker_processes 1;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid;
- events {
- worker_connections 1024;
- }
- rtmp {
- server {
- listen 1935;
- 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;
- }
- application hls {
- live on;
- hls on;
- hls_path /tmp/app;
- hls_fragment 5s;
- }
- }
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #access_log logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #gzip on;
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- root html;
- index index.html index.htm;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http://127.0.0.1;
- #}
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443 ssl;
- # server_name localhost;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_cache shared:SSL:1m;
- # ssl_session_timeout 5m;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
參考文章:
nginx的RTMP協議服務器
http://www.cnblogs.com/aHuner/p/3247068.html
用開源nginx-rtmp-module搭建flash直播環境
http://www.rosoo.net/a/201307/16696.html
http://blog.csdn.net/cjsafty/article/details/7922849
基于nginx的hls直播系統(記錄了rtmp的問題和解決方法)
http://blog.csdn.net/cjsafty/article/details/9108587
C++實現RTMP協議發送H.264編碼及AAC編碼的音視頻
http://www.cnblogs.com/haibindev/archive/2011/12/29/2305712.html
Centos安裝FLASHPlayer插件
http://www.linuxidc.com/Linux/2013-03/81073.htm
ffmpeg推流
ffmpeg -re -i ~/2012.flv -f flv rtmp://192.168.1.102/myapp/test1
ffmpeg -re -i IpCam.h264 -vcodec copy -f flv -b 20000000 rtmp://localhost/myapp/test1
from:http://blog.csdn.net/gubenpeiyuan/article/details/38089013RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成