ffmpeg使用nvidia硬件加速编译过程
下载 ffmpeg 源码包
下载地址:https://github.com/FFmpeg/FFmpeg 注意选择版本
bash
wget https://github.com/FFmpeg/FFmpeg/archive/refs/heads/release/6.1.zip -O ffmpeg-6.1.zip
安装依赖
bash
sudo apt install yasm libgmp-dev libass-dev libx264-dev libx265-dev libdav1d-dev libsvtav1-dev libsvtav1enc-dev libwebp-dev libvpx-dev libfdk-aac-dev libplacebo-dev libvpl-dev libplacebo-dev libvpl
sudo apt install autoconf automake build-essential cmake git-core libass-dev libfreetype6-dev libgnutls28-dev libmp3lame-dev libsdl2-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev meson ninja-build pkg-config texinfo wget yasm zlib1g-dev nasm
解压源码包并配置
bash
./configure --prefix=/opt/ffmpeg --target-os=linux --disable-doc --disable-ffplay --disable-ptx-compression --disable-static --disable-libxcb --disable-sdl2 --disable-xlib --enable-lto --enable-gpl --enable-version3 --enable-shared --enable-gmp --enable-gnutls --enable-libdrm --enable-libass --enable-libdav1d --enable-libsvtav1 --enable-libwebp --enable-libvpx --enable-libx264 --enable-libx265 --enable-libfdk-aac --arch=amd64 --enable-libplacebo --enable-libvpl --enable-ffnvcodec --enable-cuda --enable-cuvid --enable-nvdec --enable-nvenc --enable-nonfree
编译并安装
bash
make -j
make install
后续工作
INFO
将 ffmpeg 可执行文件与动态库加入到环境变量中
bash
cat >> ~/.bashrc <<"EOF"
function load_ffmpeg(){
ff_home=/opt/ffmpeg
export PATH=$ff_home/bin:$PATH
export LD_LIBRARY_PATH=$ff_home/lib:$LD_LIBRARY_PATH
}
load_ffmpeg
EOF
重启shell并验证
shell
$ ffmpeg -codecs | grep x264
ffmpeg version 6.1.2 Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 11 (Ubuntu 11.4.0-1ubuntu1~22.04)
configuration: --prefix=/opt/bin --target-os=linux --disable-doc --disable-ffplay --disable-ptx-compression --disable-static --disable-libxcb --disable-sdl2 --disable-xlib --enable-lto --enable-gpl --enable-version3 --enable-shared --enable-gmp --enable-gnutls --enable-libdrm --enable-libass --enable-libdav1d --enable-libsvtav1 --enable-libwebp --enable-libvpx --enable-libx264 --enable-libx265 --enable-libfdk-aac --arch=amd64 --enable-libplacebo --enable-libvpl --enable-ffnvcodec --enable-cuda --enable-cuvid --enable-nvdec --enable-nvenc --enable-nonfree
libavutil 58. 29.100 / 58. 29.100
libavcodec 60. 31.102 / 60. 31.102
libavformat 60. 16.100 / 60. 16.100
libavdevice 60. 3.100 / 60. 3.100
libavfilter 9. 12.100 / 9. 12.100
libswscale 7. 5.100 / 7. 5.100
libswresample 4. 12.100 / 4. 12.100
libpostproc 57. 3.100 / 57. 3.100
DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_v4l2m2m h264_qsv h264_cuvid) (encoders: libx264 libx264rgb h264_nvenc h264_qsv h264_v4l2m2m h264_vaapi)
简化使用ffmpeg的函数
bash
#!/bin/bash
function ffpp(){
fullname=$(basename "$1")
newname=${fullname%.*}
dirr=$(dirname "$1")
ext=${fullname#*.}
ext="mp4"
optimizedoutput="$dirr"/optimized-"$newname".$ext
if [[ -f "$optimizedoutput" ]]; then
return 0
fi
ffmpeg -hwaccel cuvid -i "$1" -c copy -movflags +faststart -brand iso6 "$optimizedoutput"
# ffmpeg -hwaccel cuvid -i "$1" -c:v copy -c:a copy -movflags +faststart -brand iso6 -y "$output"
# ffmpeg -hwaccel cuvid -i "$1" -c:v h264_nvenc -preset fast -crf 18 -b:v 2M -maxrate 10M -bufsize 20M -c:a aac -b:a 192k "$output"
if [ $? -eq 0 ]; then
echo infoo-"$1" >> ffpp.log
else
echo error-"$1" >> ffpp.log
fi
echo ""
echo ""
echo ""
}
function mvoptimizetoori(){
fullname=$(basename "$1")
newname=${fullname%.*}
dirr=$(dirname "$1")
ext=${fullname#*.}
optimizedoutput="$dirr"/optimized-"$newname".$ext
if [[ -f "$optimizedoutput" ]]; then
orioutput="$dirr"/ori-"$newname".$ext
mv "$1" "$orioutput" && mv "$optimizedoutput" "$1"
fi
}
for file in movie/*/*.mp4; do
fullname=$(basename "$file")
if [[ "$fullname" == optimized* ]]; then
continue
fi
ffpp "$file"
mvoptimizetoori "$file"
done