ffmpeg linux安装_音视频开发2. FFMPEG+Nginx实现推流服务

4f1f9bf03a614106d47b66868be74ba1.png

一、说明

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 updatesudo apt-get install ffmpeg# 查看是否安装成功:ffmpeg -version

(2) 编译安装

sudo apt-get install -y autoconf automake build-essential git libass-dev libfreetype6-dev libsdl2-devsudo 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-x86asmsudo 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.orgrpm -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 nvidiafbblacklist nouveauoptions nouveau modeset=0
mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bakdracut /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/nvidiawget https://international.download.nvidia.com/XFree86/Linux-x86_64/430.40/NVIDIA-Linux-x86_64-430.40.runchmod +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 NVENCgit clone https://github.com/keylase/nvidia-patchcd nvidia-patchbash ./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.reposudo yum clean allsudo yum -y install nvidia-driver-latest-dkms cuda
nvidia-smi
e752c82e62f8e469be25bf78c0e3a431.png

(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_sourcescurl -L -O http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gztar xvzf yasm-1.3.0.tar.gzcd 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.xztar xf nasm-2.14.02.tar.xzcd 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-aaccd fdk-aacsudo autoreconf -fiv./configure --prefix="/usr" --disable-sharedmakesudo make install

lame

curl -L -O http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gztar xzvf lame-3.99.5.tar.gzcd lame-3.99.5./configure --prefix="/usr" --disable-shared --enable-nasmmakesudo make install

libopus

curl -L -O https://archive.mozilla.org/pub/opus/opus-1.2.tar.gztar xvzf opus-1.2.tar.gzcd opus-1.2./configure --prefix="/usr" --disable-sharedmakesudo make install

libogg

curl -L -O http://downloads.xiph.org/releases/ogg/libogg-1.3.2.tar.gztar xzvf libogg-1.3.2.tar.gzcd libogg-1.3.2./configure --prefix="/usr" --disable-sharedmakesudo make install

libvorbis

curl -L -O http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.4.tar.gztar xzvf libvorbis-1.3.4.tar.gzcd libvorbis-1.3.4./configure --prefix="/usr" --with-ogg="/usr" --disable-sharedmakesudo make install

libtheora

git clone git://git.xiph.org/mirrors/theora.git theora-gitcd 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 cmakehg clone http://hg.videolan.org/x265cd x265/build/linuxsudo cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="/usr" -DENABLE_SHARED:bool=off ../../sourcemakesudo 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.bz2tar xjvf last_x264.tar.bz2cd x264-snapshot-20170625-2245/./configure --prefix="/usr" --enable-staticmakesudo make install

disable-gpl

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

安装ffnvcodec

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

libvpx

VP8/VP9 video encoder.

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

https

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

(4) 配置ffmpeg, 编译安装

(i) 下载

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

(ii) 配置

PKG_CONFIG_PATH="/usr/lib/pkgconfig" export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATHcd 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.rpmyum 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 rootgit clone https://github.com/winshining/nginx-http-flv-modulewget 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-modulegit clone https://github.com/arut/nginx-rtmp-module.git# 编译安装nginxwget 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# 设置nginxvi /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/nginxrm -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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值