Communitygithub.com

iamaanahmad/ffmpeg-essentials

包含 FFmpeg 與 ffprobe 的核心配方,用於修剪、合併、轉換、調整大小、壓縮、變速、淡入淡出、音訊擷取與 GIF 轉換。適用於任何直接媒體轉換任務,或當影片無法播放、尺寸異常、不同步時使用。

ffmpeg-essentials 是什麼?

這是一個針對開發者與 AI 代理設計的 FFmpeg 及 ffprobe 核心技巧集,提供常見媒體處理任務的精準命令與最佳實踐。此技能涵蓋修剪(Trim)、合併(Join)、調整大小與重新構圖(Resize & Reframe)、壓縮(Compress)、變速(Speed)、淡入淡出(Fades)、音訊處理(Audio)、GIF 轉換(GIF)以及靜態圖片處理(Images),並包含疑難排解與安全習慣。使用時,它會優先建議使用 `ffprobe` 進行探查,避免覆寫輸入檔案,並在需要精確剪輯時進行重新編碼。典型使用場景包括:當影片無法播放、尺寸異常、音訊不同步、需要快速裁剪片段或從影片中擷取音軌。此技能支援所有標準的 FFmpeg 安裝環境(如 macOS、Linux、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

# 機器可讀
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預設 (Preset)
母版 / 歸檔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 倍。

淡入淡出

# 1 秒淡入,1 秒淡出,作用於 10 秒的片段
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加入 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.

相關技能