你好,游客 登录 注册 搜索
背景:
阅读新闻

使用Nginx+FFmpeg搭建HLS直播转码服务器

[日期:2015-01-03] 来源:Linux社区  作者:wutong_login [字体:大 中 小]

二、安装Nginx相关模块

1.环境准备

yum install –y pcre pcre-devel

yum install –y zlib zlib-devel

2.下载nginx及rtmp模块

wget http://nginx.org/download/nginx-1.6.2.tar.gz

tar xzvf nginx_1.6.2.tar.gz

git clone git://github.com/arut/nginx-rtmp-module.git

3.编译nginx-rtmp

./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_stub_status_module

make

make install

安装完成后,nginx位于/usr/local/nginx/sbin目录下,配置文件nginx.conf在/usr/local/nginx/conf目录下

++++++++测试nginx是否安装正确+++++++++++

#cd /usr/local/nginx

#./sbin/nginx -c ./conf/nginx.conf

打开网页http://localhost,如果显示Welcome表示安装下正确,如果没有显示,请查看一下nginx的日志。

++++++++测试RTMP+++++++++++

修改/usr/local/nginx/conf/nginx.conf的内容如下:

#debug
daemon off;
master_process off;


error_log ./error.log debug;
events{
    worker_connections 1024;
}


rtmp{
    server {
        listen 1935;
        chunk_size 4000;

        #live
        application myapp {
            live on;
      }

}

从网上下载一款RTMP推流工具,我使用的OBS(Open Broadcaster Software),开始推流rtmp://your_ip/myapp/test,使用播放器(http://www.cutv.com/demo/live_test.swf)查看是否正常。

++++++++测试HLS切片功能+++++++++++


修改/usr/local/nginx/conf/nginx.conf的内容如下:

#debug
daemon off;
master_process off;


error_log ./error.log debug;
events{
    worker_connections 1024;
}


rtmp{
    server {
        listen 1935;
        chunk_size 4000;

        #live
        application myapp {
            live on;
 
            hls on;
            hls_path /tmp/hls;
            hls_fragment 2s;
            hls_playlist_length 6s;

      }
    }
}

#HTTP
http{
    server {
        listen 80;

        #welcome
        location / {
            root  html;
            index  index.html index.htm;
        }

        #hls
        location /hls {
            types {
                application/vnd.apple.mpegusr m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        } 
    }
}


使用VLC或iPAD上的播放器进行查看 http://yourip/hls/test.m3u8。

 


++++++++测试FFMPEG转码功能+++++++++++


修改/usr/local/nginx/conf/nginx.conf的内容如下:


#debug
daemon off;
master_process off;


error_log ./error.log debug;
events{
    worker_connections 1024;
}


rtmp{
    server {
        listen 1935;
        chunk_size 4000;

        #live
        application myapp {
            live on;
 
          exec /opt/ffmpeg/bin/ffmpeg -i rtmp://localhost/myapp/$name
            -c:a copy  -c:v libx264 -b:v 512K -g 30 -f flv rtmp://localhost/hls/$name_low;
        }
     
        application hls {

            live on;

            hls on;
            hls_path /tmp/hls;
            hls_nested on;
            hls_fragment 2s;
            hls_playlist_length 6s;

          hls_variant _hi  BANDWIDTH=640000;

        }
    }
}

#HTTP
http{
    server {
        listen 80;

        #welcome
        location / {
            root  html;
            index  index.html index.htm;
        }

        #hls
        location /hls {
            types {
                application/vnd.apple.mpegusr m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        } 
    }
}


使用ffmpeg转码时,

exec /opt/ffmpeg/bin/ffmpeg -i rtmp://localhost/myapp/$name
            -c:a copy  -c:v libx264 -b:v 512K -g 30 -f flv rtmp://localhost/hls/$name_low;

仅对视频进行转码,音频不做处理,同时向流从myapp转推到hls, hls_variant会生成一个多码率的m3u8文件,同时把切片文件存放到test_low目录下,访问多码率时,直接访问http://yourip/hls/test.m3u8,根据这个m3u8中的实现的内容访问相应的码流,在本例中,实际码流URL为http://yourip/hls/test_low/index.m3u8

--------------------------------------分割线 --------------------------------------

CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

--------------------------------------分割线 --------------------------------------

FFmpeg 的详细介绍:请点这里
FFmpeg 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-01/111182.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款