ffmpeg-graceful-degradation
它能做什么
这项技能描述了用 FFmpeg 进行视频编码的备用方案,从检查编码器到尝试其他选项。它适合处理大量视频、而编码错误不断中断任务的人。
适用场景
- ✓当 FFmpeg 编码因缺少编解码器或库错误而失败的时候。
- ✓当我需要处理一批视频,又不希望整批任务都失败的时候。
- ✓当我希望在完整运行开始前,先检查一段短的测试片段的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/ffmpeg-graceful-degradation/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的 FFmpeg 编码在一批片段上一直失败。请先测试一段短片,并找到一个能完成任务的方法。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 会探测可用的编码器,运行一次短测试,然后沿着备用方案列表依次尝试:从流复制到其他编解码器,最后是 moviepy,之后才处理整批视频。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
FFmpeg Graceful Degradation
When processing videos with ffmpeg, encoding failures are common due to codec availability, library version mismatches, or system configuration issues. This skill provides a systematic fallback strategy to ensure video processing completes successfully.
Overview
The pattern involves: (1) probing encoder availability upfront, (2) testing on a short clip before batch processing, (3) progressive fallback through copy mode, alternative codecs, and finally moviepy, (4) using moviepy as a reliable bundled alternative.
Step 1: Probe Encoder Availability
Before any encoding work, check what encoders are available:
ffmpeg -encoders | grep -E "libx264|libopenh264|mpeg4"
Expected output shows which encoders are present:
libx264- Preferred H.264 encoder (may be missing)libopenh264- Alternative H.264 (often has library issues)mpeg4- Universal fallback (always available)
Step 2: Test Encoding on Single Short Clip
Never start batch processing without validation. Extract and test a short segment:
# Extract 5-second test clip
ffmpeg -y -i input.mp4 -ss 0 -t 5 -c copy test_clip.mp4
# Attempt encode with preferred codec
ffmpeg -y -i test_clip.mp4 -c:v libx264 -preset fast test_output.mp4
Check the exit code and output for errors. Common failures:
libopenh264.so: wrong ELF classEncoder libx264 not found- Library version mismatches
Step 3: Progressive Fallback Strategy
If the preferred encoder fails, try these fallbacks in order:
Fallback A: Copy Mode (No Re-encoding)
ffmpeg -y -i input.mp4 -c:v copy -c:a copy output.mp4
Fast, lossless, but doesn't change codec/format.
Fallback B: MPEG4 Codec
ffmpeg -y -i input.mp4 -c:v mpeg4 -q:v 3 -c:a copy output.mp4
Universal compatibility, larger file sizes, always available.
Fallback C: Install MoviePy (Bundles Working FFmpeg)
pip install moviepy
Then use Python instead of raw ffmpeg:
from moviepy.editor import VideoFileClip, concatenate_videoclips
# Single clip processing
clip = VideoFileClip("input.mp4")
clip.write_videofile("output.mp4", codec="libx264")
# Concatenate multiple clips
clips = [VideoFileClip(f) for f in clip_files]
final = concatenate_videoclips(clips)
final.write_videofile("output.mp4", codec="libx264")
MoviePy bundles its own ffmpeg binary, avoiding system library issues.
Step 4: Implementation Pattern
Here's a complete graceful degradation workflow:
import subprocess
import os
def safe_video_encode(input_path, output_path, clips=None):
"""
Encode video with graceful degradation fallbacks.
Args:
input_path: Single input file path, or
clips: List of clip paths for concatenation
Returns:
True if successful, False otherwise
"""
# Step 1: Check encoder availability
result = subprocess.run(
["ffmpeg", "-encoders"],
capture_output=True, text=True
)
has_libx264 = "libx264" in result.stdout
has_mpeg4 = "mpeg4" in result.stdout
# Step 2: If concatenating, prepare clips with moviepy
if clips:
try:
from moviepy.editor import VideoFileClip, concatenate_videoclips
loaded_clips = [VideoFileClip(c) for c in clips]
final = concatenate_videoclips(loaded_clips)
final.write_videofile(output_path, codec="libx264", logger=None)
return True
except Exception as e:
print(f"MoviePy failed: {e}")
# Step 3: Try ffmpeg with progressive fallbacks
encoders_to_try = []
if has_libx264:
encoders_to_try.append("libx264")
encoders_to_try.append("mpeg4") # Always available
for codec in encoders_to_try:
cmd = [
"ffmpeg", "-y",
"-i", input_path,
"-c:v", codec,
"-c:a", "copy",
output_path
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
return True
# Step 4: Last resort - copy mode
cmd = ["ffmpeg", "-y", "-i", input_path, "-c", "copy", output_path]
result = subprocess.run(cmd, capture_output=True, text=True)
return result.returncode == 0
Decision Flow
Start
│
▼
Check encoders (ffmpeg -encoders)
│
├─ libx264 available? ──Yes──► Try libx264
│ │ │
│ No └─►
常见问题
什么是 moviepy?
它是一个用于编辑视频的 Python 库,指南将其作为内置的备选方案。
为什么先测试一段片段?
短测试可以在处理整批视频之前发现编解码器问题。
常见的错误有哪些?
指南列出了 libopenh264 中的 ELF 类错误,以及找不到编码器等错误。
详情
作者的原始描述
Graceful degradation workflow for ffmpeg encoding failures with progressive fallback strategies
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12887
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。