【FFMPEG】Install FFmpeg CUDA gltransition in Ubuntu

因为比较复杂,记录一下自己安装过程,方便后续查找,所有都是在docker环境安装cuda11.7的
**ffmpeg 4.2.2
nv-codec-headers-9.1.23.3 ** 手动下载安装吧

https://github.com/aperim/docker-nvidia-cuda-ffmpeg/blob/v0.1.10/ffmpeg/Dockerfile
最好手动一个一个安装,错误跳过,后面报错再说。
 apt-get update && \
    apt-get -y --allow-change-held-packages full-upgrade && \
    apt-get -y install \
        build-essential \
            cmake \
            curl \
            flite1-dev \
            frei0r-plugins-dev \
            git \
            ladspa-sdk \
            libaom-dev \
            libass-dev \
            libavc1394-dev \
            libbluray-dev \
            libbs2b-dev \
            libc6 \
            libc6-dev \
            libcaca-dev \
            libcdio-dev \
            libcdio-paranoia-dev \
            libcdparanoia-dev \
            libchromaprint-dev \
            libcodec2-dev \
            libdav1d-dev \
            libdc1394-dev \
            libdrm-dev \
            libgles2-mesa-dev \
            libgme-dev \
            libgnutls28-dev \
            libgsm1-dev \
            libiec16022-dev \
            libiec61883-dev \
            libjack-dev \
            liblilv-dev \
            libmfx-dev \
            libmp3lame-dev \
            libmysofa-dev \
            libnettle8 \
            libnuma-dev \
            libnuma1 \
            libomxil-bellagio-dev \
            libopenal-dev \
            libopengl-dev \
            libopenjp2-7-dev \
            libopenmpt-dev \
            libopus-dev \
            libpocketsphinx-dev \
            libpulse-dev \
            librabbitmq-dev \
            librsvg2-dev \
            librubberband-dev \
            libsdl2-gfx-dev \
            libshine-dev \
            libsnappy-dev \
            libsoxr-dev \
            libspeex-dev \
            libsrt-gnutls-dev \
            libssh-dev \
            libtheora-dev \
            libtool \
            libtwolame-dev \
            libunistring-dev \
            libvidstab-dev \
            libvpx-dev \
            libwebp-dev \
            libx264-dev \
            libx265-dev \
            libxvidcore-dev \
            libzimg-dev \
            libzmq3-dev \
            libzvbi-dev \
            lzip \
            pkg-config \
            unzip \
            wget \
            yasm
apt-get install git -y
apt-get install -y vim
apt-get install -y   x265-devel
apt install libavcodec-dev libavformat-dev libavutil-dev(最好对比最上面的apt-get是否安装了)
(比如yasm在上面已输入命令,但是还是需要手动安装的原因,是因为上述安装报错了,导致一些安装包没安装,虽然一些安装包也不需要,这里需要一步一步对照测一下哪些安装包是上述没有安装好的,后面又报错的,上述有的再apt-get install 安装包)
apt-get install wget
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
sudo tar -xvf yasm-1.3.0.tar.gz
time tar -xvf yasm-1.3.0.tar.gz
ls
cd yasm-1.3.0
./configure
make

后面修改显卡计算能力查询用:https://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/
apt install libavcodec-dev libavformat-dev libavutil-dev
在ffmpeg文件下
git clone https://github.com/transitive-bullshit/ffmpeg-gl-transition.git

#修改文件
#在libavfilter/Makefile里加入
OBJS-$(CONFIG_GLTRANSITION_FILTER) += vf_gltransition.o
#在Libavfilter/aLLfiLters.c加入
extern AVFilter ff_vf_gltransition;


cp ffmpeg-gl-transition/vf_gltransition.c libavfilter/
 RUN grep -v "define GL_TRANSITION_USING_EGL" ffmpeg-gl-transition/vf_gltransition.c > ffmpeg/libavfilter/vf_gltransition.c
 apt-get install libglew libglew-dev
 apt-get install  libass
 apt-get install -y glfw glew-devel
 apt-get install -y lrzsz

./configure --prefix=/usr --extra-version=0ubuntu0.20.04.1 --extra-cflags=-I/usr/local/cuda-11.7/include --extra-ldflags=-L/usr/local/cuda-11.7/lib64 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-nonfree --enable-cuda --enable-cuda-nvcc --enable-cuvid --enable-gnutls --enable-libass --enable-libfontconfig --enable-libnpp --enable-nvdec --enable-nvenc --enable-opencl --enable-libx264 --enable-shared --enable-filter=gltransition --enable-gpl --enable-opengl --extra-libs='-lGLEW -lglfw -ldl'

