nginx+ffmpeg搭建rtmp转播rtsp流的flash服务器

来源:互联网 发布:脸书注册网络错误 编辑:程序博客网 时间:2024/05/17 07:45

nginx+ffmpeg搭建rtmp转播rtsp流的flash服务器

    <div class="article_manage clearfix">    <div class="article_l">        <span class="link_categories">        标签:          <a href="http://www.csdn.net/tag/nginx" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">nginx</a>        </span>    </div>    <div class="article_r">        <span class="link_postdate">2016-01-23 11:04</span>        <span class="link_view" title="阅读次数">1272人阅读</span>        <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>        <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('nginx%2bffmpeg%e6%90%ad%e5%bb%bartmp%e8%bd%ac%e6%92%adrtsp%e6%b5%81%e7%9a%84flash%e6%9c%8d%e5%8a%a1%e5%99%a8','50568999');return false;" title="收藏" target="_blank">收藏</a></span>         <span class="link_report"> <a href="#report" onclick="javascript:report(50568999,2);return false;" title="举报">举报</a></span>    </div></div>    <style type="text/css">                .embody{            padding:10px 10px 10px;            margin:0 -20px;            border-bottom:solid 1px #ededed;                        }        .embody_b{            margin:0 ;            padding:10px 0;        }        .embody .embody_t,.embody .embody_c{            display: inline-block;            margin-right:10px;        }        .embody_t{            font-size: 12px;            color:#999;        }        .embody_c{            font-size: 12px;        }        .embody_c img,.embody_c em{            display: inline-block;            vertical-align: middle;                       }         .embody_c img{                           width:30px;            height:30px;        }        .embody_c em{            margin: 0 20px 0 10px;            color:#333;            font-style: normal;        }</style><script type="text/javascript">    $(function () {        try        {            var lib = eval("("+$("#lib").attr("value")+")");            var html = "";            if (lib.err == 0) {                $.each(lib.data, function (i) {                    var obj = lib.data[i];                    //html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";                    html += ' <a href="' + obj.url + '" target="_blank">';                    html += ' <img src="' + obj.logo + '">';                    html += ' <em><b>' + obj.name + '</b></em>';                    html += ' </a>';                });                if (html != "") {                    setTimeout(function () {                        $("#lib").html(html);                                              $("#embody").show();                    }, 100);                }            }              } catch (err)        { }    });</script>  <div class="category clearfix">    <div class="category_l">       <img src="http://static.blog.csdn.net/images/category_icon.jpg">        <span>分类:</span>    </div>    <div class="category_r">                <label onclick="GetCategoryArticles('6079159','zhangxiao93','top','50568999');">                    <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">Nginx<em>(18)</em></span>                  <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">                  <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">                    <div class="subItem">                        <div class="subItem_t"><a href="http://blog.csdn.net/zhangxiao93/article/category/6079159" target="_blank">作者同类文章</a><i class="J_close">X</i></div>                        <ul class="subItem_l" id="top_6079159">                                                    </ul>                    </div>                </label>                        </div></div>

目录(?)[+]

  1. 原文链接httpblogcsdnnetgubenpeiyuanarticledetails38089013
  2. 本文概要

原文链接:http://blog.csdn.net/gubenpeiyuan/article/details/38089013

本文概要:

        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


  1. #user  nobody;  
  2. worker_processes  1;  
  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.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15. rtmp {    
  16.     server {    
  17.         listen 1935;    
  18.     
  19.         application myapp {    
  20.             live on;    
  21.     
  22.             #record keyframes;    
  23.             #record_path /tmp;    
  24.             #record_max_size 128K;    
  25.             #record_interval 30s;    
  26.             #record_suffix .this.is.flv;    
  27.     
  28.             #on_publish http://localhost:8080/publish;    
  29.             #on_play http://localhost:8080/play;    
  30.             #on_record_done http://localhost:8080/record_done;    
  31.     
  32.        }    
  33.        application hls {    
  34.              live on;    
  35.              hls on;    
  36.              hls_path /tmp/app;    
  37.              hls_fragment 5s;    
  38.     
  39.     
  40.        }    
  41.     }    
  42. }  
  43.   
  44. http {  
  45.     include       mime.types;  
  46.     default_type  application/octet-stream;  
  47.   
  48.     #log_format  main  ’remote_addr&nbsp;-&nbsp;remote_user [time_local]&nbsp;"request” ’  
  49.     #                  ’status&nbsp;body_bytes_sent ”$http_referer” ’  
  50.     #                  ’”http_user_agent"&nbsp;"http_x_forwarded_for”’;  
  51.   
  52.     #access_log  logs/access.log  main;  
  53.   
  54.     sendfile        on;  
  55.     #tcp_nopush     on;  
  56.   
  57.     #keepalive_timeout  0;  
  58.     keepalive_timeout  65;  
  59.   
  60.     #gzip  on;  
  61.   
  62.     server {  
  63.         listen       80;  
  64.         server_name  localhost;  
  65.   
  66.         #charset koi8-r;  
  67.   
  68.         #access_log  logs/host.access.log  main;  
  69.   
  70.         location / {  
  71.             root   html;  
  72.             index  index.html index.htm;  
  73.         }  
  74.   
  75.         #error_page  404              /404.html;  
  76.   
  77.         # redirect server error pages to the static page /50x.html  
  78.         #  
  79.         error_page   500 502 503 504  /50x.html;  
  80.         location = /50x.html {  
  81.             root   html;  
  82.         }  
  83.   
  84.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  85.         #  
  86.         #location ~ \.php$ {  
  87.         #    proxy_pass   http://127.0.0.1;  
  88.         #}  
  89.   
  90.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  91.         #  
  92.         #location ~ \.php$ {  
  93.         #    root           html;  
  94.         #    fastcgi_pass   127.0.0.1:9000;  
  95.         #    fastcgi_index  index.php;  
  96.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  97.         #    include        fastcgi_params;  
  98.         #}  
  99.   
  100.         # deny access to .htaccess files, if Apache’s document root  
  101.         # concurs with nginx’s one  
  102.         #  
  103.         #location ~ /\.ht {  
  104.         #    deny  all;  
  105.         #}  
  106.     }  
  107.   
  108.   
  109.     # another virtual host using mix of IP-, name-, and port-based configuration  
  110.     #  
  111.     #server {  
  112.     #    listen       8000;  
  113.     #    listen       somename:8080;  
  114.     #    server_name  somename  alias  another.alias;  
  115.   
  116.     #    location / {  
  117.     #        root   html;  
  118.     #        index  index.html index.htm;  
  119.     #    }  
  120.     #}  
  121.   
  122.   
  123.     # HTTPS server  
  124.     #  
  125.     #server {  
  126.     #    listen       443 ssl;  
  127.     #    server_name  localhost;  
  128.   
  129.     #    ssl_certificate      cert.pem;  
  130.     #    ssl_certificate_key  cert.key;  
  131.   
  132.     #    ssl_session_cache    shared:SSL:1m;  
  133.     #    ssl_session_timeout  5m;  
  134.   
  135.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  136.     #    ssl_prefer_server_ciphers  on;  
  137.   
  138.     #    location / {  
  139.     #        root   html;  
  140.     #        index  index.html index.htm;  
  141.     #    }  
  142.     #}  
  143.   
  144. }  



参考文章:

nginx的RTMP协议服务器

http://www.cnblogs.com/aHuner/p/3247068.html

用开源nginx-rtmp-module搭建flash直播环境

http://www.rosoo.net/a/201307/16696.html

nginx上搭建HLS流媒体服务器

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


document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
    <div id="digg" articleid="50568999">        <dl id="btnDigg" class="digg digg_enable" onclick="btndigga();">             <dt>顶</dt>            <dd>0</dd>        </dl>        <dl id="btnBury" class="digg digg_enable" onclick="btnburya();">              <dt>踩</dt>            <dd>0</dd>                       </dl>    </div> <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank">&nbsp;</a>   </div><div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank">&nbsp;</a></div><script type="text/javascript">    function btndigga() {        $(".tracking-ad[data-mod='popu_222'] a").click();    }    function btnburya() {        $(".tracking-ad[data-mod='popu_223'] a").click();    }        </script>


阅读全文
0 0