Communitygithub.com

终端技能/FFmpeg

使用 FFmpeg 进行音视频转码、转换、编辑和处理。当用户需要转换视频格式(如 WebM 转 MP4、MKV 转 MP4)、从视频中提取音频、压缩视频文件、裁剪片段、拼接视频、添加字幕、生成缩略图、应用滤镜、调整分辨率或比特率、重新编码媒体、从视频创建 GIF、添加水印、标准化音频、流媒体或构建自动化媒体处理流水线时使用。

FFmpeg 是什么?

该技能围绕 FFmpeg 构建,FFmpeg 是媒体处理领域的瑞士军刀,支持几乎所有音视频格式、编解码器和容器。技能覆盖了格式转换、编解码器转码、修剪、拼接、滤镜、压缩、缩略图提取、字幕嵌入、音频标准化、GIF 创建、水印添加以及集成 yt-dlp 和 gallery-dl 等工具的流水线操作。提供了从安装验证(Ubuntu/Debian、macOS)到具体操作命令的完整指导,包括:格式转换(WebM/ MKV/ AVI/ MOV 转 MP4,支持流复制或重新编码)、音频操作(提取、格式转换、标准化、采样率调整)、视频修剪与拼接(按时间戳裁剪、合并文件列表)、分辨率与压缩调整(缩放至 1080p/720p、CRF 质量控制、双遍编码)、缩略图与截图生成、字幕处理(硬编码/软编码)、滤镜与效果(GIF 生成、水印、变速、旋转、裁剪、淡入淡出),以及通过 ffprobe 获取媒体信息。适用于各类媒体处理自动化场景,支持跨平台运行,是开发者和高级用户必备的音视频工具集。

兼容平台~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

# 任意格式 → 带有 web 优化快速启动的 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 原子移动到文件开头,实现即时流媒体播放。
  • 在处理前使用 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.

相关技能