以下两个模块选择一个安装:
nginx-http-flv-modulenginx-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中onvif客户端c源码,末尾添加 /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.confdist-blacklist.conf
#blacklist nvidiafbblacklist nouveauoptions nouveau modeset=0mv /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 cudanvidia-smi(3) 编译ffmpeg的准备工作(i) 库说明x264 编码H.264视频,编译参数–enable-gpl --enable-libx264fdk-aac 编码AAC音频,编译参数–enable-libfdk-aaclibvpx VP8/VP9视频编码器,编译参数–enable-libvpxlibvorbis 编码Vorbis音频,需要libogg。编译参数–enable-libvorbislibopus 编码Opus音频。LAME 编码MP3音频,编译参数–enable-libmp3lamelibass 字幕渲染器,编译参数–enable-libass(ii) 下载路径mkdir ~/ffmpeg_sources(iii) 配置库yasmcd ~/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 installnasmcurl -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 installlibfdk-aacgit clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aaccd fdk-aacsudo autoreconf -fiv./configure --prefix="/usr" --disable-sharedmakesudo make installlamecurl -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 installlibopuscurl -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 installliboggcurl -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 installlibvorbiscurl -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 installlibtheoragit 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 installlibx265sudo 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
x264curl -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 installdisable-gpl./configure --prefix="/usr" --enable-static --disable-gpl --disable-openclmakesudo make install安装ffnvcodecgit clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.gitcd nv-codec-headersmake && sudo make installlibvpxVP8/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 installhttpswget 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.confyum 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.confnginx.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标签: onvif客户端c源码