Communitygithub.com

FFmpeg

轉碼、轉換、編輯和處理音訊與視訊,使用 FFmpeg。當使用者要求轉換影片格式(WebM 轉 MP4、MKV 轉 MP4)、從影片中提取音訊、壓縮影片檔案、修剪或剪輯片段、串聯影片、添加字幕、建立縮圖、套用濾鏡、更改解析度或位元率、重新編碼媒體、從影片建立 GIF、添加浮水印、標準化音訊、串流媒體或建立自動化媒體處理管道時使用。

FFmpeg 是什麼?

FFmpeg 是媒體處理的瑞士軍刀,支援幾乎所有的音訊與視訊格式、編解碼器和容器。此技能涵蓋格式轉換、編解碼轉碼、修剪、串聯、濾鏡、壓縮、縮圖提取、字幕嵌入、音訊標準化、GIF 建立、浮水印,以及與 yt-dlp 和 gallery-dl 等工具的管道整合。使用時,您應先安裝 FFmpeg,然後根據任務選擇對應的命令。對於常見的格式轉換,例如將 WebM 轉為 MP4,建議重新編碼為 H.264+AAC 以確保最大相容性,並添加 `-movflags +faststart` 以實現行動裝置的即時播放。當變更編解碼器或套用濾鏡時,必須重新編碼;否則應優先使用 `-c copy` 進行串流複製,以達到無損且即時的效果。在批量處理前,務必先使用 `ffprobe` 檢查輸入格式,避免不必要的重新編碼。此技能支援 Linux、macOS 和 Windows 平台,並與 Claude Code、Cursor 和 Codex 等 AI 代理工具整合。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/TerminalSkills/skills/tree/HEAD/skills/ffmpeg

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

FFmpeg 是做什麼的?

概述

FFmpeg 是媒體處理的瑞士軍刀。它能處理幾乎所有現有的音訊與視訊格式、編解碼器和容器。此技能涵蓋格式轉換、編解碼轉碼、修剪、串聯、濾鏡、壓縮、縮圖提取、字幕嵌入、音訊標準化、GIF 建立、浮水印,以及與 yt-dlp 和 gallery-dl 等工具的管道整合。

說明

步驟 1:安裝

# Ubuntu/Debian
sudo apt install -y ffmpeg

# macOS
brew install ffmpeg

# 驗證
ffmpeg -version
ffprobe -version   # 媒體資訊工具,與 ffmpeg 同捆

步驟 2:格式轉換

最常見的任務——在容器格式和編解碼器之間進行轉換。

# WebM → MP4(重新編碼為 H.264 + AAC 以獲得通用相容性)
ffmpeg -i input.webm -c:v libx264 -c:a aac -movflags +faststart output.mp4

# WebM → MP4(如果已是 H.264/AAC,則串流複製 — 即時,無品質損失)
ffmpeg -i input.webm -c copy output.mp4

# MKV → MP4(串流複製,無需重新編碼)
ffmpeg -i input.mkv -c copy output.mp4

# AVI → MP4
ffmpeg -i input.avi -c:v libx264 -crf 23 -c:a aac -b:a 128k output.mp4

# MOV → MP4(Apple ProRes 轉 H.264)
ffmpeg -i input.mov -c:v libx264 -crf 18 -preset slow -c:a aac output.mp4

# 任何格式 → MP4(網頁最佳化快速啟動)
ffmpeg -i input.* -c:v libx264 -crf 23 -preset medium -c:a aac -movflags +faststart output.mp4

CRF(固定位元率因子): 控制品質。數值越低,品質越好,檔案越大。

  • 18:視覺無損
  • 23:預設值,良好的平衡
  • 28:較小的檔案,部分品質損失
  • 51:最差品質

步驟 3:音訊操作

# 從影片中提取音訊
ffmpeg -i video.mp4 -vn -c:a copy audio.aac          # 複製編解碼器(快速)
ffmpeg -i video.mp4 -vn -c:a libmp3lame -q:a 2 audio.mp3  # 轉換為 MP3
ffmpeg -i video.mp4 -vn -c:a flac audio.flac          # 無損 FLAC

# 音訊格式轉換
ffmpeg -i input.wav -c:a libmp3lame -b:a 320k output.mp3
ffmpeg -i input.mp3 -c:a libopus -b:a 128k output.opus
ffmpeg -i input.flac -c:a aac -b:a 256k output.m4a

# 標準化音訊音量
ffmpeg -i input.mp3 -af loudnorm=I=-16:LRA=11:TP=-1.5 output.mp3

# 更改取樣率
ffmpeg -i input.wav -ar 44100 output.wav

# 將音訊合併到影片
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 output.mp4

