云上极速转码:阿里云ECS+T4 GPU打造高性能FFmpeg视频处理引擎(部署指南)
为什么需要GPU加速?
在视频处理领域,时间就是金钱。传统的CPU转码在处理高清、4K视频时往往需要数小时,而借助NVIDIA T4
GPU的硬件加速,同样的工作可以在几分钟内完成!本文将带您一步步在阿里云ECS上打造一个极速视频处理引擎
一、部署环境
类别 | 说明 |
---|---|
服务器类别 | 阿里云ECS |
系统版本 | CentOS7.9 |
GPU | NVIDIA T4 |
NVIDIA驱动 | 550.127.08 |
CUDA版本 | 12.4.1 |
cuDNN版本 | 9.2.0.82 |
FFmpeg版本 | 5.1.3 |
ffnvcodec版本 | n12.0.16.0 |
GPU编码器 | h264_nvenc, hevc_nvenc |
二、服务器开通
记得勾选安装GPU驱动,开机会自动安装
开机后远程进入服务器可以看到在下载驱动
再次远程服务器,重启后
始连接到 root(root)@172.25.9.18 0.1
Last login: Tue Oct 21 15:11:50 2025 from 172.31.15.114Welcome to Alibaba Cloud Elastic Compute Service !% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 20 100 20 0 0 4042 0 --:--:-- --:--:-- --:--:-- 5000
CHECKING AUTO INSTALL, DRIVER_VERSION=550.127.08 CUDA_VERSION=12.4.1 CUDNN_VERSION=9.2.0.82 , INSTALL RDMA=FALSE, INSTALL eRDMA=FALSE, PLEASE WAIT ......
The script automatically downloads and installs a NVIDIA GPU driver and CUDA, CUDNN library. if you choose install RDMA or ERDMA, RDMA or ERDMA software will install.
if you choose install perseus, perseus environment will install as well.
1. The installation takes 15 to 20 minutes, depending on the intranet bandwidth and the quantity of vCPU cores of the instance. Please do not operate the GPU or install any GPU-related software until the GPU driver is installed successfully.
2. After the GPU is installed successfully, the instance will restarts automatically.ALL INSTALL OK !!nvidia-smi
Tue Oct 21 15:25:27 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.127.08 Driver Version: 550.127.08 CUDA Version: 12.4 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 Tesla T4 On | 00000000:00:07.0 Off | 0 |
| N/A 32C P8 9W / 70W | 1MiB / 15360MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------++-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Thu_Mar_28_02:18:24_PDT_2024
Cuda compilation tools, release 12.4, V12.4.131
Build cuda_12.4.r12.4/compiler.34097967_0
三、系统准备
# 更新系统
sudo yum update -y
sudo yum install -y epel-release
sudo yum groupinstall -y "Development Tools"# 安装基础依赖
sudo yum install -y cmake git wget nasm
sudo yum install -y openssl-devel libxcb-devel libX11-devel
四、依赖安装
1、安装ffnvcodec(NVIDIA编解码器头文件)
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
Cloning into 'nv-codec-headers'...
remote: Enumerating objects: 1081, done.
remote: Counting objects: 100% (1081/1081), done.
remote: Compressing objects: 100% (918/918), done.
remote: Total 1081 (delta 583), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (1081/1081), 195.15 KiB | 0 bytes/s, done.
Resolving deltas: 100% (583/583), done.cd nv-codec-headersgit checkout n12.0.16.0
Note: checking out 'n12.0.16.0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:git checkout -b new_branch_nameHEAD is now at c5e4af7... Add cuArrayCreatemake
sed 's#@@PREFIX@@#/usr/local#' ffnvcodec.pc.in > ffnvcodec.pcsudo make install
sed 's#@@PREFIX@@#/usr/local#' ffnvcodec.pc.in > ffnvcodec.pc
install -m 0755 -d '/usr/local/include/ffnvcodec'
install -m 0644 include/ffnvcodec/*.h '/usr/local/include/ffnvcodec'
install -m 0755 -d '/usr/local/lib/pkgconfig'
install -m 0644 ffnvcodec.pc '/usr/local/lib/pkgconfig'
2、安装编码器依赖(x264/x265)
cd ~
mkdir -p ~/ffmpeg_build
cd ~/ffmpeg_build# 安装x264
git clone https://code.videolan.org/videolan/x264.gitcd x264./configure --prefix="$HOME/ffmpeg_build" --enable-shared --enable-pic
Found no assembler
Minimum version is nasm-2.13这里会出现报错Found no assembler,NASM汇编器缺失
cd ~
wget https://www.nasm.us/pub/nasm/releasebuilds/2.16.01/nasm-2.16.01.tar.gz
tar xzf nasm-2.16.01.tar.gz
cd nasm-2.16.01
./autogen.sh && ./configure
make -j4 && sudo make installcd ~/ffmpeg_build/x264
./configure --prefix="$HOME/ffmpeg_build" --enable-shared --enable-pic
make -j4 && make install
cd ..# 安装x265
cd ~
git clone https://bitbucket.org/multicoreware/x265_git.git
cd x265_git/build/linux
cmake -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -G "Unix Makefiles" ../../source
make -j4 && make install
cd ~/ffmpeg_build
3、编译安装FFmpeg
# 下载FFmpeg 5.1.3(稳定版本)
wget https://ffmpeg.org/releases/ffmpeg-5.1.3.tar.xz
tar xf ffmpeg-5.1.3.tar.xz
cd ffmpeg-5.1.3# 配置编译选项
./configure \--prefix="$HOME/ffmpeg_build" \--extra-cflags="-I/usr/local/cuda/include -I/usr/local/include/ffnvcodec" \--extra-ldflags="-L/root/ffmpeg_build/lib -L/usr/local/cuda/lib64 -L/usr/local/lib" \--enable-nonfree \--enable-gpl \--enable-version3 \--enable-shared \--disable-static \--enable-cuda-nvcc \--enable-libnpp \--enable-libx264 \--enable-libx265 \--enable-cuvid \--enable-nvenc \--enable-ffnvcodec \--extra-libs=-lpthread \--extra-libs=-lm
ERROR: x264 not found using pkg-config
出现报错pkg-config找不到x264库pkg-config --exists x264echo $? #返回0才是正常
1export PKG_CONFIG_PATH=$HOME/ffmpeg_build/lib/pkgconfig:$PKG_CONFIG_PATH#检查x264
pkg-config --exists x264
echo $?
0#检查x265
pkg-config --exists x265
echo $?
0./configure \--prefix="$HOME/ffmpeg_build" \--extra-cflags="-I/usr/local/cuda/include -I/usr/local/include/ffnvcodec" \--extra-ldflags="-L/root/ffmpeg_build/lib -L/usr/local/cuda/lib64 -L/usr/local/lib" \--enable-nonfree \--enable-gpl \--enable-version3 \--enable-shared \--disable-static \--enable-cuda-nvcc \--enable-libnpp \--enable-libx264 \--enable-libx265 \--enable-cuvid \--enable-nvenc \--enable-ffnvcodec \--extra-libs=-lpthread \--extra-libs=-lmERROR: cuvid requested, but not all dependencies are satisfied: ffnvcodec
出现报错没有设置正确的pkg-config路径pkg-config --exists ffnvcodec
echo $? #返回0才是正常
1export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$HOME/ffmpeg_build/lib/pkgconfig:$PKG_CONFIG_PATH
pkg-config --exists ffnvcodec
echo $?
1./configure \--prefix="$HOME/ffmpeg_build" \--extra-cflags="-I/usr/local/cuda/include -I/usr/local/include/ffnvcodec" \--extra-ldflags="-L/root/ffmpeg_build/lib -L/usr/local/cuda/lib64 -L/usr/local/lib" \--enable-nonfree \--enable-gpl \--enable-version3 \--enable-shared \--disable-static \--enable-cuda-nvcc \--enable-libnpp \--enable-libx264 \--enable-libx265 \--enable-cuvid \--enable-nvenc \--enable-ffnvcodec \--extra-libs=-lpthread \--extra-libs=-lm
看到License: nonfree and unredistributable就是正常的# 编译安装
make -j4 && make install# 设置环境变量
echo 'export PATH="$HOME/ffmpeg_build/bin:$PATH"' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH="$HOME/ffmpeg_build/lib:/usr/local/cuda/lib64:$LD_LIBRARY_PATH"' >> ~/.bashrc
source ~/.bashrc# 查看版本
ffmpeg -version
ffmpeg version 5.1.3 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-44)
configuration: --prefix=/root/ffmpeg_build --extra-cflags='-I/usr/local/cuda/include -I/usr/local/include/ffnvcodec' --extra-ldflags='-L/root/ffmpeg_build/lib -L/usr/local/cuda/lib64 -L/usr/local/lib' --enable-nonfree --enable-gpl --enable-version3 --enable-shared --disable-static --enable-cuda-nvcc --enable-libnpp --enable-libx264 --enable-libx265 --enable-cuvid --enable-nvenc --enable-ffnvcodec --extra-libs=-lpthread --extra-libs=-lm
libavutil 57. 28.100 / 57. 28.100
libavcodec 59. 37.100 / 59. 37.100
libavformat 59. 27.100 / 59. 27.100
libavdevice 59. 7.100 / 59. 7.100
libavfilter 8. 44.100 / 8. 44.100
libswscale 6. 7.100 / 6. 7.100
libswresample 4. 7.100 / 4. 7.100
libpostproc 56. 6.100 / 56. 6.100
五、验证结果
1、GPU加速支持验证
ffmpeg -encoders | grep nvenc
# 输出:
# V....D h264_nvenc NVIDIA NVENC H.264 encoder (codec h264)
# V....D hevc_nvenc NVIDIA NVENC hevc encoder (codec hevc)
2、实际性能测试
# 创建测试视频
ffmpeg -f lavfi -i testsrc=duration=10:size=1280x720:rate=30 -c:v libx264 test_input.mp4# 另打开一个终端运行
watch -n 1 nvidia-smi# 当前终端GPU加速转码测试
ffmpeg -i test_input.mp4 -c:v h264_nvenc -b:v 2M test_gpu_output.mp4# 另一个终端可以看到GPU内存使用、GPU利用率、ffmpeg进程正常显示在GPU进程列表中
Every 1.0s: nvidia-smi Tue Oct 21 16:10:35 2025Tue Oct 21 16:10:35 2025
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.127.08 Driver Version: 550.127.08 CUDA Version: 12.4 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 Tesla T4 On | 00000000:00:07.0 Off | 0 |
| N/A 32C P0 31W / 70W | 217MiB / 15360MiB | 12% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------++-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| 0 N/A N/A 27026 C ffmpeg 213MiB |
+-----------------------------------------------------------------------------------------+
六、附:自动化部署脚本
#!/bin/bash# 阿里云ECS + NVIDIA T4 GPU FFmpeg自动化部署脚本
# 作者:基于实际部署经验总结
# 版本:1.0
# 日期:2024年set -e # 遇到错误立即退出# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color# 日志函数
log_info() {echo -e "${BLUE}[INFO]${NC} $1"
}log_success() {echo -e "${GREEN}[SUCCESS]${NC} $1"
}log_warning() {echo -e "${YELLOW}[WARNING]${NC} $1"
}log_error() {echo -e "${RED}[ERROR]${NC} $1"
}# 检查命令执行结果
check_result() {if [ $? -ne 0 ]; thenlog_error "$1 failed!"exit 1elselog_success "$1 completed"fi
}# 检查系统环境
check_environment() {log_info "检查系统环境..."# 检查操作系统if [ ! -f /etc/redhat-release ]; thenlog_error "This script is designed for CentOS/RHEL systems only"exit 1fi# 检查GPUif ! command -v nvidia-smi &> /dev/null; thenlog_error "NVIDIA driver not found. Please install NVIDIA driver first."exit 1fi# 检查CUDAif [ ! -d "/usr/local/cuda" ]; thenlog_error "CUDA not found. Please install CUDA first."exit 1filog_success "Environment check passed"
}# 安装系统依赖
install_dependencies() {log_info "安装系统依赖..."yum update -ycheck_result "System update"yum install -y epel-releasecheck_result "EPEL repository installation"yum groupinstall -y "Development Tools"check_result "Development tools installation"yum install -y cmake git wget openssl-devel libxcb-devel libX11-devel \SDL2-devel libass-devel libvorbis-devel freetype-devel \fontconfig-develcheck_result "Basic dependencies installation"
}# 安装NASM汇编器
install_nasm() {log_info "安装NASM汇编器..."cd /tmpwget https://www.nasm.us/pub/nasm/releasebuilds/2.16.01/nasm-2.16.01.tar.gzcheck_result "NASM download"tar xzf nasm-2.16.01.tar.gzcd nasm-2.16.01./autogen.sh./configuremake -j4make installcheck_result "NASM compilation and installation"# 验证安装nasm --versioncheck_result "NASM version check"
}# 安装ffnvcodec
install_ffnvcodec() {log_info "安装NVIDIA编解码器头文件(ffnvcodec)..."cd /tmpif [ -d "nv-codec-headers" ]; thenrm -rf nv-codec-headersfigit clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.gitcheck_result "ffnvcodec clone"cd nv-codec-headersgit checkout n12.0.16.0check_result "ffnvcodec version checkout"makemake installcheck_result "ffnvcodec installation"# 验证安装if [ -f "/usr/local/include/ffnvcodec/nvEncodeAPI.h" ]; thenlog_success "ffnvcodec installed successfully"elselog_error "ffnvcodec installation verification failed"exit 1fi
}# 编译安装x264
install_x264() {log_info "编译安装x264编码器..."cd /tmpif [ -d "x264" ]; thenrm -rf x264figit clone https://code.videolan.org/videolan/x264.gitcheck_result "x264 clone"cd x264./configure --prefix="$HOME/ffmpeg_build" --enable-shared --enable-piccheck_result "x264 configuration"make -j4make installcheck_result "x264 compilation and installation"# 创建pkg-config文件mkdir -p $HOME/ffmpeg_build/lib/pkgconfigcat > $HOME/ffmpeg_build/lib/pkgconfig/x264.pc << 'EOF'
prefix=/root/ffmpeg_build
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/includeName: x264
Description: H.264 (MPEG4 AVC) encoder library
Version: 0.164.x
Libs: -L${libdir} -lx264
Libs.private: -lpthread -lm
Cflags: -I${includedir}
EOFlog_success "x264 installed successfully"
}# 编译安装x265
install_x265() {log_info "编译安装x265编码器..."cd /tmpif [ -d "x265_git" ]; thenrm -rf x265_gitfigit clone https://bitbucket.org/multicoreware/x265_git.gitcheck_result "x265 clone"cd x265_git/build/linuxcmake -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -G "Unix Makefiles" ../../sourcecheck_result "x265 configuration"make -j4make installcheck_result "x265 compilation and installation"# 创建pkg-config文件cat > $HOME/ffmpeg_build/lib/pkgconfig/x265.pc << 'EOF'
prefix=/root/ffmpeg_build
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/includeName: x265
Description: H.265/HEVC video encoder
Version: 3.5
Libs: -L${libdir} -lx265
Libs.private: -lstdc++ -lm -lrt -ldl
Cflags: -I${includedir}
EOFlog_success "x265 installed successfully"
}# 编译安装FFmpeg
install_ffmpeg() {log_info "编译安装FFmpeg with GPU加速..."cd /tmpwget https://ffmpeg.org/releases/ffmpeg-5.1.3.tar.xzcheck_result "FFmpeg download"tar xf ffmpeg-5.1.3.tar.xzcd ffmpeg-5.1.3# 设置环境变量export PKG_CONFIG_PATH=$HOME/ffmpeg_build/lib/pkgconfig:/usr/local/lib/pkgconfig:$PKG_CONFIG_PATHexport LD_LIBRARY_PATH=$HOME/ffmpeg_build/lib:/usr/local/cuda/lib64:$LD_LIBRARY_PATH# 配置FFmpeg./configure \--prefix="$HOME/ffmpeg_build" \--extra-cflags="-I/usr/local/cuda/include -I/usr/local/include/ffnvcodec" \--extra-ldflags="-L$HOME/ffmpeg_build/lib -L/usr/local/cuda/lib64" \--enable-nonfree \--enable-gpl \--enable-version3 \--enable-shared \--disable-static \--enable-cuda-nvcc \--enable-libnpp \--enable-libx264 \--enable-libx265 \--enable-cuvid \--enable-nvenc \--enable-ffnvcodec \--extra-libs=-lpthread \--extra-libs=-lmcheck_result "FFmpeg configuration"make -j4make installcheck_result "FFmpeg compilation and installation"
}# 配置环境变量
setup_environment() {log_info "配置环境变量..."# 添加到bashrccat >> ~/.bashrc << 'EOF'# FFmpeg with GPU Support
export PATH="$HOME/ffmpeg_build/bin:$PATH"
export LD_LIBRARY_PATH="$HOME/ffmpeg_build/lib:/usr/local/cuda/lib64:$LD_LIBRARY_PATH"
export PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig:$PKG_CONFIG_PATH"
EOF# 立即生效source ~/.bashrclog_success "Environment variables configured"
}# 验证安装
verify_installation() {log_info "验证FFmpeg安装..."# 检查FFmpeg版本ffmpeg -versioncheck_result "FFmpeg version check"# 检查GPU编码器支持log_info "检查GPU编码器支持..."ffmpeg -encoders | grep nvencif [ $? -eq 0 ]; thenlog_success "GPU encoders are available"elselog_warning "GPU encoders not found, but FFmpeg is installed"fi# 创建测试视频log_info "创建测试视频..."ffmpeg -f lavfi -i testsrc=duration=5:size=1280x720:rate=30 -c:v libx264 test_input.mp4 -ycheck_result "Test video creation"# 测试GPU编码log_info "测试GPU编码..."ffmpeg -i test_input.mp4 -c:v h264_nvenc -b:v 2M test_gpu_output.mp4 -yif [ $? -eq 0 ]; thenlog_success "GPU encoding test passed"elselog_warning "GPU encoding test failed, but FFmpeg is installed"fi# 清理测试文件rm -f test_input.mp4 test_gpu_output.mp4
}# 显示安装总结
show_summary() {echo ""log_success "🎉 FFmpeg with GPU加速安装完成!"echo ""echo "📋 安装摘要:"echo " ✅ FFmpeg版本: 5.1.3"echo " ✅ GPU支持: NVIDIA T4 (h264_nvenc, hevc_nvenc)"echo " ✅ 安装目录: $HOME/ffmpeg_build"echo " ✅ 环境变量: 已配置"echo ""echo "🚀 使用方法:"echo " 基本GPU转码: ffmpeg -i input.mp4 -c:v h264_nvenc output.mp4"echo " 高质量转码: ffmpeg -i input.mp4 -c:v h264_nvenc -preset slow -crf 18 output.mp4"echo " 监控GPU: watch -n 1 nvidia-smi"echo ""echo "⚠️ 注意事项:"echo " - 重新登录或执行 'source ~/.bashrc' 使环境变量生效"echo " - 如遇硬件解码警告,不影响GPU编码功能"echo ""
}# 主函数
main() {echo "=========================================="echo " 阿里云ECS + T4 GPU FFmpeg自动化部署脚本"echo "=========================================="echo ""log_info "开始自动化部署过程..."# 执行各个步骤check_environmentinstall_dependenciesinstall_nasminstall_ffnvcodecinstall_x264install_x265install_ffmpegsetup_environmentverify_installationshow_summary
}# 脚本入口
if [ "$(id -u)" -ne 0 ]; thenlog_warning "建议使用root权限运行此脚本以获得最佳效果"read -p "是否继续? (y/n) " -n 1 -rechoif [[ ! $REPLY =~ ^[Yy]$ ]]; thenexit 1fi
fimain