Author: dongdong
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比鹽津網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式鹽津網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋鹽津地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。
Email: ldyldy828@126.com
本文內(nèi)容包括:
在FFmpeg官網(wǎng)https://ffmpeg.org/download.html可以下載到ubunto/debian的發(fā)行包,其他Linux發(fā)行版需自行編譯。同時(shí),如果要使用GPU進(jìn)行硬件加速的話,也是必須自己編譯FFmpeg的,所以本節(jié)將介紹從源碼編譯安裝FFmpeg的方法(基于RHEL/Centos)
yum install autoconf automake bzip2 cmake freetype-devel gcc gcc-c++ git libtool make mercurial pkgconfig zlib-devel
在$HOME下創(chuàng)建ffmpeg_sources目錄
我自己定義的HOME=/home/local/
本節(jié)中的依賴庫基本都是必須的,建議全部安裝
匯編編譯器,編譯某些依賴庫的時(shí)候需要
cd /home/local/ffmpeg_sources
curl -O -L http://www.nasm.us/pub/nasm/releasebuilds/2.13.02/nasm-2.13.02.tar.bz2
tar xjvf nasm-2.13.02.tar.bz2
cd nasm-2.13.02
./autogen.sh
./configure --prefix="/home/local/ffmpeg_build" --bindir="/home/local/bin"
make
make install
匯編編譯器,編譯某些依賴庫的時(shí)候需要
cd /home/local/ffmpeg_sources
curl -O -L http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="/home/local/ffmpeg_build" --bindir="/home/local/bin"
make
make install
H.264視頻編碼器,如果需要輸出H.264編碼的視頻就需要此庫,所以可以說是必備
cd /home/local/ffmpeg_sources
git clone --depth 1 http://git.videolan.org/git/x264
cd x264
PKG_CONFIG_PATH="/home/local/ffmpeg_build/lib/pkgconfig" ./configure --prefix="/home/local/ffmpeg_build" --bindir="/home/local/bin" --enable-static
make
make install
H.265/HEVC視頻編碼器。
如果不需要此編碼器,可以跳過,并在ffmpeg的configure命令中移除--enable-libx265
cd /home/local/ffmpeg_sources
hg clone https://bitbucket.org/multicoreware/x265
cd ~/ffmpeg_sources/x265/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="/home/local/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install
AAC音頻編碼器,必備
cd /home/local/ffmpeg_sources
git clone --depth 1 --branch v0.1.6 https://github.com/mstorsjo/fdk-aac.git
cd fdk-aac
autoreconf -fiv
./configure --prefix="/home/local/ffmpeg_build" --disable-shared
make
make install
MP3音頻編碼器,必備
cd /home/local/ffmpeg_sources
curl -O -L http://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
tar xzvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix="/home/local/ffmpeg_build" --bindir="/home/local/bin" --disable-shared --enable-nasm
make
make install
OPUS音頻編碼器
如果不需要此編碼器,可以跳過,并在ffmpeg的configure命令中移除--enable-libopus
cd /home/local/ffmpeg_sources
curl -O -L https://archive.mozilla.org/pub/opus/opus-1.2.1.tar.gz
tar xzvf opus-1.2.1.tar.gz
cd opus-1.2.1
./configure --prefix="/home/local/ffmpeg_build" --disable-shared
make
make install
被libvorbis依賴
cd /home/local/ffmpeg_sources
curl -O -L 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 --prefix="/home/local/ffmpeg_build" --disable-shared
make
make install
Vorbis音頻編碼器
如果不需要此編碼器,可以跳過,并在ffmpeg的configure命令中移除--enable-libvorbis
cd /home/local/ffmpeg_sources
curl -O -L http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.5.tar.gz
tar xzvf libvorbis-1.3.5.tar.gz
cd libvorbis-1.3.5
./configure --prefix="/home/local/ffmpeg_build" --with-ogg="/home/local/ffmpeg_build" --disable-shared
make
make install
VP8/VP9視頻編/解碼器
如果不需要此編/解碼器,可以跳過,并在ffmpeg的configure命令中移除--enable-libvpx
cd /home/local/ffmpeg_sources
git clone --depth 1 https://github.com/webmproject/libvpx.git
cd libvpx
./configure --prefix="/home/local/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
make
make install
cd /home/local/ffmpeg_sources
curl -O -L https://ffmpeg.org/releases/ffmpeg-3.3.8.tar.bz2
tar xjvf ffmpeg-3.3.8.tar.bz2
cd ffmpeg-3.3.8
PATH="/home/local/ffmpeg_sources/bin:$PATH" PKG_CONFIG_PATH="/home/local/ffmpeg_sources/ffmpeg_build/lib/pkgconfig" ./configure --prefix="/home/local/ffmpeg_sources/ffmpeg_build" --pkg-config-flags="--static" --extra-cflags="-I /home/local/ffmpeg_sources/ffmpeg_build/include" --extra-ldflags="-L /home/local/ffmpeg_sources/ffmpeg_build/lib" --extra-libs=-lpthread --extra-libs=-lm --bindir="$HOME/bin" --enable-gpl --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-libfreetype
make
make install
hash -r
驗(yàn)證安裝
ffmpeg -h
通過ffprobe命令識(shí)別并輸出視頻信息
ffprobe -v error -show_streams -print_format json
為方便程序解析,將視頻信息輸出為json格式,樣例如下:
{
"streams": [
{
"index": 0,
"codec_name": "h364",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "High",
"codec_type": "video",
"codec_time_base": "61127/3668400",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 1920,
"height": 1080,
"coded_width": 1920,
"coded_height": 1080,
"has_b_frames": 0,
"sample_aspect_ratio": "0:1",
"display_aspect_ratio": "0:1",
"pix_fmt": "yuv420p",
"level": 40,
"color_range": "tv",
"color_space": "bt709",
"color_transfer": "bt709",
"color_primaries": "bt709",
"chroma_location": "left",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"r_frame_rate": "30/1",
"avg_frame_rate": "1834200/61127",
"time_base": "1/600",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 61127,
"duration": "101.878333",
"bit_rate": "16279946",
"bits_per_raw_sample": "8",
"nb_frames": "3057",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"rotate": "90",
"creation_time": "2018-08-09T09:13:33.000000Z",
"language": "und",
"handler_name": "Core Media Data Handler",
"encoder": "H.264"
},
"side_data_list": [
{
"side_data_type": "Display Matrix",
"displaymatrix": "\n00000000: 0 65536 0\n00000001: -65536 0 0\n00000002: 70778880 0 1073741824\n",
"rotation": -90
}
]
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "LC",
"codec_type": "audio",
"codec_time_base": "1/44100",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "44100",
"channels": 1,
"channel_layout": "mono",
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/44100",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 4492835,
"duration": "101.878345",
"bit_rate": "91595",
"max_bit_rate": "96000",
"nb_frames": "4390",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"creation_time": "2018-08-09T09:13:33.000000Z",
"language": "und",
"handler_name": "Core Media Data Handler"
}
},
{
"index": 2,
"codec_type": "data",
"codec_tag_string": "mebx",
"codec_tag": "0x7862656d",
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/600",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 61127,
"duration": "101.878333",
"bit_rate": "119",
"nb_frames": "17",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"creation_time": "2018-08-09T09:13:33.000000Z",
"language": "und",
"handler_name": "Core Media Data Handler"
}
},
{
"index": 3,
"codec_type": "data",
"codec_tag_string": "mebx",
"codec_tag": "0x7862656d",
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/600",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 61127,
"duration": "101.878333",
"nb_frames": "1",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"creation_time": "2018-08-09T09:13:33.000000Z",
"language": "und",
"handler_name": "Core Media Data Handler"
}
}
]
}
可以看到一共返回了4個(gè)流,其中第0個(gè)是視頻流,1是音頻流,2和3是附加數(shù)據(jù),沒什么用
如果想指定分析視頻流或音頻流的話,可以加上參數(shù)-show_streams -v
或-show_streams -a
,這樣就會(huì)只輸出視頻/音頻流的分析結(jié)果
ffmpeg -i -c:v libx264 -b:v 2048k -vf scale=1280:-1 -y
上述命令將輸入視頻轉(zhuǎn)碼為h364編碼的視頻
ffmpeg -h encoder=libx264
命令查詢,如轉(zhuǎn)碼為其他編碼,也可使用類似命令查詢可用參數(shù)重頭戲來了,這塊的資料相當(dāng)少,我也是費(fèi)了一番力氣才搞定
CUDA是Nvidia出的一個(gè)GPU計(jì)算庫,讓程序員可以驅(qū)動(dòng)Nvidia顯卡的GPU進(jìn)行各種工作,其中就包含了視頻的編解碼
首先驗(yàn)證一下顯卡驅(qū)動(dòng)是否裝好
nvidia-smi
如果驅(qū)動(dòng)正常的話,此命令會(huì)輸出顯卡的型號(hào)、驅(qū)動(dòng)版本、現(xiàn)存/GPU占用等信息。如何安裝顯卡驅(qū)動(dòng)本文不描述,請(qǐng)參考其他資料。
到CUDA官網(wǎng)https://developer.nvidia.com/cuda-downloads下載對(duì)應(yīng)平臺(tái)的發(fā)行包,這里我選擇
wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda-repo-rhel7-10-2-local-10.2.89-440.33.01-1.0-1.x86_64.rpm
sudo rpm -i cuda-repo-rhel7-10-2-local-10.2.89-440.33.01-1.0-1.x86_64.rpm
sudo yum clean all
sudo yum -y install nvidia-driver-latest-dkms cuda
sudo yum -y install cuda-drivers
一共大概要安裝90多個(gè)依賴庫,注意一下安裝完成后的報(bào)告,我首次安裝時(shí)有一個(gè)庫不知道為什么安裝失敗了,又單獨(dú)yum install
了該庫一次才成功
/usr/local/cuda-9.2/bin/nvcc -V
安裝成功的話,會(huì)輸出類似文本:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Tue_Jun_12_23:07:04_CDT_2018
Cuda compilation tools, release 9.2, V9.2.148
要讓ffmpeg能夠使用CUDA提供的GPU編解碼器,必須重新編譯ffmpeg,讓其能夠通過動(dòng)態(tài)鏈接調(diào)用CUDA的能力
首先要編譯安裝nv-codec-headers庫
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
make PREFIX="/home/local/ffmpeg_build" BINDDIR="/home/local/bin"
make install PREFIX="/home/local/ffmpeg_build" BINDDIR="/home/local/bin"
進(jìn)入/home/local/ffmepg_sources/ffmpeg-3.3.8/
目錄重新執(zhí)行ffmpeg的編譯和安裝
注意configure命令參數(shù)和之前configure命令參數(shù)的區(qū)別
PATH="/home/local/ffmpeg_sources/bin:$PATH" PKG_CONFIG_PATH="/home/local/ffmpeg_sources/ffmpeg_build/lib/pkgconfig" ./configure --prefix="/home/local/ffmpeg_sources/ffmpeg_build" --pkg-config-flags="--static" --extra-cflags="-I /home/local/ffmpeg_sources/ffmpeg_build/include -I/usr/local/cuda/include" --extra-ldflags="-L /home/local/ffmpeg_sources/ffmpeg_build/lib -L/usr/local/cuda/lib64" --extra-libs=-lpthread --extra-libs=-lm --bindir="$HOME/bin" --enable-gpl --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-nonfree --enable-libfreetype --enable-cuda --enable-cuvid --enable-nvenc --enable-libnpp
重新安裝完ffmpeg,使用ffmpeg -hwaccels
命令查看支持的硬件加速選項(xiàng)
Hardware acceleration methods:
cuvid
可以看到多出來一種叫做cuvid的硬件加速選項(xiàng),這就是CUDA提供的GPU視頻編解碼加速選項(xiàng)
然后查看cuvid提供的GPU編解碼器ffmpeg -codecs | grep cuvid
DEV.LS h364 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h364 h364_cuvid ) (encoders: libx264 libx264rgb h364_nvenc nvenc nvenc_h364 )
DEV.L. hevc H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_cuvid ) (encoders: libx265 nvenc_hevc hevc_nvenc )
DEVIL. mjpeg Motion JPEG (decoders: mjpeg mjpeg_cuvid )
DEV.L. mpeg1video MPEG-1 video (decoders: mpeg1video mpeg1_cuvid )
DEV.L. mpeg2video MPEG-2 video (decoders: mpeg2video mpegvideo mpeg2_cuvid )
DEV.L. mpeg4 MPEG-4 part 2 (decoders: mpeg4 mpeg4_cuvid )
D.V.L. vc1 SMPTE VC-1 (decoders: vc1 vc1_cuvid )
DEV.L. vp8 On2 VP8 (decoders: vp8 libvpx vp8_cuvid ) (encoders: libvpx )
DEV.L. vp9 Google VP9 (decoders: vp9 libvpx-vp9 vp9_cuvid ) (encoders: libvpx-vp9 )
所有帶有"cuvid"或"nvenc"的,都是CUDA提供的GPU編解碼器
可以看到,我們現(xiàn)在可以進(jìn)行h364/hevc/mjpeg/mpeg1/mpeg2/mpeg4/vc1/vp8/vp9格式的GPU解碼,以及h364/hevc格式的GPU編碼
用GPU進(jìn)行轉(zhuǎn)碼的命令和軟轉(zhuǎn)碼命令不太一樣,CPU轉(zhuǎn)碼的時(shí)候,我們可以依賴ffmpeg識(shí)別輸入視頻的編碼格式并選擇對(duì)應(yīng)的解碼器,但ffmpeg只會(huì)自動(dòng)選擇CPU解碼器,要讓ffmpeg使用GPU解碼器,必須先用ffprobe識(shí)別出輸入視頻的編碼格式,然后在命令行中指定對(duì)應(yīng)的GPU解碼器。
例如,將h364編碼的源視頻轉(zhuǎn)碼為指定尺寸和碼率的h364編碼視頻:
ffmpeg -hwaccel cuvid -c:v h364_cuvid -i -c:v h364_nvenc -b:v 2048k -vf scale_npp=1280:-1 -y
轉(zhuǎn)碼期間使用nvidia-smi查看顯卡狀態(tài),能夠看到ffmpeg確實(shí)是在使用GPU進(jìn)行轉(zhuǎn)碼:
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 62543 C ffmpeg 193MiB |
+-----------------------------------------------------------------------------+
在配有兩顆Intel-E5-2630v3 CPU和兩塊Nvidia Tesla M4顯卡的服務(wù)器上,進(jìn)行h364視頻轉(zhuǎn)碼測試,成績?nèi)缦拢?/p>
并行轉(zhuǎn)碼時(shí),CPU軟轉(zhuǎn)的效率有所提高,3個(gè)轉(zhuǎn)碼任務(wù)并行時(shí)32顆核心全被占滿,此時(shí)的成績
不難看出,并行時(shí)GPU的轉(zhuǎn)碼速度并沒有提高,可見一顆GPU同時(shí)只能執(zhí)行一個(gè)轉(zhuǎn)碼任務(wù)。那么,如果服務(wù)器上插有多塊顯卡,ffmpeg是否會(huì)使用多顆GPU進(jìn)行并行轉(zhuǎn)碼呢?
很遺憾,答案是否。
ffmpeg并不具備自動(dòng)向不同GPU分配轉(zhuǎn)碼任務(wù)的能力,但經(jīng)過一番調(diào)查后,發(fā)現(xiàn)可以通過-hwaccel_device參數(shù)指定轉(zhuǎn)碼任務(wù)使用的GPU!
ffmpeg -hwaccel cuvid -hwaccel_device 0 -c:v h364_cuvid -i -c:v h364_nvenc -b:v 2048k -vf scale_npp=1280:-1 -y
此時(shí)nvidia-smi顯示:
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 96931 C ffmpeg 193MiB |
| 1 96930 C ffmpeg 193MiB |
+-----------------------------------------------------------------------------+
可以進(jìn)行并行GPU轉(zhuǎn)碼了!
那么在占滿服務(wù)器資源時(shí),GPU轉(zhuǎn)碼和CPU轉(zhuǎn)碼的效率如下:
GPU效率是CPU的4.5倍
一、錯(cuò)誤: ./autogen.sh: 4: ./autogen.sh: autoreconf: not found
解決:yum -y install autoconf automake libtool
然后執(zhí)行
二、 錯(cuò)誤:ERROR: freetype2 not found using pkg-config
yum install -y bzip2
wget http://download.savannah.gnu.org/releases/freetype/freetype-2.10.0.tar.bz2
tar -jxvf freetype-2.10.0.tar.bz2
cd freetype-2.10.0
./configure --prefix= /home/local/ffmpeg_sources/ffmpeg_build
make
make install
三、錯(cuò)誤: configure: error: C compiler cannot create executables See `config.log' for more details make: *** [setup] Error 77
解決:先卸載舊的c c++ gcc gcc++
然后重新安裝
四、錯(cuò)誤:make: *** [libavcodec/libfdk-aacenc.o] Error 1
因本身3.*帶有BUG換成4.1 官網(wǎng)下載地址
(此步驟也可以使用git clone下載源碼包,本質(zhì)上是一樣的 )
然后重新編譯即可。
參考鏈接:https://www.jianshu.com/p/59da3d350488