
一、说明
1. 业务场景
- 直播源是rtsp或rtmp等,播放端不支持直接播放rtsp(如网页播放)
- 源视频带宽和负载有限,不支持很多用户访问
- 客户端点播
2. 流程
- 使用ffmpeg从节目源拉流
- 推流到nginx-rtmp/flv服务
- 客户端从nginx服务器拉流观看视频
3. 本文工具
以下两个模块选择一个安装:
- 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-next |
| sudo 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 |
| 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.rpm |
| yum install yum-plugin-fastestmirror |
| sudo vim /lib/modprobe.d/dist-blacklist.conf |
dist-blacklist.conf
| |
| 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 检测结果 |
| |
(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 |
| |
| 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 all |
| sudo yum -y install nvidia-driver-latest-dkms cuda |
| nvidia-smi |

(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” |
| make |
| sudo 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” |
| make |
| sudo make install |
libfdk-aac
| git clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aac |
| cd fdk-aac |
| sudo autoreconf -fiv |
| ./configure --prefix="/usr" --disable-shared |
| make |
| sudo 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 |
| make |
| sudo 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 |
| make |
| sudo 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 |
| make |
| sudo 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 |
| make |
| sudo make install |
libtheora
| git clone git://git.xiph.org/mirrors/theora.git theora-git |
| cd theora-git |
| PKG_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 |
| make |
| sudo make install |
| vim /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 |
| make |
| sudo make install |
disable-gpl
| ./configure --prefix="/usr" --enable-static --disable-gpl --disable-opencl |
| make |
| sudo make install |
安装ffnvcodec
| git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git |
| cd nv-codec-headers |
| make && 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 |
| make |
| sudo 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; |
| |
| |
| load_module modules/ngx_http_flv_live_module.so; |
| |
| |
| |
| |
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
| |
| git clone https://github.com/arut/nginx-rtmp-module.git |
| |
| 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 |
| |
| 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; |
| } |
| } |
| } |
| |
| /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf |
| |
| |
| chkconfig nginx off |
| 服务 nginx 信息读取出错:没有那个文件或目录 |
| |
| whereis nginx |
| nginx: /usr/local/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 |