Communitygithub.com

FFmpeg 必备实用技巧

核心 FFmpeg 和 ffprobe 使用技巧,涵盖裁剪、拼接、转换、缩放、压缩、变速、淡入淡出、音频提取以及 GIF 转换。适用于任何直接的媒体处理任务,或处理无法播放、分辨率异常、音画不同步的视频。

FFmpeg 必备实用技巧 是什么?

该技能提供了一套完整的 FFmpeg 和 ffprobe 实用命令集,覆盖了开发者日常最常用的视频/音频处理场景。它详细说明了如何安全地探测媒体信息(使用 ffprobe),并给出了裁剪、拼接、缩放、压缩、变速、添加淡入淡出效果、音视频分离与替换、GIF 生成以及图片序列处理等核心操作的具体命令和参数说明。技能还特别强调安全习惯(如避免覆盖源文件)和常见故障排除方案(如分辨率不可被 2 整除、编码格式不兼容、音画不同步等)。适用于在 Claude Code、Cursor 或 Codex 等 AI 编程助手或终端环境中直接调用 FFmpeg 完成批量处理或自动化任务的场景。所有命令都保留了原始的 bash 语法,并针对 Windows PowerShell 用户做了简要适配说明,确保跨平台可用。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/iamaanahmad/agents-video-editing-skills/tree/HEAD/skills/ffmpeg-essentials

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

FFmpeg 必备实用技巧 是做什么的?

始终先探查,绝不覆盖输入文件,除非另有指示,否则裁剪时重新编码。

探查

# 人类可读摘要
ffprobe -v error -show_entries stream=codec_type,codec_name,width,height,r_frame_rate,sample_rate -show_entries format=duration,size -of default=nw=1 input.mp4

# 机器可读(JSON 格式)
ffprobe -v quiet -print_format json -show_format -show_streams input.mp4

裁剪

# 从 30 秒开始,保留 15 秒。重新编码:精确且安全。
ffmpeg -i input.mp4 -ss 00:00:30 -t 15 -c:v libx264 -crf 20 -c:a aac out/clip.mp4

# 快速,不重新编码。仅定位在关键帧上,因此起始位置可能偏差数秒。
ffmpeg -ss 00:00:30 -i input.mp4 -t 15 -c copy out/clip.mp4

-ss 放在 -i 之前可以快速跳转。放在 -i 之后则精确但较慢。对于重新编码的情况,在现代 FFmpeg 中两种方式均可。

拼接

# 相同编码器、分辨率、帧率:即时、无损
printf "file 'a.mp4'\nfile 'b.mp4'\n" > list.txt
ffmpeg -f concat -safe 0 -i list.txt -c copy out/joined.mp4

# 不同来源:在拼接时做归一化处理
ffmpeg -i a.mp4 -i b.mp4 -filter_complex \
 "[0:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,fps=30,setsar=1[v0];\
  [1:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,fps=30,setsar=1[v1];\
  [0:a][1:a]concat=n=2:v=1:a=1[v][a]" \
 -map "[v]" -map "[a]" -c:v libx264 -crf 20 -c:a aac out/joined.mp4

PowerShell:使用 Set-Content 写入 list.txt,并使用反引号行延续符或写成一整行(不要用 \)。

调整大小与重新构图

# 适配到 1920x1080 框内,保留黑边
-vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2"
# 填满 1920x1080,裁剪溢出部分
-vf "scale=1920:1080:force_original_aspect_ratio=increase,crop=1920:1080"
# 宽度设为 1280,高度自动取偶数
-vf "scale=1280:-2"

压缩

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k -movflags +faststart out/small.mp4
用途CRF预设策略
母版 / 存档16-18slow
正式发布20-22medium
网页 / 预览23-26fast
快速草稿28+veryfast

CRF 值越低,质量越高,文件越大。若需目标文件大小,请使用二遍编码 -b:v 代替 CRF。

变速

# 2 倍速,视频和音频同时加速
ffmpeg -i in.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" out/fast.mp4
# 0.5 倍慢动作
ffmpeg -i in.mp4 -filter_complex "[0:v]setpts=2.0*PTS[v];[0:a]atempo=0.5[a]" -map "[v]" -map "[a]" out/slow.mp4
# 仅视频加速
ffmpeg -i in.mp4 -an -filter:v "setpts=0.25*PTS" out/timelapse.mp4

