日志样式

音视频开发2. FFMPEG+Nginx实现推流服务.

  • 标签 :


一、说明

1. 业务场景

  • 直播源是rtsp或rtmp等,播放端不支持直接播放rtsp(如网页播放)
  • 源视频带宽和负载有限,不支持很多用户访问
  • 客户端点播

2. 流程

  1. 使用ffmpeg从节目源拉流
  2. 推流到nginx-rtmp/flv服务
  3. 客户端从nginx服务器拉流观看视频

3. 本文工具

  • ffmpeg
  • nginx
  • VLC(用来测试拉流)

以下两个模块选择一个安装:

  • nginx-http-flv-module
  • nginx-rtmp-module

其实nginx flv也是基于nginx-rtmp-module的流媒体服务器。

功能对比:

功能nginx-http-flv-modulenginx-rtmp-module备注HTTP-FLV (播放)√x支持HTTPS-FLV和chunked回复GOP缓存√x虚拟主机√x省略listen配置√见备注配置中必须有一个listen纯音频支持√见备注wait_video或wait_key开启后无法工作定时打印访问记录√xJSON风格的stat√x

二、FFMPEG 的安装

1. Ubuntu环境

(1) apt-get安装

# 可通过PPA进行安装
sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-nextsudo apt-get update
sudo apt-get install ffmpeg
# 查看是否安装成功:ffmpeg -version

(2) 编译安装

sudo apt-get install -y autoconf automake build-essential git libass-dev libfreetype6-dev libsdl2-dev
sudo apt-get install -y libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo wget zlib1g-dev  
sudo apt-get install -y nasam yasm cmake mercurial  
./configure --enable-shared  --prefix=/usr/local/ffmpeg --disable-x86asm
sudo make 
sudo make install

在 /etc/ld.so.conf中,末尾添加 /usr/local/ffmpeg/lib,执行sudo ldconfig

为 ffmpeg 加入环境变量

vi /etc/profile

加入以下内容:

export PATH="/usr/local/ffmpeg/bin:$PATH"

然后保存并运行source /etc/profile

2. CentOS7(支持nVidia硬件解码)

硬件环境:nVidia 20602块

(1) 安装nVidia驱动

(A) yum方式安装nVidia驱动

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpmyum install yum-plugin-fastestmirrorsudo vim /lib/modprobe.d/dist-blacklist.conf

dist-blacklist.conf

#blacklist nvidiafb
blacklist nouveau
options nouveau modeset=0
mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak
dracut /boot/initramfs-$(uname -r).img $(uname -r)lspci | grep nouveau

没有输出,说明屏蔽默认带有的nouveau成功。

一些命令:

sudo yum install nvidia-detect   nvidia显卡检测
nvidia-detect -v                       检测结果# yum -y install kmod-nvidia     安装显卡驱动 这一步应该不需要 

(B) run脚本方式安装nVidia驱动

参考:https://github.com/keylase/nvidia-patch

cd /opt/nvidia
wget https://international.download.nvidia.com/XFree86/Linux-x86_64/430.40/NVIDIA-Linux-x86_64-430.40.run
chmod +x NVIDIA-Linux-x86_64-430.40.run
./NVIDIA-Linux-x86_64-430.40.run --kernel-source-path=/usr/src/kernels/3.10.0-957.27.2.el7.x86_64
# 下面是移除NVENC同时运行最大数量的限制 restriction on maximum number of simultaneous NVENC
git clone https://github.com/keylase/nvidia-patch
cd nvidia-patch
bash ./patch.sh

如果需要卸载,使用命令:

./NVIDIA-Linux-x86_64-430.40.run --uninstall

(2) 安装Cuda

参考

sudo yum-config-manager --add-repo http://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64/cuda-rhel7.repo
sudo yum clean allsudo yum -y install nvidia-driver-latest-dkms cuda
nvidia-smi
音视频开发2. FFMPEG+Nginx实现推流服务

 

(3) 编译ffmpeg的准备工作

(i) 库说明

  • x264 编码H.264视频,编译参数–enable-gpl --enable-libx264
  • fdk-aac 编码AAC音频,编译参数–enable-libfdk-aac
  • libvpx VP8/VP9视频编码器,编译参数–enable-libvpx
  • libvorbis 编码Vorbis音频,需要libogg。编译参数–enable-libvorbis
  • libopus 编码Opus音频。
  • LAME 编码MP3音频,编译参数–enable-libmp3lame
  • libass 字幕渲染器,编译参数–enable-libass