步驟 4:修剪、剪輯和串聯

# 從時間戳開始修剪到指定持續時間
ffmpeg -i input.mp4 -ss 00:01:30 -t 00:00:45 -c copy clip.mp4    # 從 1:30 開始的 45 秒片段

# 從開始時間戳修剪到結束時間戳
ffmpeg -i input.mp4 -ss 00:05:00 -to 00:10:00 -c copy clip.mp4   # 從 5:00 到 10:00

# 前 30 秒
ffmpeg -i input.mp4 -t 30 -c copy first30.mp4

# 串聯檔案(先建立檔案清單)
cat > filelist.txt << 'EOF'
file 'clip1.mp4'
file 'clip2.mp4'
file 'clip3.mp4'
EOF
ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4

# 使用重新編碼串聯(當編解碼器不同時)
ffmpeg -f concat -safe 0 -i filelist.txt -c:v libx264 -c:a aac output.mp4

步驟 5:解析度與壓縮

# 調整為 1080p(保持寬高比)
ffmpeg -i input.mp4 -vf "scale=-2:1080" -c:v libx264 -crf 23 -c:a copy output.mp4

# 調整為 720p
ffmpeg -i input.mp4 -vf "scale=-2:720" -c:v libx264 -crf 23 -c:a copy output.mp4

# 調整為指定寬度(高度自動)
ffmpeg -i input.mp4 -vf "scale=1280:-2" output.mp4

# 積極壓縮(較小檔案)
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset faster -c:a aac -b:a 96k small.mp4

# 雙通道編碼以達到目標檔案大小
ffmpeg -i input.mp4 -c:v libx264 -b:v 1M -pass 1 -an -f null /dev/null
ffmpeg -i input.mp4 -c:v libx264 -b:v 1M -pass 2 -c:a aac -b:a 128k output.mp4

# 更改影格率
ffmpeg -i input.mp4 -r 30 output.mp4

步驟 6:縮圖與螢幕截圖

# 在時間戳處截取單張圖
ffmpeg -i input.mp4 -ss 00:01:30 -frames:v 1 thumb.jpg

# 每 10 秒截取一張
ffmpeg -i input.mp4 -vf "fps=1/10" thumbs/frame_%04d.jpg

# 縮圖網格(4x4 聯絡表)
ffmpeg -i input.mp4 -vf "select='not(mod(n,300))',scale=320:-1,tile=4x4" -frames:v 1 grid.jpg

# 擷取第一幀
ffmpeg -i input.mp4 -frames:v 1 first_frame.png

步驟 7:字幕

# 將字幕燒錄到影片(硬編碼)
ffmpeg -i input.mp4 -vf "subtitles=subs.srt" output.mp4

# 燒錄帶有樣式的 ASS/SSA 字幕
ffmpeg -i input.mp4 -vf "ass=subs.ass" output.mp4

# 添加字幕軌(軟字幕,使用者可切換)
ffmpeg -i input.mp4 -i subs.srt -c copy -c:s mov_text output.mp4

# 從影片提取字幕
ffmpeg -i input.mkv -map 0:s:0 subs.srt

步驟 8:濾鏡與效果

# 從影片片段建立 GIF
ffmpeg -i input.mp4 -ss 5 -t 3 -vf "fps=15,scale=480:-1:flags=lanczos" output.gif

# 使用調色板的高品質 GIF
ffmpeg -i input.mp4 -ss 5 -t 3 -vf "fps=15,scale=480:-1:flags=lanczos,palettegen" palette.png
ffmpeg -i input.mp4 -i palette.png -ss 5 -t 3 -lavfi "fps=15,scale=480:-1:flags=lanczos[v];[v][1:v]paletteuse" output.gif

# 添加浮水印
ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=W-w-10:H-h-10" output.mp4

# 加速 / 慢放
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" fast.mp4
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=2.0*PTS[v];[0:a]atempo=0.5[a]" -map "[v]" -map "[a]" slow.mp4

# 旋轉影片
ffmpeg -i input.mp4 -vf "transpose=1" rotated.mp4    # 順時針 90°
ffmpeg -i input.mp4 -vf "transpose=2" rotated.mp4    # 逆時針 90°

# 裁切影片(寬度:高度:x:y)
ffmpeg -i input.mp4 -vf "crop=1280:720:0:180" cropped.mp4

# 淡入/淡出
ffmpeg -i input.mp4 -vf "fade=in:0:30,fade=out:870:30" -af "afade=in:0:30,afade=out:870:30" output.mp4

步驟 9:媒體資訊

# 完整媒體資訊
ffprobe -v quiet -print_format json -show_format -show_streams input.mp4

