Linux下,使用nginx+ffmpeg+video.js实现直播效果(含centos7环境配置步骤)

 

前言

近来因为项目需要,需要做一个把视频解码然后推流,在浏览器播放的功能。然后查资料、找demo,最终决定使用FFmpeg+nginx来完成相应功能,过程颇为心酸,在此做下笔记,以备不时之需。

参考链接:nginx+ffmpeg搭建流媒体服务器(直播流)

nginx配置

一、软件包下载

首先需要安装gcc、g++编译器,后面的大部分编译工作都是编译器来完成的。因为我的环境中已经安装好了(搞嵌入式的,没有编译器还搞个什么劲 ^-^),在此不做赘述,相信大部分人的机器中也已经安装,对于没有安装的,yum -y install gcc gcc-c++即可。

新建目录  mkdir /root/install,进入目录

wget http://nginx.org/download/nginx-1.6.0.tar.gz
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
wget http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.gz
wget http://www.openssl.org/source/openssl-1.0.1g.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

以上链接可以直接下载。

二、安装

下载完成之后,解压缩:

tar.gz 解压缩方式:tar -zxvf  包名

zip 解压缩方式 unzip 包名

安装顺序 openssl--------prce--------zlib

./configure --prefix=/usr/local/XXX/
make 
make install

一般都是安装在/usr/local/下面,以库的名字命名的文件夹中

openssl安装过程出现错误:

POD document had syntax errors at /usr/bin/pod2man line 69.
make: *** [install_docs] Error 1

vim /usr/bin/pod2man

使用命令:set nu 显示行号,将70行注释掉即可。

 

将上述三个安装完成之后。解压剩下的两个包,并且将除了nginx-1.6.0之外的包全部移动到 /var/tmp目录下。

mv nginx_mod_h264_streaming-2.2.7 nginx-rtmp-module-master/ pcre-8.35 zlib-1.2.11 openssl-1.0.1g /var/tmp/

进入nginx-1.6.0

./configure --prefix=/usr/local/nginx --add-module=/usr/tmp/nginx_mod_h264_streaming-2.2.7 --add-module=/usr/tmp/nginx-rtmp-module-master --with-http_flv_module --with-http_mp4_module --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/tmp/pcre-8.35 --with-zlib=/usr/tmp/zlib-1.2.11 --with-debug --with-openssl=/usr/tmp/openssl-1.0.1g

执行完之后 make,在此过程中,出现了错误:

错误1

make[1]: Entering directory `/root/install/nginx-1.6.0'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -D_LARGEFILE_SOURCE -DBUILDING_NGINX -I/usr/tmp/nginx-rtmp-module-master  -I src/core -I src/event -I src/event/modules -I src/os/unix -I /usr/tmp/pcre-8.35 -I /usr/tmp/openssl-1.0.1g/.openssl/include -I /usr/tmp/zlib-1.2.11 -I objs -I src/http -I src/http/modules -I src/mail \
	-o objs/addon/src/ngx_http_h264_streaming_module.o \
	/usr/tmp/nginx_mod_h264_streaming-2.2.7/src/ngx_http_h264_streaming_module.c
In file included from /usr/tmp/nginx_mod_h264_streaming-2.2.7/src/ngx_http_h264_streaming_module.c:2:0:
/usr/tmp/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c: In function ‘ngx_streaming_handler’:
/usr/tmp/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158:8: error: ‘ngx_http_request_t’ has no member named ‘zero_in_uri’
   if (r->zero_in_uri)
        ^
make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1
make[1]: Leaving directory `/root/install/nginx-1.6.0'
make: *** [build] Error 2

解决方法:

进入nginx_mod_h264_streaming-2.2.7的源代码:

vim /usr/tmp/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c  158行

注释掉下面语句即可:
 if (r->zero_in_uri)   { 
    return NGX_DECLINED;  
 } 

错误2

/usr/tmp/nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c: In function ‘stsd_parse_vide’:
/usr/tmp/nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:529:22: error: variable ‘level_indication’ set but not used [-Werror=unused-but-set-variable]
         unsigned int level_indication;
                      ^
/usr/tmp/nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:528:22: error: variable ‘profile_compatibility’ set but not used [-Werror=unused-but-set-variable]
         unsigned int profile_compatibility;
                      ^
/usr/tmp/nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:527:22: error: variable ‘profile_indication’ set but not used [-Werror=unused-but-set-variable]
         unsigned int profile_indication;
                      ^
/usr/tmp/nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:526:22: error: variable ‘configuration_version’ set but not used [-Werror=unused-but-set-variable]
         unsigned int configuration_version;

解决方法:vim nginx-1.6.0/objs/Makefile 里面的--Werror 选项去掉即可

最后make install完成安装。

三、nginx启动

进入目录,执行以下操作

cd /usr/local/nginx/sbin
./nginx

遇到的错误:

这是因为80端口被占用了,修改监听端口

cd ../conf
vim nginx.conf

