【总结】ffmpeg+nginx做点播直播服务器示例

Posted on Posted in 计算机4,067 views

零、准备

0.1、环境

    硬件和系统环境同上一篇文章第零节;

    ffmpeg-2.7.2;

    nginx-1.10.2;

    目录如下:

|---biye
     |---ffmpeg
           |---install                   # ffmpeg和其他自编译程序安装位置
           |---source                    # ffmpeg和相关库源文件
           |---web                       # 媒体文件和页面文件,作为网站根目录
     |---nginx
           |---nginx-1.10.2              # nginx源码
           |---nginx-rtmp-module         # rtmp模块源码
           |---nginx_mod_h264_streaming  # stream模块源码
           |---ngx_cache_purge           # cache模块源码
           |---server                    # nginx安装目录
           |---yamdi                     # yamdi源码

0.2、编译ffmpeg

    参考上一篇文章ffmpeg编译过程,可根据自己需求添加和删除编解码库;

0.3、编译nginx

    参考之前文章进行编译,不过需要另外添加nginx模块。具体模块如下:

    • nginx_mod_h264_streaming  让nginx支持flv/mp4流播放

    • nginx-rtmp-module 让nginx支持rtmp/hls协议

    • ngx_cache_purge 缓存清理模块

    为了使nginx为flv创建关键帧自持随意拖动,需要安装yamdi,同时也可以安装正则libpcre3-dev。

0.3.1、安装yamdi和libpcre3-dev

    1、安装libpcre3-dev

apt-get install libpcre3-dev

    2、安装yamdi

cd nginx && mkdir yamdi && cd yamdi
wget http://downloads.sourceforge.net/project/yamdi/yamdi/1.9/yamdi-1.9.tar.gz
tar zxvf yamdi-1.9.tar.gz
cd yamdi-1.9/
vi Makefile
# 将安装目录/usr/local/bin 修改为 /home/xiaoxia/biye/ffmpeg/install/bin
make && make install

    yamdi-1.9.tar.gz

    注意在上一篇文章中,我们将/home/xiaoxia/biye/ffmpeg/install目录添加到了.bashrc。

0.3.2、安装nginx模块

    1、获取模块

cd nginx/
# 获取nginx_mod_h264_streaming
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
# 获取nginx-rtmp-module
wget -O nginx-rtmp-module.zip  https://github.com/arut/nginx-rtmp-module/archive/master.zip
unzip nginx-rtmp-module.zip
# 获取ngx_cache_purge
wget -O ngx_cache_purge.zip https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip
unzip ngx_cache_purge.zip

    nginx_mod_h264_streaming-2.2.7.tar.gz

    nginx-rtmp-module-master.zip

    ngx_cache_purge-master.zip

    2、nginx编译安装

    因为之前.bashrc修改的是root用户下的,所以将nginx配置为root用户。具体nginx配置如下:

./configure \
    --prefix=/home/xiaoxia/biye/nginx/server/ \
    --user=root \
    --group=root \
    --with-http_ssl_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module  \
    --with-http_realip_module \
    --with-http_gzip_static_module \
    --with-http_stub_status_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-pcre=./external/pcre-8.38 \
    --with-zlib=./external/zlib-1.2.8 \
    --with-debug \
    --with-http_addition_module  \
    --http-client-body-temp-path=/var/tmp/nginx/client \
    --http-proxy-temp-path=/var/tmp/nginx/proxy \
    --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
    --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
    --http-scgi-temp-path=/var/tmp/nginx/scgi  \
    --add-module=../nginx_mod_h264_streaming-2.2.7 \
    --add-module=../nginx-rtmp-module \
    --add-module=../ngx_cache_purge

    然后进行编译安装

make && make install

    问题1:ngx_http_streaming_module.c:158:8: error: ‘ngx_http_request_t’ has no member named ‘zero_in_uri

1482213715362653.png

    解决:注释掉nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c的158到161行。

    问题2:error: variable ‘stream_priority’ set but not used [-Werror=unused-but-set-variable]

1482213998199060.png

    解决:修改nginx-1.10.2/objs/Makefile文件第2行CFLAGS变量去掉“-Werror”字段。

一、http+flv/mp4 点播