(ii) 下载路径

mkdir ~/ffmpeg_sources

(iii) 配置库

yasm

cd ~/ffmpeg_sources
curl -L -O http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xvzf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix=”/usr”
makesudo make install

nasm

curl -L -O http://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.xz
tar xf nasm-2.14.02.tar.xz
cd nasm-2.14.02
./configure --prefix=”/usr”makesudo make install

libfdk-aac

git clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aac
cd fdk-aacsudo autoreconf -fiv./configure --prefix="/usr" --disable-shared
makesudo make install

lame

curl -L -O 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 --prefix="/usr" --disable-shared --enable-nasm
makesudo make install

libopus

curl -L -O https://archive.mozilla.org/pub/opus/opus-1.2.tar.gz
tar xvzf opus-1.2.tar.gz
cd opus-1.2
./configure --prefix="/usr" --disable-shared
makesudo make install

libogg

curl -L -O http://downloads.xiph.org/releases/ogg/libogg-1.3.2.tar.gz
tar xzvf libogg-1.3.2.tar.gz
cd libogg-1.3.2
./configure --prefix="/usr" --disable-shared
makesudo make install

libvorbis

curl -L -O http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.4.tar.gz
tar xzvf libvorbis-1.3.4.tar.gz
cd libvorbis-1.3.4
./configure --prefix="/usr" --with-ogg="/usr" --disable-shared
makesudo make install

libtheora

git clone git://git.xiph.org/mirrors/theora.git theora-git
cd theora-gitPKG_CONFIG_PATH=/usr/lib/pkgconfig ./autogen.sh --prefix=/usr --disable-examples --disable-shared --disable-sdltest --disable-vorbistest && make && make install

libx265

sudo yum install hg cmake
hg clone http://hg.videolan.org/x265
cd x265/build/linux
sudo cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="/usr" -DENABLE_SHARED:bool=off ../../source
makesudo make installvim /usr/lib/pkgconfig/x265.pc

加上:在Libs.private: -lstdc++ -lm -lrt -ldl后面加上-lpthread

x264

curl -L -O http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar xjvf last_x264.tar.bz2
cd x264-snapshot-20170625-2245/
./configure --prefix="/usr" --enable-static
makesudo make install

disable-gpl

./configure --prefix="/usr" --enable-static --disable-gpl --disable-opencl
makesudo make install

安装ffnvcodec

git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headersmake && sudo make install

libvpx

VP8/VP9 video encoder.

git clone --depth 1 https://github.com/webmproject/libvpx.git
cd libvpx./configure --prefix="/usr" --disable-examples --disable-unit-tests --as=yasm
makesudo make install

https

wget http://mirrors.ibiblio.org/openssl/source/old/1.0.2/openssl-1.0.2k.tar.gz
tar -xvf openssl-1.0.2k.tar.gz
cd openssl-1.0.2k
make clean./config shared --prefix="/usr"
make -j32 && make install

(4) 配置ffmpeg, 编译安装

(i) 下载

git clone https://git.ffmpeg.org/ffmpeg.git
git clone https://github.com/libav/libav

(ii) 配置

PKG_CONFIG_PATH="/usr/lib/pkgconfig" 
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
cd ffmpeg
./configure --prefix="/usr" --pkg-config-flags="--static" --enable-gpl --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libxvid --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --extra-cflags="-I/usr/local/cuda/include/" --extra-ldflags=-L/usr/local/cuda/lib64 --disable-shared --enable-nvenc --enable-cuda --enable-cuvid --enable-libnpp --enable-pthreads --enable-openssl

我实际编译去掉了–enable-libfdk-aac enable-libxvid

(iii) 编译

make -j 10

(iv) 安装

make install

(5) 使用示例

ffmpeg -hwaccel cuvid -c:v h264_cuvid -i "rtsp://admin:adminpassword@ip:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif"   -c:v h264_nvenc -an  -preset slow -tune film  -f flv rtmp://rtmpip:port/live/v_test```

 

三、nginx的rtmp/flv模块安装

1. CentOS7环境

(1) 安装nginx-http-flv-module模块

A. 通过nginx.conf