把此处的80端口修改为未监听的端口即可,我改为了81,保存退出。

重新启动,成功。

测试方法:在浏览器输入ip+端口,成功则会显示如下结果。

至此,nginx安装成功!

FFmpeg配置

一、相关安装包下载

【   lame   】 wget  https://nchc.dl.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz

【  libogg  】wget  https://ftp.osuosl.org/pub/xiph/releases/ogg/libogg-1.3.3.tar.gz

【libvorbis】wget  https://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.6.tar.gz

【xvidcore】wget http://ftp.br.debian.org/debian-multimedia/pool/main/x/xvidcore/xvidcore_1.3.3.orig.tar.gz 低于1.3.3会出现问题

【   x264   】wget ftp://ftp.videolan.org/pub/x264/snapshots/last_x264.tar.bz2

【    a52    】wget http://liba52.sourceforge.net/files/a52dec-0.7.4.tar.gz

【   faad2  】wget https://nchc.dl.sourceforge.net/project/faac/faad2-src/faad2-2.8.0/faad2-2.8.8.tar.gz

【    faac   】wget https://nchc.dl.sourceforge.net/project/faac/faac-src/faac-1.29/faac-1.29.9.2.tar.gz

【  amrnb 】wget http://ftp.penguin.cz/pub/users/utx/amr/amrnb-11.0.0.0.tar.bz2

【  amrwb 】wget http://ftp.penguin.cz/pub/users/utx/amr/amrwb-11.0.0.0.tar.bz2

【    amr    】wget https://jaist.dl.sourceforge.net/project/opencore-amr/opencore-amr/0.1.2/opencore-amr-0.1.2.tar.gz

下载完之后,解压,安装。

tar -zxvf xxx.tar.gz  or  unzip xxx.zip  or tar -jxvf tar.bz2
cd xxx
./configure --prefix=/usr/ 
make && make install

需要注意的是,在最后的安装过程中,有些库和头文件不一定会自动给你移动到/usr/lib 或者/usr/include下面去,只会把bin文件移动过去,需要自己移动(我就踩过坑,前前后后装了好几遍,明明已经安装成功,但是就是找不到.a或者.so文件。这时你要注意了,很有可能是make install没有给你做相关文件移动的命令;例如查看有无x264库,执行命令find /usr -name libx264*,看看有无.a或者.so字样结尾的文件,如果没有,去源码安装目录,寻找以.a或者.so结尾的库移动到/usr/lib和/usr/lib64下面即可)。

安装过程大部分还是很顺利的,偶尔碰到的小问题,由于当时没做记录,也没有什么很深的印象,已经忘记了。

二、ffmpeg配置安装

ffmpeg源码地址:https://ffmpeg.org/releases/ffmpeg-4.1.tar.bz2,版本为4.1最新版

依旧是/root/install目录,解压tar -jxvf ffmpeg-4.1.tar.bz2

终结版命令:

cd ffmpeg-4.1/
./configure --prefix=/usr/local/ffmpeg --enable-libmp3lame --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libxvid --enable-postproc --enable-ffplay --extra-cflags=-I/usr/local/include --extra-libs=-ldl --extra-libs=-lxvidcore --extra-libs=-lm --extra-libs=-lpthread

后面的--extra-cflags=-I/usr/local/include --extra-libs=-ldl --extra-libs=-lxvidcore --extra-libs=-lm --extra-libs=-lpthread

这些选项为安装过程出现各种错误之后解决掉之后的终结版命令!这只是在我电脑上的路径,不一定适用于所有电脑~~请择需选取。

如果想体验一下痛并快乐着的感觉,可以使用和我一样起点的命令:

./configure --prefix=/usr/local/ffmpeg2 --enable-libmp3lame --enable-libvorbis --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libxvid --enable-postproc --enable-ffserver --enable-ffplay

命令参考链接:Linux下ffmpeg的完整安装,最后等待执行完成即可。

 

以上步骤已经处理了大部分会出现的错误,理论上不会出现下列错误,可略过。

 

错误汇总:

1、ERROR: libmp3lame >= 3.98.3 not found

https://nchc.dl.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz

tar -zxvf lame-3.100.tar.gz
cd lame-3.100
./configure
make && make install

2、ERROR: libopencore_amrnb not found

https://jaist.dl.sourceforge.net/project/opencore-amr/opencore-amr/0.1.2/opencore-amr-0.1.2.tar.gz

tar -zxvf opencore-amr-0.1.2.tar.gz
cd opencore-amr-0.1.2
./configure
make && make install

3、configure: error: must have Ogg installed!

https://ftp.osuosl.org/pub/xiph/releases/ogg/libogg-1.3.3.tar.gz

tar -xf libogg-1.3.3.tar.xz
./configure --prefix=/usr --disable-static
make && make install

4、ERROR: libx264 not found

ftp://ftp.videolan.org/pub/x264/snapshots/last_x264.tar.bz2

tar -jxvf last_x264.tar.bz2
cd x264-snapshot-20190111-2245/
./configure --disable-asm --enable-static --enable-shared
make && make install

