FFmpeg安装及将rtsp流转hls协议流通过nginx进行视频直播
在工作中遇到需要將海康威視等的rtsp協議的監控視頻在瀏覽器上播放,如果直接通過瀏覽器播放需要在客戶pc上安裝插件,明顯這是不可行的,在網上找了一段時間的方案后確定了通過ffmpeg將rtsp轉成hls協議的視頻流再通過nginx轉達的方式進行視頻播放的方案,hls協議是蘋果公司開發的視頻直播協議,它可以通過h5的video標簽進行視頻播放,非常方便,不過也存在一個弊端就是延遲比較大,我這邊上線之后的延遲在30s左右,對于我們公司的業務需求來說是可以接受的,如不能接受需謹慎選擇,另外如果那位大神知道怎么調優,降低延遲還望多多交流<抱拳>。
話不多說,開始我們的正題吧。說明:以下步驟需在有網情況下進行,如需離線安裝,需自行下載安裝包,我這里會將部分安裝包上傳。
1、安裝依賴
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64 gcc-c++
2、安裝ffmpeg的依賴
++++++++Yasm+++++++++++
#wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
++++++++x264+++++++++++
#附件中有該安裝包
#cd?x264-snapshot-20190626-2245
#./configure --enable-shared?
#make
#make install
++++++++LAME+++++++++++
#wget 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 --enable-nasm
#make
#make install
++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
#tar xzvf libogg-1.3.3.tar.gz
#cd libogg-1.3.3
#./configure
#make
#make install
++++++++libvorbis+++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.6.tar.gz
#tar xzvf libvorbis-1.3.6.tar.gz
#cd libvorbis-1.3.6
#./configure
#make
#make install
++++++++libvpx+++++++++++
#附件中有該安裝包
#cd libvpx
#./configure??--enable-shared
#make
#make install
++++++++FAAD2+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tar zxvf faad2-2.7.tar.gz
#cd faad2-2.7
#./configure
#make
#make install
++++++++Xvid+++++++++++
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.5.tar.gz
#tar zxvf xvidcore-1.3.5.tar.gz
#cd xvidcore/build/generic
#./configure
#make
#make install
++++++++ffmpeg+++++++++++
#附件中有安裝包
#cd ffmpeg-4.1.3
#./configure --prefix=/usr/local/ffmpeg/ --enable-version3 --enable-libvpx --enable-libmp3lame --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads
#make && make install
安裝ffmpeg的過程較長,請耐心等待,安裝成功之后,ffmpeg安裝到了/usr/local/ffmpeg目錄下。
修改/etc/ld.so.conf如下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/usr/local/ffmpeg/lib
報錯退出,執行以下命令
#ldconfig
4、錯誤處理
如果在安裝ffmpeg過程中出現某些依賴版本過低或不存在,請自行安裝相應依賴。
*** Could not run Ogg test program, checking why... *** The test program compiled, but did not run. This usually means *** that the run-time linker is not finding Ogg or finding the wrong *** version of Ogg. If it is not finding Ogg, you'll need to set your *** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point *** to the installed location ?Also, make sure you have run ldconfig if that *** is required on your system *** *** If you have an old version installed, it is best to remove it, although *** you may also be able to get things to work by modifying LD_LIBRARY_PATH
configure: error: must have Ogg installed!
確定已經安裝了libogg 然后安裝libvorbis報類似如上錯誤時,可以執行以下命令解決:
echo /usr/local/lib >> /etc/ld.so.conf; ldconfig
vi /etc/profile?
文件最后面 添加?
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"退出保持?
source /etc/profile
3、安裝nginx和rtmp模塊
下載pcre、zlib、openssl,進行編譯安裝
到nginx官網下載nginx,解壓
到?https://github.com/arut/nginx-rtmp-module/releases?下載rtmp模塊,并解壓
編譯安裝nginx-rtmp,進入nginx的解壓包下
./configure --prefix=/usr/local/nginx-1.17.1-rtmp --add-module=../nginx-rtmp-module-1.2.1 --with-http_stub_status_module
make
make install
安裝完成之后,nginx被安裝到/usr/local/nginx-1.17.1-rtmp目錄下
4、修改nginx配置文件,使其可以分發hls協議視頻流進行直播
user root; worker_processes 1;error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024; }rtmp{server{listen 1935;chunk_size 4000;#liveapplication myapp {live on;hls on;hls_path /tmp/hls;hls_fragment 2s;hls_playlist_length 6s;}} }http{include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server{listen 8090;server_name dengxingyao.cn;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#hls location /hls {types {application/vnd.apple.mpegusr m3u8;video/mp2t ts;}root /usr/local/nginx-1.17.1-rtmp/temp;add_header Cache-Control no-cache;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}} }上述配置文件的/hls既是nginx通過http請求分發hls協議的.ts文件的配置,該配置表明.m3u8和.ts文件位于/usr/local/nginx-1.17.1-rtmp/temp目錄下
?5、通過ffmpeg將rtsp協議視頻流轉成hls協議
nohup ./ffmpeg -i rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov -strict -2 -c copy -map 0 -f segment -segment_list /usr/local/nginx-1.17.1-rtmp/temp/hls/ipc1/ipc1.m3u8 -segment_time 5 /usr/local/nginx-1.17.1-rtmp/temp/hls/ipc1/output%03d.ts &?其中rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov為rtsp視頻源地址,/usr/local/nginx-1.17.1-rtmp/temp/hls/ipc1/ipc1.m3u8是轉換成的.m3u8文件,其保存在上面nginx配置文件中配置的目錄下
6、通過VLC播放器可播放rtsp源和轉換后的hls視頻源進行測試
通過上述操作后的hls視頻源地址為:http://ip:8090/hls/ipc1/ipc1.m3u8
7、上述所有安裝包資源如下,歡迎下載
https://download.csdn.net/download/ccf199201261/11263464
參考博客:https://blog.csdn.net/wutong_login/article/details/42292787
?
總結
以上是生活随笔為你收集整理的FFmpeg安装及将rtsp流转hls协议流通过nginx进行视频直播的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv移植
- 下一篇: c语言void*指针,C语言void指针