yum install https://extras.getpagespeed.com/release-el$(rpm -E %{rhel})-latest.rpm
yum install nginx-module-flv
  • 安装完毕后,HTTP-FLV功能的配置文件http-flv.conf和RTMP功能的配置文件rtmp.conf会被放在/etc/nginx/http-flv目录下
  • 通过include手工将它们添加到/etc/nginx/nginx.conf,以开启HTTP-FLV和RTMP功能:
http {    ...    include /etc/nginx/http-flv/http-flv.conf;
}include /etc/nginx/http-flv/rtmp.conf;
# 添加以下配置到/etc/nginx/nginx.conf,启动或者重启NGINX来启用本模块:
load_module modules/ngx_http_flv_live_module.so;
# 注意
# 上述的配置必须位于events配置项之前,否则NGINX不能启动。
# 更新可以通过yum update来完成。关于其他NGINX模块的详情见GetPageSpeed。

B. 源码编译安装

cd root
git clone https://github.com/winshining/nginx-http-flv-module
wget http://nginx.org/download/nginx-1.8.1.tar.gz tar -zxvf nginx-1.8.1.tar.gz cd nginx-1.8.1 
./configure --add-module=/root/nginx-http-flv-module  --with-http_ssl_module   make && make install

(2) 安装 nginx-rtmp-module

# 下载nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
# 编译安装nginx
wget http://nginx.org/download/nginx-1.8.1.tar.gz 
tar -zxvf nginx-1.8.1.tar.gz 
cd nginx-1.8.1 
./configure --prefix=/usr/local/nginx  --add-module=../nginx-rtmp-module  --with-http_ssl_module   
make && make install
# 设置nginx
vi /usr/local/nginx/conf/nginx.conf

nginx.conf内容

worker_processes  1;
events {
    worker_connections  1024;
}http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       809;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }    }}    rtmp {
        out_queue 4096;
        out_cork 8;
        max_streams 4096;
        server {
            listen 90;
            drop_idle_publisher 30s;
            application live {
                live on;
            }        }    }
# 启动nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 如果要卸载# 如果有自启动,则删除 Nginx 的自启动chkconfig nginx off服务 nginx 信息读取出错:没有那个文件或目录# 查找nginx的安装目录whereis nginxnginx: /usr/local/nginx
# 停止nginx服务/usr/local/nginx/sbin/nginx -s stop
# 删除安装目录rm -rf /usr/local/nginx/
# 查找是否还有残余的find / -name nginx
/usr/local/lib64/nginx-1.15.7/objs/nginx
rm -rf /usr/local/lib64/nginx-1.15.7/

2. windows环境安装nginx-rtmp

(1) 下载

官方的nginx不带rtmp模块,在下面地址可下载windows版本的nginx+rtmp:
https://github.com/illuspas/nginx-rtmp-win32
使用:nginx-rtmp-win32-1.2.1.zip

(2) 配置文件

nginx.conf

worker_processes  1;
error_log  logs/error.log debug;
events {
    worker_connections  1024;
}rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;
rtmp {
    out_queue 4096;
    out_cork 8;
    max_streams 4096;
    server {
        listen 90;
        drop_idle_publisher 30s;
		        application live {
            live on;
        }		          }}http {
    server {
        listen      80;
		        location / {
            root html;
        }		        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }        location /stat.xsl {
            root html;
        }		           }}

/tmp需要有可写权限。

上面示例中的推流/拉流地址
http://ip:90/live/test

四、测试

# 推流测试
ffmpeg -hwaccel cuvid -c:v h264_cuvid -i "视频资源源地址"  -c:v h264_nvenc -an  -crf 20 -b:a 96k -b:v 2048k -preset medium -tune animation -vf scale_npp=1024:-1 -f flv rtmp://ip:90/live/test
# 拉流测试
使用VLC Media Player打开网址: rtmp://ip:90/live/test

天津市犀思科技有限公司是专业从事web应用定制开发的一家公司,主营业务包括定制功能型网站建设开发微信小程序开发微信公众号开发APP定制开发天津企业微信开发、ERP、CRM、OA等企业应用场景信息化解决方案等服务,致力于成为中国领先的IT服务及行业解决方案的提供商。


发表评论

电子邮件地址不会被公开。 必填项已用*标注

看不清?点击更换