Communitygithub.com

FFmpeg 媒体处理技能

FFmpeg 是媒体处理的瑞士军刀,几乎支持所有音视频格式、编解码器和容器。本技能涵盖格式转换、编解码转码、裁剪、拼接、滤镜、压缩、缩略图提取、字幕嵌入、音频标准化、GIF 制作、水印添加,以及与 yt-dlp 和 gallery-dl 等工具的流水线集成。

FFmpeg 媒体处理技能 是什么?

FFmpeg 是业界公认的媒体处理瑞士军刀,本技能为其提供了全面的开发者指南,涵盖 10 个核心操作步骤和丰富的实战示例。它详细解释了如何使用 `ffmpeg` 和 `ffprobe` 进行格式转换(如 WebM 转 MP4)、音频提取与标准化、视频裁剪与拼接、分辨率调整与压缩、缩略图与截图生成、字幕烧录与嵌入,以及各种滤镜效果(如 GIF 制作、水印、变速、旋转、淡入淡出)。技能特别强调与 yt-dlp 和 gallery-dl 的集成,为处理从这些工具下载的媒体文件提供了最佳实践。通过支持流复制(-c copy)以避免不必要的重新编码,并推荐使用 -movflags +faststart 优化移动端播放体验,该技能在确保最高兼容性的同时追求处理效率。提供的批量处理脚本和 ffprobe 信息查询命令,使自动化大规模媒体处理任务变得简单直接。此技能适用于任何支持 CLI 工具调用的 AI Agent 平台(如 Claude Code、Cursor、Codex),是处理音视频编辑、转码、归档和发布工作的必备工具。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ComeOnOliver/skillshub/tree/HEAD/skills/TerminalSkills/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 上播放。”

Agent 将执行:

  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 个会议视频片段。裁剪出最精彩的部分,并将它们合并成一个带淡入淡出过渡的视频。”

Agent 将执行:

  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.

ComeOnOliver/skillshub

Create stunning, animation-rich HTML presentations from scratch or by converting PowerPoint files. Use when the user wants to build a presentation, convert a PPT/PPTX to web, or create slides for a talk/pitch. Helps non-designers discover their aesthetic through visual exploration rather than abstract choices.

ComeOnOliver/skillshub

Next.js 16+ 和 Turbopack — 增量打包、文件系统缓存、开发速度,以及何时使用 Turbopack 与 webpack。

ComeOnOliver/skillshub

SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices.

ComeOnOliver/skillshub

See, Understand, Act on video and audio. See- ingest from local files, URLs, RTSP/live feeds, or live record desktop; return realtime context and playable stream links. Understand- extract frames, build visual/semantic/temporal indexes, and search moments with timestamps and auto-clips. Act- transcode and normalize (codec, fps, resolution, aspect ratio), perform timeline edits (subtitles, text/image overlays, branding, audio overlays, dubbing, translation), generate media assets (image, audio, video), and create real time alerts for events from live streams or desktop capture.

ComeOnOliver/skillshub

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

ComeOnOliver/skillshub

Next.js 15 애플리케이션을 위한 프론트엔드 개발 가이드라인. React 19, TypeScript, Shadcn/ui, Tailwind CSS를 사용한 모던 패턴. Server Components, Client Components, App Router, 파일 구조, Shadcn/ui 컴포넌트, 성능 최적화, TypeScript 모범 사례 포함. 컴포넌트, 페이지, 기능 생성, 데이터 페칭, 스타일링, 라우팅, 프론트엔드 코드 작업 시 사용.

ComeOnOliver/skillshub

Provides Tamagui patterns for config v4, compiler optimization, styled context, and cross-platform styling. Must use when working with Tamagui projects (tamagui.config.ts, @tamagui imports).

ComeOnOliver/skillshub

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

ComeOnOliver/skillshub

Create high-converting, visually distinctive landing pages. Use when building marketing pages, product launches, SaaS homepages, or any single-page conversion-focused website. Guides section-by-section composition with anti-AI-slop principles.

ComeOnOliver/skillshub

Auto-loaded context for Portfolio Buddy 2 development. Use for ANY task involving: React 19 development, TypeScript, portfolio analysis features, metrics calculations, trading strategy comparison, or working with the Portfolio Buddy 2 codebase. Contains tech stack, known issues, and architectural constraints.

ComeOnOliver/skillshub

Next.js development tooling via MCP. Inspect routes, components, build info, and debug Next.js apps. Use when working on Next.js applications, debugging routing, or inspecting app structure. NOT for general React or non-Next.js projects.

ComeOnOliver/skillshub

Implement UI using Shadcn MCP (atoms/theme) and 21st.dev MCP (complex sections). Use when adding buttons, layouts, or generating landing pages.

ComeOnOliver/skillshub

A conceptual skill for building an API client in Next.js that handles JWT tokens

ComeOnOliver/skillshub

Best practices and patterns for Next.js App Router, Server Actions, and Routing in this project.

ComeOnOliver/skillshub

The Design System, Theme, and UX rules for the Physical AI Hub.

ComeOnOliver/skillshub

Comprehensive frontend development skill for building modern, performant web applications using ReactJS, NextJS, TypeScript, Tailwind CSS. Includes component scaffolding, performance optimization, bundle analysis, and UI best practices. Use when developing frontend features, optimizing performance, implementing UI/UX designs, managing state, or reviewing frontend code.

ComeOnOliver/skillshub

This document captures learnings from fixing keyboard navigation issues when floating components (Select, DropdownMenu, Popover) have CSS open/close animations.

ComeOnOliver/skillshub

Build Next.js 16 applications with correct patterns and distinctive design. Use when creating pages, layouts, dynamic routes, upgrading from Next.js 15, or implementing proxy.ts. Covers breaking changes (async params/searchParams, Turbopack, cacheComponents) and frontend aesthetics. NOT when building non-React or backend-only applications.

ComeOnOliver/skillshub

Comprehensive Framer Motion animation library for React. Covers motion components, variants, gestures, page transitions, and scroll animations. Use when adding animations to React/Next.js applications.

ComeOnOliver/skillshub

Build interfaces that resonate, not just render. This skill enforces bold, intentional

相关技能