Check available FFmpeg encoders before writing encoding scripts to avoid library version mismatches
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-ffmpeg-encoder-check-4855c0-15e2bdcd4dd7 ,按照其中的说明把「ffmpeg-encoder-check-4855c0」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
Before writing any FFmpeg encoding script, always probe the system for available encoders. This prevents failures from missing or incompatible codec libraries (especially libopenh264 which frequently has version mismatches).
Always run this command before deciding on encoding parameters:
ffmpeg -encoders | grep h264
This shows which H.264 encoders are available on the system.
Priority order for H.264 encoding:
-c:v copy - If source and target resolution/format match, copy the stream without re-encoding (fastest, no quality loss)
-c:v libx264 - If available, this is the most reliable and widely-compatible H.264 encoder
-c:v h264 - Hardware acceleration if available (varies by system)
Avoid libopenh264 - This encoder frequently has library version mismatches causing runtime failures
After choosing an encoder, verify it works with a short test:
ffmpeg -t 5 -i input.mp4 -c:v libx264 -preset fast -crf 23 -c:a copy test_output.mp4
#!/bin/bash
# Check available encoders
ENCODERS=$(ffmpeg -encoders 2>/dev/null | grep h264)
if echo "$ENCODERS" | grep -q "libx264"; then
VIDEO_CODEC="libx264"
echo "Using libx264 encoder"
elif echo "$ENCODERS" | grep -q "h264"; then
VIDEO_CODEC="h264"
echo "Using h264 encoder"
else
VIDEO_CODEC="copy"
echo "No H.264 encoder available, using stream copy"
fi
# Use $VIDEO_CODEC in your ffmpeg command
ffmpeg -i input.mp4 -c:v $VIDEO_CODEC output.mp4
Use -c:v copy when: