FFmpeg安装及将rtsp流转hls协议流通过nginx进行视频直播

在工作中遇到需要将海康威视等的rtsp协议的监控视频在浏览器上播放,如果直接通过浏览器播放需要在客户pc上安装插件,明显这是不可行的,在网上找了一段时间的方案后确定了通过ffmpeg将rtsp转成hls协议的视频流再通过nginx转达的方式进行视频播放的方案,hls协议是苹果公司开发的视频直播协议,它可以通过h5的video标签进行视频播放,非常方便,不过也存在一个弊端就是延迟比较大,我这边上线之后的延迟在30s左右,对于我们公司的业务需求来说是可以接受的,如不能接受需谨慎选择,另外如果那位大神知道怎么调优,降低延迟还望多多交流<抱拳>。

话不多说,开始我们的正题吧。说明:以下步骤需在有网情况下进行,如需离线安装,需自行下载安装包,我这里会将部分安装包上传。

1、安装依赖

#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64 gcc-c++

2、安装ffmpeg的依赖

++++++++Yasm+++++++++++
#wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
++++++++x264+++++++++++
#附件中有该安装包
#cd x264-snapshot-20190626-2245
#./configure --enable-shared 
#make
#make install

++++++++LAME+++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
#tar xzvf lame-3.99.5.tar.gz
#cd lame-3.99.5
#./configure --enable-nasm
#make
#make install
++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
#tar xzvf libogg-1.3.3.tar.gz
#cd libogg-1.3.3
#./configure
#make
#make install
++++++++libvorbis+++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.6.tar.gz
#tar xzvf libvorbis-1.3.6.tar.gz
#cd libvorbis-1.3.6
#./configure
#make
#make install
++++++++libvpx+++++++++++
#附件中有该安装包
#cd libvpx
#./configure  --enable-shared
#make
#make install
++++++++FAAD2+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tar zxvf faad2-2.7.tar.gz
#cd faad2-2.7
#./configure
#make
#make install

++++++++Xvid+++++++++++
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.5.tar.gz
#tar zxvf xvidcore-1.3.5.tar.gz
#cd xvidcore/build/generic
#./configure
#make
#make install

++++++++ffmpeg+++++++++++

#附件中有安装包

#cd ffmpeg-4.1.3
#./configure --prefix=/usr/local/ffmpeg/ --enable-version3 --enable-libvpx --enable-libmp3lame --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads
#make && make install

安装ffmpeg的过程较长,请耐心等待,安装成功之后,ffmpeg安装到了/usr/local/ffmpeg目录下。

修改/etc/ld.so.conf如下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/usr/local/ffmpeg/lib

报错退出,执行以下命令

#ldconfig

4、错误处理

如果在安装ffmpeg过程中出现某些依赖版本过低或不存在,请自行安装相应依赖。

*** Could not run Ogg test program, checking why... *** The test program compiled, but did not run. This usually means *** that the run-time linker is not finding Ogg or finding the wrong *** version of Ogg. If it is not finding Ogg, you'll need to set your *** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point *** to the installed location  Also, make sure you have run ldconfig if that *** is required on your system *** *** If you have an old version installed, it is best to remove it, although *** you may also be able to get things to work by modifying LD_LIBRARY_PATH

configure: error: must have Ogg installed!

确定已经安装了libogg 然后安装libvorbis报类似如上错误时,可以执行以下命令解决:

echo /usr/local/lib >> /etc/ld.so.conf; ldconfig

vi /etc/profile 

文件最后面 添加 

export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"

退出保持 

source /etc/profile

3、安装nginx和rtmp模块

下载pcre、zlib、openssl,进行编译安装

到nginx官网下载nginx,解压

到 https://github.com/arut/nginx-rtmp-module/releases 下载rtmp模块,并解压

编译安装nginx-rtmp,进入nginx的解压包下

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

make

make install

安装完成之后,nginx被安装到/usr/local/nginx-1.17.1-rtmp目录下

4、修改nginx配置文件,使其可以分发hls协议视频流进行直播

user  root;
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;
        chunk_size 4000;
        #live
        application myapp {
            live on;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 2s;
            hls_playlist_length 6s;
       }
    }
}

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       8090;
        server_name  dengxingyao.cn;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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


        #hls 
         location /hls {
            types {
                application/vnd.apple.mpegusr m3u8;
                 video/mp2t ts;
              }
           root /usr/local/nginx-1.17.1-rtmp/temp;
           add_header Cache-Control no-cache;
           }
          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
                root   html;
          }
  }
}

上述配置文件的/hls既是nginx通过http请求分发hls协议的.ts文件的配置,该配置表明.m3u8和.ts文件位于/usr/local/nginx-1.17.1-rtmp/temp目录下

 5、通过ffmpeg将rtsp协议视频流转成hls协议

nohup ./ffmpeg -i rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov -strict -2 -c copy -map 0 -f segment -segment_list /usr/local/nginx-1.17.1-rtmp/temp/hls/ipc1/ipc1.m3u8 -segment_time 5 /usr/local/nginx-1.17.1-rtmp/temp/hls/ipc1/output%03d.ts &

 其中rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov为rtsp视频源地址,/usr/local/nginx-1.17.1-rtmp/temp/hls/ipc1/ipc1.m3u8是转换成的.m3u8文件,其保存在上面nginx配置文件中配置的目录下

6、通过VLC播放器可播放rtsp源和转换后的hls视频源进行测试

通过上述操作后的hls视频源地址为:http://ip:8090/hls/ipc1/ipc1.m3u8

7、上述所有安装包资源如下,欢迎下载

https://download.csdn.net/download/ccf199201261/11263464

参考博客:https://blog.csdn.net/wutong_login/article/details/42292787

 

你可能感兴趣的:(ffmpeg)