setpts 倍率 = 1 / 速度。atempo 每个滤镜仅支持 0.5 到 2.0,因此要获得更高倍率需要链式使用:例如 atempo=2.0,atempo=2.0 实现 4 倍速。

淡入淡出

# 10 秒片段:开始处淡入 1 秒,结尾处淡出 1 秒
ffmpeg -i in.mp4 -vf "fade=t=in:st=0:d=1,fade=t=out:st=9:d=1" -af "afade=t=in:st=0:d=1,afade=t=out:st=9:d=1" out/faded.mp4

音频

ffmpeg -i in.mp4 -vn -c:a libmp3lame -q:a 2 out/audio.mp3      # 提取为 mp3
ffmpeg -i in.mp4 -vn -c:a copy out/audio.m4a                   # 提取且不重新编码(AAC 源)
ffmpeg -i video.mp4 -i voice.wav -map 0:v -map 1:a -c:v copy -c:a aac -shortest out/dubbed.mp4   # 替换音频
ffmpeg -i in.mp4 -an -c:v copy out/silent.mp4                  # 移除音频

GIF 制作

ffmpeg -i in.mp4 -t 5 -vf "fps=15,scale=480:-1:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse" out/preview.gif

图片

ffmpeg -ss 5 -i in.mp4 -frames:v 1 -q:v 2 out/frame.jpg          # 单帧截图
ffmpeg -framerate 30 -i frames/%04d.png -c:v libx264 -pix_fmt yuv420p out/from-frames.mp4
ffmpeg -loop 1 -i still.png -t 5 -c:v libx264 -pix_fmt yuv420p -r 30 out/still.mp4

故障排除

症状解决方法
height not divisible by 2(高度不能被 2 整除)添加 scale=trunc(iw/2)*2:trunc(ih/2)*2
VLC 能播放,但浏览器或手机不能播放-c:v libx264 -pix_fmt yuv420p -movflags +faststart
变速后音画不同步在 filter_complex 中同时使用 setpts 和 atempo
变帧率手机视频的音画不同步添加 -vsync cfr -r 30(或 -fps_mode cfr)
使用 -c copy 裁剪后开头出现黑帧改用重新编码而非流复制
输出文件为 0 字节使用 -v error 查看完整错误;检查流映射
路径中包含空格用引号包裹:"my clip.mp4"
覆盖提示阻塞自动化添加 -y(覆盖)或 -n(从不覆盖)

安全习惯

  • 输出到 out/ 目录,绝不写入输入路径。
  • 在脚本中添加 -hide_banner -loglevel error,添加 -stats 可查看进度。
  • 使用 ffprobe 和 video-qa 技能检查结果。

Individual skills in this repo

This repo contains 6 individual skills — each has its own dedicated page.

iamaanahmad/captions-subtitles

Create, edit, translate and burn in captions and subtitles - transcribe speech with Whisper, write SRT/VTT/ASS files, style them, and hard-burn word-highlight captions for Shorts, Reels and TikTok. Use for any captioning, subtitle, transcript or accessibility request.

iamaanahmad/motion-design-craft

Motion design rules for programmatic and edited video - durations, easing, springs, staggering, read time for on-screen text, beat grids, transitions and visual hierarchy, plus the defaults that make a video look AI-generated. Use when animating scenes, choosing timings, or reviewing a video that feels off.

iamaanahmad/moviepy-python-video

Compose and edit video in pure Python with moviepy 2.x - overlay text, lower thirds and captions on footage, cut and join clips, add audio, build short single-file build.py videos without Node. Use when the user wants Python-based video editing or deterministic text over AI-generated video.

iamaanahmad/remotion-programmatic-video

Build videos as React code with Remotion - compositions, sequences, springs, interpolation, audio sync, data-driven props, studio preview and rendering to MP4. Use for explainers, product demos, release notes, data charts, branded templates, or any video that should be reproducible from code.

iamaanahmad/screen-recording-demos

Record clean product and web app demos - scripted browser recording with Playwright, cursor and zoom polish, speed ramps, trimming dead time and syncing to narration. Use when the user wants a walkthrough, tutorial, UI demo or release video of a web app, or a repeatable recording.

iamaanahmad/voiceover-tts

Generate and time narration with text-to-speech (ElevenLabs, OpenAI TTS, Kokoro, Piper) or prepare a recorded voice - script prep, per-scene audio files, pacing, pronunciation, cleanup and loudness. Use when the user needs a voiceover, narration, dubbing, or voice cloning guidance.

相关技能