5、ERROR: libxvid not found 

http://ftp.br.debian.org/debian-multimedia/pool/main/x/xvidcore/xvidcore_1.3.3.orig.tar.gz

tar -xf xvidcore_1.3.3.orig.tar.gz
cd xvidcore/build/generic
./configure
make && make install

6、ERROR: libvorbis not found

 https://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.6.tar.gz

tar -xf libvorbis-1.3.6.tar.gz
cd libvorbis-1.3.6
./configure
make && make install

7、gcc is unable to create an executable file

这种情况下要么是gcc、gcc-c++没有安装,要么是 ./configure后面的选项中路径有错误,或者是链接的库不存在。好好检查下附加库路径或者名字有没有错误。

8、config.log中报错,相关头文件找不到

查找相关头文件是否存在,如不存在,安装;如果存在依然没有找到增加选项:--extra-cflags=-I头文件路径名;例如-I/usr/local/include

9、config.log中报错,无法解决的外部符号错误或找不到相关函数的实现

百度搜索函数所在的库,如不存在,安装,如果存在没有找到,增加编译选项: --extra-libs=-l库名;例如 --extra-libs=-lxvidcore

总结:

遇到错误,先不要急着百度,去源码所在目录,执行命令

vim ./ffbuild/config.log

 在这里面,有更为详细的错误原因,更加能直接定位到错误所在。如什么文件找不到或者是什么库没有连接到,这里面都有比较详细的说明,至此,ffmpeg已经安装完成了。

 

三、FFmpeg测试

编译完成之后,可以使用make install,他会自动拷贝相关头文件到编译选项中--prefix=后面的路径里去,也可以不执行,在当前目录测试。输入命令:

./ffmpeg

报错:

./ffmpeg: error while loading shared libraries: libopencore-amrwb.so.0: cannot open shared object file: No such file or directory

find /usr/ -name libopencore*

cp /usr/local/lib/libopencore-amrwb.so.0 /usr/lib64/
cp /usr/local/lib/libopencore-amrwb.so.0 /usr/lib/

cp /usr/local/lib/libopencore-amrnb.so.0 /usr/lib64/
cp /usr/local/lib/libopencore-amrnb.so.0 /usr/lib/

把相关库复制到/usr/lib和/usr/lib64下面,也可以增加相关库位置到系统路径。总之让程序找到库即可。

执行成功:

[root@instance-5vgw70ig ffmpeg-4.1]# ./ffmpeg
ffmpeg version 4.1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-36)
  configuration: --prefix=/usr/local/ffmpeg --enable-libmp3lame --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libxvid --enable-postproc --enable-ffplay --extra-cflags=-I/usr/local/include --extra-libs=-ldl --extra-libs=-lxvidcore --extra-libs=-lm --extra-libs=-lpthread
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

显示如图所示,即为成功安装!

这里有编译好的ffmpeg_4.1版下载链接(附带运行环境):   终结版下载地址

 

推流直播配置

一、nginx配置

打开nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf

在上所示位置,添加代码 保存退出即可。

rtmp {
 
    server {
 
        listen 8090;  #//服务端口 
 
        chunk_size 4096;   #//数据传输块的大小
 
        application live {
            live on; 
            #play /usr/local/data/video; #//视频文件存放位置,访问方式rtmp://localhost:8090:/video
            #如视频路径存有视频welcome.mp4访问路径即为rtmp://localhost:8090/video/welcome.mp4
        }   
    }   
}

重启nginx服务,无输出即为成功。

[root@192 klxs]# 
[root@192 klxs]# /usr/local/nginx/sbin/nginx -s reload
[root@192 klxs]# 
[root@192 klxs]# 

ffmpeg推流命令:
 

./ffmpeg -re -i 2.avi -c copy -vcodec libx264 -acodec aac -f flv rtmp://192.168.1.5:8090/live/room

-i :这个参数表示输入 ,后面2.avi 就是输入文件。

-vcodec libx264: -vcodec表示使用的视频编解码器 ,前缀v表示video。后面紧跟的libx264表示使用h264视频编解码器

-acodec aac: -acodec表示使用的音频编解码器,前缀a表示audio。后面的aac表示使用aac格式的音频编解码器。

-f flv : -f表示format ,就是强制输出格式为flv,这一步其实也叫封装(mux),封装要做的事就是把视频和音频混合在一起,进行同步。紧跟在后面的rtmp://192.168.1.5:8090/live/room 表示输出的"文件名",这个文件名可以是一个本地的文件,也可以指定为rtmp流媒体地址。指定为rtmp流媒体地址后,则ffmpeg就可以进行推流。

播放方式一:下载VLC播放器,输入推流地址即可。

播放方式二:video.js,  下载链接

下载之后放入/usr/local/nginx/html目录下,在网页直接打开index.html即可。此方式在第二次实验时未成功,暂时没有截图,待以后上传。至此,安装过程全部结束!!

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值