# 僅持續時間
ffprobe -v quiet -show_entries format=duration -of csv=p=0 input.mp4

# 解析度
ffprobe -v quiet -select_streams v:0 -show_entries stream=width,height -of csv=p=0 input.mp4

# 編解碼器資訊
ffprobe -v quiet -select_streams v:0 -show_entries stream=codec_name -of csv=p=0 input.mp4

步驟 10:批次處理

# 將所有 WebM 檔案轉換為 MP4
for f in *.webm; do
  ffmpeg -i "$f" -c:v libx264 -crf 23 -c:a aac "${f%.webm}.mp4"
done

# 壓縮目錄中的所有 MP4 檔案
for f in *.mp4; do
  ffmpeg -i "$f" -c:v libx264 -crf 28 -preset fast -c:a aac "compressed_${f}"
done

# 從所有影片中提取音訊
for f in *.mp4; do
  ffmpeg -i "$f" -vn -c:a libmp3lame -q:a 2 "${f%.mp4}.mp3"
done

範例

範例 1:將 yt-dlp 下載的 WebM 轉換為 MP4

使用者提示: 「我使用 yt-dlp 下載了影片,但它們都是 .webm 檔案。將它們轉換為 MP4,以便在我的 iPhone 上播放。」

代理程式將:

  1. 使用 ffprobe 檢查 WebM 檔案是否包含 H.264+AAC(可串流複製)或 VP9+Opus(需要重新編碼)。
  2. 對於 VP9/Opus 檔案,執行:for f in *.webm; do ffmpeg -i "$f" -c:v libx264 -crf 23 -preset medium -c:a aac -movflags +faststart "${f%.webm}.mp4"; done
  3. -movflags +faststart 旗標將中繼資料移至檔案開頭,從而在行動裝置和網頁瀏覽器上實現即時播放。
  4. 報告轉換結果,包含轉換前後的檔案大小。

範例 2:從多個片段建立精彩片段

使用者提示: 「我有 5 個來自會議的影片剪輯。修剪最佳部分,並將它們合併為一個帶有淡入淡出轉場的影片。」

代理程式將:

  1. 使用 -ss 和 -to 旗標將每個剪輯修剪到指定的時間戳。
  2. 將所有剪輯重新編碼為匹配的編解碼器/解析度:ffmpeg -i clip.mp4 -c:v libx264 -crf 23 -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:-1:-1,fade=in:0:15,fade=out:st=DURATION:d=0.5" -c:a aac -ar 44100 segment_N.mp4
  3. 建立串聯檔案清單並合併:ffmpeg -f concat -safe 0 -i filelist.txt -c copy highlight_reel.mp4

指南

  • 轉換 yt-dlp 下載(通常是 WebM/VP9+Opus)時,始終重新編碼為 H.264+AAC 以獲得最大裝置相容性。使用 -c:v libx264 -crf 23 -c:a aac -movflags +faststart。
  • 盡可能使用串流複製(-c copy)以避免重新編碼——它是即時且無損的。僅在變更編解碼器、套用濾鏡或來源與目標容器不相容時才重新編碼。
  • 為網頁或行動裝置建立 MP4 檔案時,始終添加 -movflags +faststart;它將 moov atom 移至檔案開頭,以實現即時串流。
  • 在處理前使用 ffprobe 了解輸入格式,避免不必要的重新編碼。
  • 對於批次操作,始終先在單一檔案上測試命令,然後再對整個目錄執行。
  • 對於單通道編碼,優先使用 CRF 模式而非位元率模式;它在不同內容類型上能產生一致的品質。

Individual skills in this repo

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

TerminalSkills/skills

>- Run GitHub Actions locally with act. Use when a user asks to test GitHub Actions workflows locally, debug CI pipelines without pushing, or run workflows offline.

TerminalSkills/skills

>- You are an expert in AG2 (formerly AutoGen), the open-source multi-agent conversation framework. You help developers build systems where multiple AI agents collaborate through structured conversations — with tool use, human-in-the-loop, code execution, group chat orchestration, and nested conversations — for complex tasks like software development, research, and data analysis.

TerminalSkills/skills

>- Generate short-form videos with AI — script writing, text-to-speech narration, stock footage selection, subtitle generation, and video assembly. Use when: creating TikTok/YouTube Shorts/Reels content, automating video production, building content pipelines.

TerminalSkills/skills

>- Animate 3D objects and characters in Blender with Python. Use when the user wants to keyframe properties, create armatures and rigs, set up IK/FK chains, animate shape keys for facial animation, edit F-Curves, use the NLA editor to blend actions, add drivers for expression-based animation, or script any animation workflow in Blender.

