環(huán)境
ubuntu16.04 cuda8.0 cudnn7 geforce gtx 1080 ti
image.png
ffmpeg的硬編碼
nvidia
- h264_nvenc
- hevc_nvenc wrappers
硬解碼
- cuvid
- NVDEC
版本需要https://developer.nvidia.com/ffmpeg
image.png
gtx 1080ti屬于pascal懒震,所以應(yīng)該安裝ffmpeg3.3以上
支持情況
image.png
image.png
請參照 https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
編譯
- 卸載x264 ffmpeg
sudo apt-get -y remove ffmpeg x264 libx264-dev
- 卸載之前編譯的x264
make uninstall ##刪除由make install命令安裝的文件
make clean ##只刪除make時(shí)產(chǎn)生的臨時(shí)文件
make distclean ##同時(shí)刪除configure和make產(chǎn)生的臨時(shí)文件
官網(wǎng)上的步驟https://developer.nvidia.com/ffmpeg
image.png
-
安裝ffnvcodec
image.png
4.安裝nasm
tar -zxvf nasm-2.15.tar.gz
cd nasm-2.15
./configure -prefix=/storage/lss/ffmpegbuild/ # 自己的路徑
make
sudo make install
- 安裝libx264
cd x264
./configure --prefix=/storage/lss/ffmpegbuild/ --enable-static --enable-shared
make
sudo make install
6.安裝yasm
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
/configure --prefix=/storage/lss/ffmpegbuild
make
make install
- 安裝x265
https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
我沒有完全按照官網(wǎng)的命令
sudo apt-get install libnuma-dev
git clone https://bitbucket.org/multicoreware/x265_git.git
cd x265_git/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/storage/lss/ffmpegbuild ../../source
make
sudo make install
- 安裝ffmpeg
https://developer.nvidia.com/ffmpeg
git clone https://git.ffmpeg.org/ffmpeg.git
./configure --prefix=/storage/lss/ffmpegbuild/ --enable-libx264
--enable-libx265
--enable-cuda-sdk
--enable-cuvid
--enable-nvenc
--enable-nonfree
--enable-gpl
--enable-libnpp
--extra-cflags=-I/storage/lss/ffmpegbuild/include
--extra-ldflags=-L/storage/lss/ffmpegbuild/lib
--extra-cflags=-I/usr/local/cuda-8.0/include
--extra-ldflags=-L/usr/local/cuda-8.0/lib64
# 報(bào)錯(cuò)1找不到x264或者npp
# 原因: x264或者cuda的路徑?jīng)]寫對
# 報(bào)錯(cuò)2ERROR: x265 not found using pkg-config
# 解決方法:在ffmpegbuild/lib/下面找到了一個(gè)pkgconfig,試著導(dǎo)入嗤详,成功解決
export PKG_CONFIG_PATH=/storage/lss/ffmpegbuild/lib/pkgconfig
# 警告 WARNING: Option --enable-cuda-sdk is deprecated. Use --enable-cuda-nvcc instead
#替換configure
./configure --prefix=/storage/lss/ffmpegbuild/ --enable-libx264 --enable-libx265 --enable-cuda-nvcc --enable-cuvid --enable-nvenc --enable-nonfree --enable-gpl --enable-libnpp --extra-cflags=-I/storage/lss/ffmpegbuild/include --extra-ldflags=-L/storage/lss/ffmpegbuild/lib --extra-cflags=-I/usr/local/cuda-8.0/include --extra-ldflags=-L/usr/local/cuda-8.0/lib64
make
# 報(bào)錯(cuò) Makefile:117: recipe for target 'ffmpeg_g' failed
# 解決方法:make clean一下
make clean
# 然后重新configure
#接著
make -j 4
sudo make install
由于本機(jī)上有一個(gè)ffmpeg个扰,需要把環(huán)境變量變成新編譯的這個(gè),所以修改環(huán)境變量葱色,把路徑改成自己的/storage/lss/ffmpegbuild
image.png
調(diào)用ffmpeg后報(bào)錯(cuò)
ffmpeg: error while loading shared libraries: libx265.so.198: cannot open shared object file: No such file or directory
使用如下方案解決
image.png
測試
1. 將MP4轉(zhuǎn)化為flv成功
ffmpeg -i orange.mp4 -c:v libx264 output.flv
2. 調(diào)用265成功
ffmpeg -i orange.mp4 -c:v libx265 -c:a copy output.mp4
2. 使用硬編碼
ffmpeg -i orange.mp4 -c:v h264_nvenc output.mp4
報(bào)錯(cuò)
image.png
經(jīng)過上網(wǎng)搜递宅,我認(rèn)為可能的原因是ffmpeg版本太高了,所以將ffmpeg卸載重新編譯一個(gè)更低版本的
從https://git.ffmpeg.org/ffmpeg.git查看ffmpeg的版本苍狰,git一個(gè)低版本的ffmpeg
git clone -b <tag> https://git.ffmpeg.org/ffmpeg.git # git指定版本的ffmpeg
重新編譯
會發(fā)現(xiàn)--enable-cuda-nvcc 會報(bào)錯(cuò)办龄,說沒有這個(gè)選項(xiàng),通過
./configure --help
查看所有的option發(fā)現(xiàn)沒有這個(gè)選項(xiàng)淋昭,可能是因?yàn)檫@個(gè)版本的不支持這個(gè)選項(xiàng)俐填,我又git一個(gè)最新版本的ffmpeg,然后在ffmpeg文件夾中從master切換到了n3.4分支翔忽,使用下面的命令切換
git checkout n3.4
重新編譯
編譯的過程中會報(bào)錯(cuò)英融,我搜了一下盏檐,有人說可能也是版本的問題,所以又換了一個(gè)版本 n3.4.3重新編譯 編譯成功
4. 使用新版本的ffmpeg調(diào)用硬編碼
ffmpeg -i orange.mp4 -c:v h264_nvenc output.mp4
成功,查看gpu調(diào)用情況驶悟,nvidia-smi
,發(fā)現(xiàn)確實(shí)調(diào)用了gpu
image.png
重新編譯ffmpeg使其支持ffplay
#安裝包
sudo apt-get install libsdl-image1.2-dev
sudo apt-get install libsdl-mixer1.2-dev
sudo apt-get install libsdl-ttf2.0-dev
sudo apt-get install libsdl-gfx1.2-dev
官網(wǎng)下載sdl
https://www.libsdl.org/download-2.0.php
解壓:tar -zxvf SDL2-2.0.12.tar.gz
cd SDL2-2.0.12
./configure --prefix=/storage/lss/ffmpegbuild
make
make install
配置ffmpeg
./configure --prefix=/storage/lss/ffmpegbuild/ --enable-libx264 --enable-libx265 --enable-cuda-sdk --enable-cuvid --enable-nvenc --enable-nonfree --enable-gpl --enable-libnpp --extra-cflags=-I/storage/lss/ffmpegbuild/include --extra-ldflags=-L/storage/lss/ffmpegbuild/lib --extra-cflags=-I/usr/local/cuda-8.0/include --extra-ldflags=-L/usr/local/cuda-8.0/lib64 --enable-sdl
make -j 4
sudo make install