1.1、nginx配置

    修改配置文件nginx/server/conf/nginx.conf,配置http部分如下:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       8080;
        server_name  tx.evanxia.com;
        root    /home/xiaoxia/biye/ffmpeg/web;
        limit_rate_after 5m;        #在flv视频文件下载了5M以后开始限速
        limit_rate 100k;            #速度限制为100K
        charset utf-8;
        location / {
            index  index.html index.htm;
        }
        #将.flv文件指向flv模块                
        location ~ \.flv$ {
            flv;
        }
        location ~ \.mp4$ {
            mp4;
        }
        error_page   404              /404.html;
        error_page   500 502 503 504  /50x.html;
    }
}

    为了调试方便,我们在该配置文件头加上如下,关闭nginx后台运行。

daemon off;

1.2、测试

    1、首先测试mp4,直接在html5浏览器地址输入mp4链接:

http://tx.evanxia.com:8080/230.mp4

1482216500354502.png

    2、测试flv。首先需要处理flv文件,处理metadata和帧。进入web目录,然后:

yamdi -i sea.flv -o sea-ym.flv

    然后利用flash播放器,或者VLC播放器播放,连接如下:

http://tx.evanxia.com:8080/sea-ym.flv

    本次使用VLC播放,效果如下:

1482217297388165.png

二、rtmp+flv/mp4 点播

1.1、nginx配置

    修改配置文件nginx/server/conf/nginx.conf,配置http部分如下:

rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        # 点播配置
        application video {
            play /home/xiaoxia/biye/ffmpeg/web;
        }
        application audio {
            play /home/xiaoxia/biye/ffmpeg/web;
        }
        application hls {
            hls on;
            hls_path /home/xiaoxia/biye/ffmpeg/web/hls;
            hls_fragment 10s;
        }
    }
}

    hls部分不需要可以去掉,video和audio命令只是为了区分,根据自己实际情况来。

1.2、测试

    此时flv文件不需要使用yamdi处理,直接用flash或者VLC播放下面任意链接:

# 链接格式如下:
# rtmp://host/命令/文件     其中命令为配置文件中的video或者audio
rtmp://tx.evanxia.com/video/sea.flv
rtmp://tx.evanxia.com/video/230.mp4

    用VLC播放效果如下:

1482220642990651.png

三、ffmpeg推送流直播

1.1、nginx配置

    主要用于直播,我们用ffmpeg推送媒体文件来模拟。在web目录下新建rec和rec/chunked目录,用于记录直播数据。nginx配置如下:

rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        # 直播流配置
        application live {
            live on;
            #为 rtmp 引擎设置最大连接数。默认为 off
            max_connections 1024;
            # default recorder
            record all;
            record_path /home/xiaoxia/biye/ffmpeg/web/rec;
            recorder audio {
                record audio;
                record_suffix -%d-%b-%y-%T.flv;
            } 
            recorder chunked {
                record all;
                record_interval 15s;
                record_path /home/xiaoxia/biye/ffmpeg/web/rec/chunked;
            }
        }
    }
}

1.2、测试

    1、ffmpeg推送,进入web目录用ffmpeg推送sea.flv流到rtmp://tx.evanxia.com/live/steam。

ffmpeg -re -i ./sea.flv -f flv rtmp://tx.evanxia.com/live/steam
ffmpeg -re -i ./230.mp4 -c copy -f flv rtmp://tx.evanxia.com/live/steam

    正常直播室客户端推送音视频的rtmp数据流到rtmp://tx.evanxia.com/live/steam。

    2、用flash或者VLC播放器进行播放:

rtmp://tx.evanxia.com/live/steam

    效果如下:

1482225063984892.png

最后,本文主要是用于学习rtmp,虽然有配置hls,但是均未进行测试。完全的nginx.conf配置如下:

    nginx.conf.rar


参考

1、http://yamdi.sourceforge.net/

2、http://blog.csdn.net/cyh970473/article/details/50593227

3、http://h264.code-shop.com/trac/wiki/Mod-H264-Streaming-Nginx-Version2

4、http://blog.sina.com.cn/s/blog_946cb2b70100x71h.html

5、http://www.cnblogs.com/wainiwann/p/3866254.html

6、http://lib.csdn.net/article/57/37915?knId=1549


转载标明出处:https://blog.evanxia.com/2016/12/1225