音視頻開發(fā)2. FFMPEG+Nginx實(shí)現(xiàn)推流服務(wù).
- 標(biāo)簽 :
一、說(shuō)明
1. 業(yè)務(wù)場(chǎng)景
- 直播源是rtsp或rtmp等,播放端不支持直接播放rtsp(如網(wǎng)頁(yè)播放)
- 源視頻帶寬和負(fù)載有限,不支持很多用戶訪問(wèn)
- 客戶端點(diǎn)播
2. 流程
- 使用ffmpeg從節(jié)目源拉流
- 推流到nginx-rtmp/flv服務(wù)
- 客戶端從nginx服務(wù)器拉流觀看視頻
3. 本文工具
- ffmpeg
- nginx
- VLC(用來(lái)測(cè)試?yán)?
以下兩個(gè)模塊選擇一個(gè)安裝:
- nginx-http-flv-module
- nginx-rtmp-module
其實(shí)nginx flv也是基于nginx-rtmp-module的流媒體服務(wù)器。
功能對(duì)比:
功能nginx-http-flv-modulenginx-rtmp-module備注HTTP-FLV (播放)√x支持HTTPS-FLV和chunked回復(fù)GOP緩存√x虛擬主機(jī)√x省略listen配置√見備注配置中必須有一個(gè)listen純音頻支持√見備注wait_video或wait_key開啟后無(wú)法工作定時(shí)打印訪問(wèn)記錄√xJSON風(fēng)格的stat√x
二、FFMPEG 的安裝
1. Ubuntu環(huán)境
(1) apt-get安裝
# 可通過(guò)PPA進(jìn)行安裝
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,執(zhí)行sudo ldconfig
為 ffmpeg 加入環(huán)境變量
vi /etc/profile
加入以下內(nèi)容:
export PATH="/usr/local/ffmpeg/bin:$PATH"
然后保存并運(yùn)行source /etc/profile
2. CentOS7(支持nVidia硬件解碼)
硬件環(huán)境:nVidia 20602塊
(1) 安裝nVidia驅(qū)動(dòng)
(A) yum方式安裝nVidia驅(qū)動(dòng)
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
沒(méi)有輸出,說(shuō)明屏蔽默認(rèn)帶有的nouveau成功。
一些命令:
sudo yum install nvidia-detect nvidia顯卡檢測(cè)
nvidia-detect -v 檢測(cè)結(jié)果# yum -y install kmod-nvidia 安裝顯卡驅(qū)動(dòng) 這一步應(yīng)該不需要
(B) run腳本方式安裝nVidia驅(qū)動(dòng)
參考: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同時(shí)運(yùn)行最大數(shù)量的限制 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
(3) 編譯ffmpeg的準(zhǔn)備工作
(i) 庫(kù)說(shuō)明
- x264 編碼H.264視頻,編譯參數(shù)–enable-gpl --enable-libx264
- fdk-aac 編碼AAC音頻,編譯參數(shù)–enable-libfdk-aac
- libvpx VP8/VP9視頻編碼器,編譯參數(shù)–enable-libvpx
- libvorbis 編碼Vorbis音頻,需要libogg。編譯參數(shù)–enable-libvorbis
- libopus 編碼Opus音頻。
- LAME 編碼MP3音頻,編譯參數(shù)–enable-libmp3lame
- libass 字幕渲染器,編譯參數(shù)–enable-libass
(ii) 下載路徑
mkdir ~/ffmpeg_sources
(iii) 配置庫(kù)
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
我實(shí)際編譯去掉了–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環(huán)境
(1) 安裝nginx-http-flv-module模塊
A. 通過(guò)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會(huì)被放在/etc/nginx/http-flv目錄下
- 通過(guò)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,啟動(dòng)或者重啟NGINX來(lái)啟用本模塊:
load_module modules/ngx_http_flv_live_module.so;
# 注意
# 上述的配置必須位于events配置項(xiàng)之前,否則NGINX不能啟動(dòng)。
# 更新可以通過(guò)yum update來(lái)完成。關(guān)于其他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
# 設(shè)置nginx
vi /usr/local/nginx/conf/nginx.conf
nginx.conf內(nèi)容
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;
} } }
# 啟動(dòng)nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 如果要卸載# 如果有自啟動(dòng),則刪除 Nginx 的自啟動(dòng)chkconfig nginx off服務(wù) nginx 信息讀取出錯(cuò):沒(méi)有那個(gè)文件或目錄# 查找nginx的安裝目錄whereis nginxnginx: /usr/local/nginx
# 停止nginx服務(wù)/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環(huán)境安裝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需要有可寫權(quán)限。
上面示例中的推流/拉流地址
http://ip:90/live/test
四、測(cè)試
# 推流測(cè)試
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
# 拉流測(cè)試
使用VLC Media Player打開網(wǎng)址: rtmp://ip:90/live/test
天津市犀思科技有限公司是專業(yè)從事web應(yīng)用定制開發(fā)的一家公司,主營(yíng)業(yè)務(wù)包括定制功能型網(wǎng)站建設(shè)開發(fā)、微信小程序開發(fā)、微信公眾號(hào)開發(fā)、APP定制開發(fā)、天津企業(yè)微信開發(fā)、ERP、CRM、OA等企業(yè)應(yīng)用場(chǎng)景信息化解決方案等服務(wù),致力于成為中國(guó)領(lǐng)先的IT服務(wù)及行業(yè)解決方案的提供商。