```html


tip:注意--extra-version=0ubuntu0.20.04.1 --extra-cflags=-I/usr/local/cuda-11.7/include --extra-ldflags=-L/usr/local/cuda-11.7/lib64要修改对应自己的服务器。


报ffbuild/config.log显卡错误时,查看
https://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/
设置configure合适显卡计算力参数
4234 
4235 if enabled cuda_nvcc; then
4236     nvcc_default="nvcc"
4237     nvccflags_default="-gencode=arch=compute_86,code=sm_86 -O2"
4238 else
4239     nvcc_default="clang"
4240     nvccflags_default="--cuda-gpu-arch=sm_30 -O2"
4241     NVCC_C=""
4242 fi



make -j12
make install
验证安装是否成功
 # ffmpeg -v 0 -filters|grep gltran
 T.. gltransition      VV->V      OpenGL blend transitions
Xvfb :1 -screen 0 1280x1024x16 (安装apt-get install xvfb)

tip:默认glsl生成效果差,需要手动修改glsl。

env DISPLAY=:1 ffmpeg -t 1 -loop 1 -i 1.jpg -t 1 -loop 1 -i 2.jpg -filter_complex "gltransition=offset=0.5:duration=0.5:source=/root/ffmpeg-gl-transition/gl-transitions/transitions/crosswarp.glsl" -y out1.mp4
下载一些其他特效库
https://github.com/gl-transitions/gl-transitions/tree/master/transitions

env DISPLAY=:1 ffmpeg -t 2 -loop 1 -i 0.png -t 2 -loop 1 -i 59.png -filter_complex "gltransition=offset=1:duration=0.5:source=/opt/src/ffmpeg/ffmpeg-gl-transition/transitions/wind.glsl"  -c:v h264_nvenc -y zzz.mp4

**使用注意事项看这:效果不理想修改相应的xxx.glsl文件变量参数**
https://blog.csdn.net/Asa_Ho/article/details/116096500

-c:v h264_nvenc报错误可以修改自己cuda驱动相对应的两个so文件
libnvidia-encode.so.1 libnvcuvid.so.1是与自己驱动适配的两个so文件。
可参考:最好手动
cp libnvidia-encode.so.1 libnvcuvid.so.1 /lib64/ /usr/lib/x86_64-linux-gnu/

#!/usr/bin/env bash
# 获取驱动程序版本和CUDA版本

driver=$(nvidia-smi | grep "Driver Version" | awk '{print $6}')
cuda=$(nvidia-smi | grep "Driver Version" | awk '{print $9}')
echo "Driver Version: $driver"
echo "CUDA Version: $cuda"
# rm /etc/apt/sources.list.d/cuda.list注释防止误操作!根据实际需要。
# rm /etc/apt/sources.list.d/nvidia-ml.list
apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub
apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/7fa2af80.pub
echo "deb https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu2004/x86_64 /" |  tee -a /etc/apt/sources.list
apt-get update
# 提取版本号
driver_version=$(echo $driver | awk -F'.' '{print $1}')
package_name_encode="libnvidia-encode"
package_name_decode="libnvidia-decode"
package_name_compute="libnvidia-compute"
full_package_name_encode="$package_name_encode-$driver_version"
full_package_name_decode="$package_name_decode-$driver_version"
full_package_name_compute="$package_name_compute-$driver_version"
# 输出版本号
echo "Driver Version Extracted: $driver_version"
echo "Full Package Name for Encoding: $full_package_name_encode"
echo "Full Package Name for Decoding: $full_package_name_decode"

# 获取可用版本号
Version_encode=$(apt-cache madison $full_package_name_encode | grep $driver | awk '{print $3}')
Version_decode=$(apt-cache madison $full_package_name_decode | grep $driver | awk '{print $3}')

# 安装
apt-get install $full_package_name_encode=$Version_encode  $full_package_name_decode=$Version_encode $full_package_name_compute=$Version_encode


import os
import subprocess

# Define the directory containing the GLSL transition files
transitions_dir = "/opt/src/ffmpeg/ffmpeg-gl-transition/transitions/"
output_dir = "./outputs"  # Directory to save the generated videos
os.makedirs(output_dir, exist_ok=True)

# Define the input images and other ffmpeg parameters
input_1 = "0.png"
input_2 = "59.png"
display_env = "DISPLAY=:1"
duration = 2
transition_offset = 1
transition_duration = 0.5
codec = "h264_nvenc"

# Get all GLSL files in the transitions directory
glsl_files = [f for f in os.listdir(transitions_dir) if f.endswith('.glsl')]

# Loop through each GLSL file and run the ffmpeg command
for glsl_file in glsl_files:
    # Construct the full path to the GLSL file
    glsl_path = os.path.join(transitions_dir, glsl_file)

    # Define the output file name
    output_file = os.path.join(output_dir, f"{glsl_file.replace('.glsl', '.mp4')}")

    # Construct the ffmpeg command
    ffmpeg_command = [
        'ffmpeg', '-t', str(duration), '-loop', '1', '-i', input_1,
        '-t', str(duration), '-loop', '1', '-i', input_2,
        '-filter_complex', f"gltransition=offset={transition_offset}:duration={transition_duration}:source={glsl_path}",
        '-c:v', codec, '-y', output_file
    ]

    # Set the DISPLAY environment variable
    env = os.environ.copy()
    env["DISPLAY"] = ":1"

    # Print and run the ffmpeg command
    print(f"Running ffmpeg for {glsl_file}...")
    try:
        subprocess.run(ffmpeg_command, env=env, check=True)
        print(f"Successfully created {output_file}")
    except subprocess.CalledProcessError as e:
        print(f"Error processing {glsl_file}: {e}")

print("All transitions processed.")

你可能感兴趣的:(环境配置,ffmpeg,ubuntu,linux)