Nginx 搭建Flash服务器(学习笔记二十三)

简介: 本文概要:        nginx是非常优秀的开源服务器,用它来做hls或者rtmp流媒体服务器是非常不错的选择。本文介绍了一种简易方法快速搭建rtmp流媒体服务器,也叫rtsp转播,数据源不是读取文件,而是采用获取rtspNal流后使用ffmpeg转播。

本文概要:

        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测试如下:

img_1fc5b7991d90f7b4e87ff05f5d33a341.jpe


此后可以将实时流推送到rmp了。

我的方案是live555接收流,串流方式推送至fifo,做rtmp的转发。可能有些延迟,哪位朋友有更好的方案,欢迎交流评论。

其框架如下:非常简单,并容易集成入大流媒体框架。


img_d49e2655306215fb4c82eeb53d88b0e6.jpe


Flex方案截图:


img_2c530cd19fb8759975f921e9979a4b37.jpe


附录:

RTMP配置如下:


[plain]view plaincopy


#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

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



from:http://blog.csdn.net/gubenpeiyuan/article/details/38089013

目录
相关文章
|
29天前
|
前端开发 应用服务中间件 nginx
使用Docker快速搭建Web服务器Nginx
本文指导如何使用Docker快速搭建Nginx服务器。首先,通过`docker pull`命令获取Nginx镜像,然后以容器形式运行Nginx并映射端口。通过挂载目录实现本地文件与容器共享,便于自定义网页。使用`docker ps`检查运行状态,访问IP:8088确认部署成功。最后,介绍了停止、删除Nginx容器的命令,强调Docker简化了服务器部署和管理。
42 0
|
6天前
|
应用服务中间件 Linux 开发工具
如何在阿里云服务器快速搭建部署Nginx环境
以下是内容的摘要: 本文档主要介绍了在阿里云上购买和配置服务器的步骤,包括注册阿里云账号、实名认证、选择和购买云服务器、配置安全组、使用Xshell和Xftp进行远程连接和文件传输,以及安装和配置Nginx服务器的过程。在完成这些步骤后,你将能够在服务器上部署和运行自己的网站或应用。
|
11天前
|
弹性计算 应用服务中间件 Linux
阿里云ECS服务器上从零开始搭建nginx服务器
阿里云ECS服务器上从零开始搭建nginx服务器
|
1月前
|
弹性计算 算法 应用服务中间件
倚天使用|Nginx性能高27%,性价比1.5倍,基于阿里云倚天ECS的Web server实践
倚天710构建的ECS产品,基于云原生独立物理核、大cache,结合CIPU新架构,倚天ECS在Nginx场景下,具备强大的性能优势。相对典型x86,Http长连接场景性能收益27%,开启gzip压缩时性能收益达到74%。 同时阿里云G8y实例售价比G7实例低23%,是Web Server最佳选择。
|
2月前
|
tengine Rust 负载均衡
反向代理学习笔记(一) Nginx与反向代理绪论
反向代理学习笔记(一) Nginx与反向代理绪论
|
2月前
|
网络协议 Unix 应用服务中间件
如何进行 Nginx HTTPS服务器搭建
【2月更文挑战第6天】
63 0
|
2月前
|
负载均衡 JavaScript 应用服务中间件
强大的WEB服务器-Nginx
强大的WEB服务器-Nginx
29 0
|
2月前
|
Kubernetes 应用服务中间件 nginx
K8S Pod Sidecar 应用场景之一 - 加入 NGINX Sidecar 做反代和 web 服务器
K8S Pod Sidecar 应用场景之一 - 加入 NGINX Sidecar 做反代和 web 服务器
|
2月前
|
应用服务中间件 nginx
将树莓派转身为强大的Web服务器:使用Nginx和cpolar实现远程访问
安装 Nginx(发音为“engine-x”)可以将您的树莓派变成一个强大的 Web 服务器,可以用于托管网站或 Web 应用程序。相比其他 Web 服务器,Nginx 的内存占用率非常低,可以在树莓派等资源受限的设备上运行。同时结合cpolar 内网穿透工具即可实现远程访问。
|
应用服务中间件 nginx
nginx服务器的反向代理proxy_pass配置方法
nginx服务器的反向代理proxy_pass配置方法
313 0