TerminalSkills/skills

>- Automate motion capture and tracking workflows in Blender with Python. Use when the user wants to import BVH or FBX mocap data, retarget motion to armatures, track camera or object motion from video, solve camera motion, clean up motion capture data, or script any tracking pipeline in Blender.

TerminalSkills/skills

>- You are an expert in Bolt.new by StackBlitz, the AI-powered full-stack development environment that runs entirely in the browser. You help developers go from idea to deployed app in minutes using natural language prompts — Bolt generates complete applications with frontend, backend, database, and deployment, all running in a WebContainer without local setup.

TerminalSkills/skills

>- Assists with using Bun as an all-in-one JavaScript/TypeScript runtime, package manager, bundler, and test runner. Use when building HTTP servers, managing packages, running tests, or migrating from Node.js. Trigger words: bun, bun serve, bun install, bun test, bun build, javascript runtime, bun runtime.

TerminalSkills/skills

>- Self-host customer support with Chatwoot. Use when a user asks to set up open-source customer support, add live chat without SaaS costs, build a multi-channel inbox, or deploy a free Intercom alternative.

TerminalSkills/skills

>- You are an expert in Chi, the lightweight, idiomatic Go HTTP router built on `net/http`. You help developers build composable HTTP services using Chi's middleware stack, route groups, URL parameters, sub-routers, and context-based request scoping — providing Express-like ergonomics while staying 100% compatible with Go's standard library.

TerminalSkills/skills

When the user wants to edit, review, or improve existing marketing copy. Also use when the user mentions 'edit this copy,' 'review my copy,' 'copy feedback,' 'proofread,' 'polish this,' 'make this better,' or 'copy sweep.' This skill provides a systematic approach to editing marketing copy through multiple focused passes.

TerminalSkills/skills

>- Add live chat and customer support with Crisp. Use when a user asks to add a chat widget, implement live customer support, set up a help desk, create a knowledge base, or add a chatbot to a website.

TerminalSkills/skills

>- Assists with building custom interactive data visualizations using D3.js. Use when creating charts, graphs, maps, force layouts, or hierarchical diagrams that require fine-grained control beyond what charting libraries provide. Trigger words: d3, data visualization, chart, svg, scales, force graph, treemap, choropleth.

TerminalSkills/skills

dbt (data build tool) transforms data in your warehouse using SQL SELECT statements. Learn project setup, models, tests, documentation, incremental materializations, and integration with data warehouses like PostgreSQL, BigQuery, and Snowflake.

TerminalSkills/skills

>- You are an expert in dlt, the open-source Python library for building data pipelines. You help developers load data from any API, file, or database into warehouses and lakes using simple Python decorators — with automatic schema inference, incremental loading, and built-in data contracts. dlt is the "requests library for data pipelines.

TerminalSkills/skills

Data Version Control for ML projects. Track large datasets and models alongside Git, build reproducible ML pipelines, and run experiments with metric comparison. Works with any storage backend including S3, GCS, Azure, and local filesystems.

TerminalSkills/skills

>- You are an expert in E2B, the cloud platform for running AI-generated code in secure sandboxes. You help developers give AI agents the ability to execute code, install packages, read/write files, and run long processes in isolated cloud environments — each sandbox is a lightweight VM that boots in ~150ms with full Linux, filesystem, and networking.

TerminalSkills/skills

>- Create advanced animations with GSAP. Use when building complex timelines, scroll-driven animations, SVG morphing, text animations, or high-performance web animations beyond CSS capabilities.

TerminalSkills/skills

>- Generate deterministic MP4 videos from HTML, CSS, media, and seekable animations using HeyGen's HyperFrames framework. Use when someone asks to "render HTML to video", "make a video with HyperFrames", "create a programmatic video", "turn an animation into MP4", "build a launch/product video from HTML", "render GSAP/Lottie/Three.js to video", or set up an agent-driven video pipeline. Covers init/preview/render/add/lint/inspect commands, data-* timing attributes, animation adapters, and the component catalog.

TerminalSkills/skills

When the user wants to perform load testing, stress testing, or performance testing of APIs and websites using k6. Also use when the user mentions "k6," "load test," "performance test," "stress test," "spike test," "soak test," "thresholds," "virtual users," or "VUs." For browser-based testing, see selenium.

TerminalSkills/skills

>- Lovable (formerly GPT Engineer) is a browser-based AI app builder that turns a natural-language description into a working full-stack web app with a built-in backend, authentication, database and one-click publishing. Use when the user wants to prototype or ship a web app with Lovable, write effective Lovable prompts, connect Supabase or GitHub, review generated security rules, or move a Lovable project into their own editor.

相關技能