关于 ffmpeg-cicd-runners
ffmpeg-cicd-runners 是在 CI/CD 流水线中运行 FFmpeg 的实用指南,面向自动化处理视频的团队。内容涵盖 GitHub Actions、GitLab CI 和 Jenkins 的安装、容器、缓存、并行任务及运行器注意事项。
主要功能
- 01CI 系统中的 FFmpeg 配置示例
- 02基于 Docker 的运行器配置
- 03缓存 FFmpeg 和依赖项
- 04并行处理与矩阵任务模式
- 05Windows 运行器注意事项
适用场景
- 01在 CI 任务中转码视频
- 02在拉取请求中验证视频文件
- 03生成缩略图和音轨
- 04将处理结果保存为构建产物
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ josiahsiegel/ claude-plugin-marketplace --skill ffmpeg-cicd-runners安装后在 Claude Code 中输入/ffmpeg-cicd-runners确认已加载。
详细介绍
关键指南
Windows 文件路径要求
强制要求:在 Windows 上始终使用反斜杠表示文件路径
在 Windows 上使用 Edit 或 Write 工具时,必须在文件路径中使用反斜杠(\),而不是正斜杠(/)。
快速参考
| 平台 | 安装方法 | 示例 |
|---|---|---|
| GitHub Actions | apt-get 或 action | uses: FedericoCarboni/setup-ffmpeg@v3 |
| GitLab CI | Docker 镜像 | image: jrottenberg/ffmpeg:7.1-ubuntu2404 |
| Jenkins | Docker 容器 | docker { image 'jrottenberg/ffmpeg:7.1' } |
| 任务 | YAML 代码片段 |
|---|---|
| 缓存 FFmpeg | 使用 uses: actions/cache@v4,并设置 path: /usr/local/bin/ffmpeg |
| 上传构建产物 | uses: actions/upload-artifact@v4 |
| 矩阵构建 | strategy: { matrix: { format: [mp4, webm, mkv] } } |
何时使用此技能
适用于 CI/CD 视频处理流水线:
- GitHub Actions FFmpeg 工作流
- GitLab CI/CD 视频转码
- Jenkins 视频处理任务
- 缓存 FFmpeg 以加快构建速度
- Windows 运行器的变通方案
CI/CD 流水线中的 FFmpeg(2025)
全面介绍如何在 GitHub Actions、GitLab CI、Jenkins 及其他 CI/CD 系统中使用 FFmpeg。
概述
常见 CI/CD 使用场景
- 视频验证:检查上传的视频是否符合要求
- 转码:将视频转换为多种格式和分辨率
- 缩略图生成:创建预览图像
- 音频提取:处理音轨
- 质量检查:验证编码参数
- 测试:测试视频处理流水线
关键注意事项
- 构建时间:编译 FFmpeg 需要 20–40 分钟以上
- 运行器资源:CPU/内存限制会影响处理过程
- 缓存:缓存 FFmpeg 构建以加快运行速度
- 平台差异:Windows 运行器存在独特问题
- 依赖项:可能需要安装编解码器库
GitHub Actions
安装 FFmpeg(快速)
name: Video Processing
on: [push, pull_request]
jobs:
process:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install FFmpeg
run: |
sudo apt-get update
sudo apt-get install -y ffmpeg
- name: Check FFmpeg version
run: ffmpeg -version
- name: Process video
run: |
ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
安装指定版本的 FFmpeg
jobs:
process:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install FFmpeg 7.1
run: |
wget https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz
tar xf ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz
sudo cp ffmpeg-n7.1-latest-linux64-gpl-7.1/bin/* /usr/local/bin/
ffmpeg -version
使用 FedericoCarboni/setup-ffmpeg Action
jobs:
process:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup FFmpeg
uses: FedericoCarboni/setup-ffmpeg@v3
with:
ffmpeg-version: release
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Process video
run: ffmpeg -i input.mp4 output.webm
基于 Docker 的 FFmpeg
jobs:
process:
runs-on: ubuntu-latest
container:
image: jrottenberg/ffmpeg:7.1-ubuntu2404
steps:
- uses: actions/checkout@v4
- name: Process video
run: |
ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
缓存 FFmpeg 构建
jobs:
build-ffmpeg:
runs-on: ubuntu-latest
steps:
- name: Cache FFmpeg
id: cache-ffmpeg
uses: actions/cache@v4
with:
path: /usr/local/bin/ffmpeg
key: ffmpeg-7.1-linux-${{ runner.arch }}
- name: Build FFmpeg (if not cached)
if: steps.cache-ffmpeg.outputs.cache-hit != 'true'
run: |
wget https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz
tar xf ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz
sudo cp ffmpeg-n7.1-latest-linux64-gpl-7.1/bin/ffmpeg /usr/local/bin/
矩阵构建(多个平台)
jobs:
process:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Setup FFmpeg
uses: FedericoCarboni/setup-ffmpeg@v3
- name: Process video
run: ffmpeg -i input.mp4 -c:v libx264 output_${{ matrix.os }}.mp4
shell: bash
Windows 运行器(已知问题)
问题:Windows 运行器耗时明显更长(使用 vcpkg 构建 FFmpeg 最长需要 4 小时)
解决方案:
jobs:
windows-ffmpeg:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
# Option 1: Use pre-built binaries (recommended)
- name: Download FFmpeg
run: |
Invoke-WebRequest -Uri "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-win64-gpl-7.1.zip" -OutFile ffmpeg.zip
Expand-Archive ffmpeg.zip -DestinationPath .
$env:PATH = "$PWD\ffmpeg-n7.1-latest-win64-gpl-7.1\bin;$env:PATH"
ffmpeg -version
# Option 2: Use setup-ffmpeg action
- name: Setup FFmpeg
uses: FedericoCarboni/setup-ffmpeg@v3
- name: Process video
run: ffmpeg -i input.mp4 output.mp4
启用 GPU 的运行器
jobs:
gpu-process:
runs-on: [self-hosted, gpu, linux]
steps:
- uses: actions/checkout@v4
- name: Process with NVIDIA GPU
run: |
ffmpeg -hwaccel cuda -hwaccel_output_format cuda \
-i input.mp4 \
-c:v h264_nvenc \
-preset p4 \
output.mp4
上传构建产物
jobs:
process:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install FFmpeg
run: sudo apt-get update && sudo apt-get install -y ffmpeg
- name: Generate outputs
run: |
mkdir -p outputs
ffmpeg -i input.mp4 -vf scale=1920:1080 outputs/1080p.mp4
ffmpeg -i input.mp4 -vf scale=1280:720 outputs/720p.mp4
ffmpeg -i input.mp4 -ss 00:00:05 -vframes 1 outputs/thumbnail.jpg
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: video-outputs
path: outputs/
retention-days: 7
视频验证工作流
name: Validate Videos
on:
pull_request:
paths:
- '**.mp4'
- '**.webm'
- '**.mov'
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install FFmpeg
run: sudo apt-get update && sudo apt-get install -y ffmpeg
- name: Validate videos
run: |
for video in $(find . -name "*.mp4" -o -name "*.webm" -o -name "*.mov"); do
echo "Validating: $video"
# Check if valid
if ! ffprobe -v error "$video"; then
echo "Invalid video: $video"
exit 1
fi
# Get info
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$video")
resolution=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 "$video")
echo "Duration: ${duration}s"
echo "Resolution: $resolution"
# Validate constraints
if (( $(echo "$duration > 300" | bc -l) )); then
echo "Video too long: $video ($duration seconds)"
exit 1
fi
done
GitLab CI
基础流水线
# .gitlab-ci.yml
stages:
- process
video-process:
stage: process
image: jrottenberg/ffmpeg:7.1-ubuntu2404
script:
- ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4
artifacts:
paths:
- output.mp4
expire_in: 1 week
多格式转码
transcode:
stage: process
image: jrottenberg/ffmpeg:7.1-ubuntu2404
script:
- mkdir -p outputs
- ffmpeg -i input.mp4 -c:v libx264 -crf 23 outputs/h264.mp4
- ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 outputs/vp9.webm
- ffmpeg -i input.mp4 -vf scale=1280:720 outputs/720p.mp4
artifacts:
paths:
- outputs/
并行处理
.process-template: &process-template
stage: process
image: jrottenberg/ffmpeg:7.1-ubuntu2404
process-1080p:
<<: *process-template
script:
- ffmpeg -i input.mp4 -vf scale=1920:1080 output_1080p.mp4
artifacts:
paths:
- output_1080p.mp4
process-720p:
<<: *process-template
script:
- ffmpeg -i input.mp4 -vf scale=1280:720 output_720p.mp4
artifacts:
paths:
- output_720p.mp4
process-480p:
<<: *process-template
script:
- ffmpeg -i input.mp4 -vf scale=854:480 output_480p.mp4
artifacts:
paths:
- output_480p.mp4
GPU 运行器(GitLab)
gpu-transcode:
stage: process
tags:
- gpu
- linux
image: jrottenberg/ffmpeg:7.1-nvidia2404
script:
- ffmpeg -hwaccel cuda -i input.mp4 -c:v h264_nvenc output.mp4
Jenkins
声明式流水线
pipeline {
agent {
docker {
image 'jrottenberg/ffmpeg:7.1-ubuntu2404'
args '-v /data/videos:/videos'
}
}
stages {
stage('Process') {
steps {
sh '''
ffmpeg -i /videos/input.mp4 \
-c:v libx264 -crf 23 \
-c:a aac -b:a 128k \
/videos/output.mp4
'''
}
}
}
post {
success {
archiveArtifacts artifacts: '/videos/output.mp4'
}
}
}
并行转码
pipeline {
agent any
stages {
stage('Transcode') {
parallel {
stage('1080p') {
agent {
docker { image 'jrottenberg/ffmpeg:7.1-ubuntu2404' }
}
steps {
sh 'ffmpeg -i input.mp4 -vf scale=1920:1080 output_1080p.mp4'
}
}
stage('720p') {
agent {
docker { image 'jrottenberg/ffmpeg:7.1-ubuntu2404' }
}
steps {
sh 'ffmpeg -i input.mp4 -vf scale=1280:720 output_720p.mp4'
}
}
}
}
}
}
BtbN/FFmpeg-Builds
概述
BtbN/FFmpeg-Builds 是最受欢迎的自动化 FFmpeg 构建系统,可为多个平台提供预构建的静态二进制文件。
构建矩阵
| 目标 | 变体 | 功能 |
|---|---|---|
| win64 | gpl, lgpl, gpl-shared, lgpl-shared | 完整的 Windows 64 位版本 |
| winarm64 | gpl, lgpl | Windows ARM64 |
| linux64 | gpl, lgpl, gpl-shared, lgpl-shared | 完整的 Linux 64 位版本 |
| linuxarm64 | gpl, lgpl | Linux ARM64 |
在 CI 中下载
- name: Download FFmpeg
run: |
# Latest release
wget https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz
# Specific version
wget https://github.com/BtbN/FFmpeg-Builds/releases/download/autobuild-2025-01-15-12-50/ffmpeg-n7.1-latest-linux64-gpl-7.1.tar.xz
性能优化
并行处理
# Process multiple files in parallel
jobs:
process:
runs-on: ubuntu-latest
strategy:
matrix:
video: [video1.mp4, video2.mp4, video3.mp4]
max-parallel: 3
steps:
- name: Process ${{ matrix.video }}
run: ffmpeg -i ${{ matrix.video }} -c:v libx264 output_${{ matrix.video }}
资源优化编码
- name: Optimized encoding
run: |
# Use appropriate threads for runner
THREADS=$(nproc)
ffmpeg -i input.mp4 \
-threads $THREADS \
-c:v libx264 \
-preset fast \
-crf 23 \
output.mp4
缓存依赖项
- name: Cache FFmpeg and libraries
uses: actions/cache@v4
with:
path: |
~/.local/bin/ffmpeg
~/.local/lib/
key: ffmpeg-${{ runner.os }}-${{ hashFiles('ffmpeg-version.txt') }}
故障排除
常见问题
“ffmpeg: command not found”
# Ensure FFmpeg is in PATH
- name: Add FFmpeg to PATH
run: echo "/path/to/ffmpeg/bin" >> $GITHUB_PATH
“Cannot allocate memory”
# Reduce memory usage
- name: Process with low memory
run: |
ffmpeg -i input.mp4 \
-threads 2 \
-preset ultrafast \
-vf scale=1280:720 \
output.mp4
Windows 路径问题
# Use forward slashes on Windows
- name: Process on Windows
run: ffmpeg -i "./input.mp4" "./output.mp4"
shell: bash
调试
- name: Debug FFmpeg
run: |
# Check FFmpeg capabilities
ffmpeg -version
ffmpeg -encoders
ffmpeg -decoders
ffmpeg -formats
# Verbose processing
ffmpeg -v verbose -i input.mp4 -c:v libx264 output.mp4 2>&1 | tee ffmpeg.log
- name: Upload logs
if: failure()
uses: actions/upload-artifact@v4
with:
name: ffmpeg-logs
path: ffmpeg.log
最佳实践
- 使用预编译二进制文件 - 避免在 CI 中编译 FFmpeg(速度慢)
- 缓存 FFmpeg 安装 - 缩短设置时间
- 使用 Docker 容器 - 确保 FFmpeg 环境一致
- 根据运行器资源进行优化 - 根据可用 CPU 调整线程数/预设
- 验证输入 - 处理前检查视频文件
- 上传构建产物 - 保存输出以供审查
- 使用矩阵构建 - 并行处理多种格式的转码
- 监控构建时间 - Windows 运行器速度较慢
- 固定版本 - 避免使用
latest,以确保可复现 - 使用
-preset fast/ultrafast- 加快 CI 中的编码速度
本指南介绍 CI/CD 中 FFmpeg 的使用模式。若要在 CI 中使用硬件加速,可考虑使用自托管 GPU 运行器。
评论
支持 Markdown