When to Use This Skill
- User asks to convert, transcode, or re-encode video files (MP4, MKV, WebM, AVI, MOV, etc.)
- User asks to trim, cut, split, or merge video/audio files
- User asks to resize, scale, or change video resolution
- User asks to compress video or audio for smaller file size
- User asks to extract audio from video
- User asks to convert between audio formats (MP3, WAV, PCM, OGG, AAC, FLAC, OPUS, WMA, M4A)
- User asks to add subtitles, watermarks, or text overlays to video
- User asks to create GIFs or animated WebP from video
- User asks to generate video thumbnails or screenshot frames
- User asks to get video/audio file info (duration, codec, bitrate, resolution)
- User asks to change audio bitrate, sample rate, or channels
- User asks to add/replace audio tracks in a video
- User mentions "ffmpeg", "ffprobe", or any video/audio manipulation task
Prerequisites
Requires ffmpeg and ffprobe CLI tools installed and on PATH.
Verify installation:
ffmpeg -version
ffprobe -version
Install if missing:
- macOS:
brew install ffmpeg
- Ubuntu/Debian:
sudo apt-get install ffmpeg
- Fedora/RHEL:
sudo dnf install ffmpeg-free (or enable RPM Fusion for full ffmpeg)
- Windows (scoop):
scoop install ffmpeg
- Windows (choco):
choco install ffmpeg
- Windows (winget):
winget install --id Gyan.FFmpeg
Workflow
1) Pre-flight check
Verify ffmpeg is available before running any operation:
command -v ffmpeg &> /dev/null || { echo "FFmpeg not found. Install: brew install ffmpeg (macOS) or sudo apt-get install ffmpeg (Linux)"; exit 1; }
2) Determine the operation
Identify what the user needs from these categories:
| Category |
Operations |
| Info |
Duration, codec, bitrate, resolution, metadata |
| Video Convert |
Format conversion, codec change, quality settings |
| Video Resize |
Scale, change resolution, aspect ratio |
| Video Trim |
Cut, split, extract segment |
| Video Merge |
Concatenate, join multiple files |
| Video Compress |
Reduce file size, CRF tuning |
| Extract Audio |
Strip audio track from video |
| Audio Convert |
Format conversion (MP3, WAV, OGG, AAC, FLAC, PCM, OPUS) |
| Audio Adjust |
Bitrate, sample rate, channels, volume |
| Subtitles |
Burn-in, soft subtitles, extract subtitles |
| GIF/Animated |
Video to GIF, video to animated WebP |
| Thumbnail |
Extract frame, generate preview thumbnails |
| Overlay |
Watermark, picture-in-picture |
| Streaming |
HLS, DASH segmentation |
3) Determine output filename
Default: write to a new file, never overwrite the original.
- If the user did NOT say to overwrite → generate a descriptive output filename in the same directory (e.g.
video_compressed.mp4, audio_converted.mp3).
- If the user explicitly asks to overwrite (e.g. "覆盖", "replace", "in-place", "same name", "overwrite") → use a temp file then move, since ffmpeg cannot read and write to the same file.
- For format conversion the extension naturally changes, so
video.avi → video.mp4 already preserves the original.
- For batch operations, prefer outputting to a subdirectory (
mkdir -p output).
4) Execute the operation
File Information
# Full info
ffprobe -v quiet -print_format json -show_format -show_streams input.mp4
# Duration only
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4
# Resolution only
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp4
# Codec info
ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 input.mp4
# Audio info
ffprobe -v error -select_streams a:0 -show_entries stream=codec_name,sample_rate,channels,bit_rate -of json input.mp4
# Human-readable summary
ffprobe -hide_banner input.mp4
Video Format Conversion
# Basic conversion (codec inferred from extension)
ffmpeg -i input.avi output.mp4
# AVI/MKV to MP4 (H.264 + AAC, widely compatible)
ffmpeg -i input.avi -c:v libx264 -c:a aac -b:a 192k output.mp4
# MP4 to WebM (VP9 + Opus)
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus output.webm
# MP4 to MKV (copy streams without re-encoding — fast)
ffmpeg -i input.mp4 -c copy output.mkv
# MOV to MP4 (copy if compatible codecs)
ffmpeg -i input.mov -c copy output.mp4
# Re-encode with H.265/HEVC for better compression
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -c:a aac -b:a 128k output_h265.mp4
Video Compression
# Good quality, reasonable size (CRF 23 is default, lower = better quality)
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4
# Smaller file (higher CRF = more compression, lower quality)
ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset slow -c:a aac -b:a 96k output_small.mp4
# Fast compression (lower quality, fast encoding)
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset ultrafast -c:a copy output_fast.mp4
# Target file size (e.g., ~50MB for a 10-minute video)
# bitrate = target_size_bits / duration_seconds
# 50MB = 400Mbit; 400Mbit / 600s ≈ 667kbit/s video (subtract ~128k for audio)
ffmpeg -i input.mp4 -c:v libx264 -b:v 540k -c:a aac -b:a 128k -pass 1 -f null /dev/null && \
ffmpeg -i input.mp4 -c:v libx264 -b:v 540k -c:a aac -b:a 128k -pass 2 output.mp4
CRF reference (H.264): 0 = lossless, 18 = visually lossless, 23 = default, 28 = noticeable loss, 51 = worst.
Preset reference: ultrafast, superfast, veryfast, faster, fast, medium (default), slow, slower, veryslow. Slower = better compression ratio.
Video Resizing
# Scale to 1280x720 (force exact, may distort)
ffmpeg -i input.mp4 -vf "scale=1280:720" -c:a copy output_720p.mp4
# Scale width to 1280, auto height (maintain aspect ratio)
ffmpeg -i input.mp4 -vf "scale=1280:-2" -c:a copy output.mp4
# Scale to 50%
ffmpeg -i input.mp4 -vf "scale=iw/2:ih/2" -c:a copy output_half.mp4
# Scale height to 720, auto width
ffmpeg -i input.mp4 -vf "scale=-2:720" -c:a copy output_720p.mp4
Use -2 instead of -1 to ensure dimensions are divisible by 2 (required by most codecs).
Video Trimming / Cutting
# Extract from 00:01:30 to 00:03:00 (copy without re-encoding — fast, may have keyframe imprecision)
ffmpeg -i input.mp4 -ss 00:01:30 -to 00:03:00 -c copy output_clip.mp4
# Extract with re-encoding (precise cuts)
ffmpeg -i input.mp4 -ss 00:01:30 -to 00:03:00 -c:v libx264 -c:a aac output_clip.mp4
# First 30 seconds
ffmpeg -i input.mp4 -t 30 -c copy output_first30s.mp4
# Skip first 10 seconds
ffmpeg -i input.mp4 -ss 10 -c copy output_skip10s.mp4
Time formats: HH:MM:SS, HH:MM:SS.mmm, or seconds (e.g. 90 = 1m30s).
Video Merging / Concatenation
# Create file list
cat > filelist.txt << 'EOF'
file 'part1.mp4'
file 'part2.mp4'
file 'part3.mp4'
EOF
# Concatenate (same codec, resolution, frame rate — fast)
ffmpeg -f concat -safe 0 -i filelist.txt -c copy output_merged.mp4
# Concatenate with re-encoding (different formats/resolutions)
ffmpeg -f concat -safe 0 -i filelist.txt -c:v libx264 -c:a aac output_merged.mp4
# Clean up file list
rm filelist.txt
Extract Audio from Video
# Extract as MP3
ffmpeg -i input.mp4 -vn -c:a libmp3lame -b:a 192k output.mp3
# Extract as WAV (lossless)
ffmpeg -i input.mp4 -vn -c:a pcm_s16le output.wav
# Extract as AAC (copy if already AAC — fast)
ffmpeg -i input.mp4 -vn -c:a copy output.aac
# Extract as OGG
ffmpeg -i input.mp4 -vn -c:a libvorbis -q:a 6 output.ogg
# Extract as FLAC
ffmpeg -i input.mp4 -vn -c:a flac output.flac
Audio Format Conversion
# MP3 to WAV
ffmpeg -i input.mp3 -c:a pcm_s16le output.wav
# WAV to MP3 (CBR 320k)
ffmpeg -i input.wav -c:a libmp3lame -b:a 320k output.mp3
# WAV to MP3 (VBR, quality 0 = best ~245kbps, 9 = lowest ~65kbps)
ffmpeg -i input.wav -c:a libmp3lame -q:a 0 output.mp3
# MP3 to OGG (Vorbis)
ffmpeg -i input.mp3 -c:a libvorbis -q:a 6 output.ogg
# WAV to OGG
ffmpeg -i input.wav -c:a libvorbis -q:a 6 output.ogg
# OGG to MP3
ffmpeg -i input.ogg -c:a libmp3lame -b:a 256k output.mp3
# WAV to AAC
ffmpeg -i input.wav -c:a aac -b:a 256k output.m4a
# WAV to FLAC (lossless)
ffmpeg -i input.wav -c:a flac output.flac
# FLAC to MP3
ffmpeg -i input.flac -c:a libmp3lame -b:a 320k output.mp3
# WAV to OPUS (excellent quality-to-size ratio)
ffmpeg -i input.wav -c:a libopus -b:a 128k output.opus
# Any format to WAV (universal intermediate)
ffmpeg -i input.any -c:a pcm_s16le -ar 44100 -ac 2 output.wav
# PCM raw to WAV (must specify format, sample rate, channels)
ffmpeg -f s16le -ar 44100 -ac 2 -i input.pcm output.wav
# WAV to PCM raw
ffmpeg -i input.wav -f s16le -acodec pcm_s16le output.pcm
# WMA to MP3
ffmpeg -i input.wma -c:a libmp3lame -b:a 256k output.mp3
# M4A to MP3
ffmpeg -i input.m4a -c:a libmp3lame -b:a 256k output.mp3
Audio Adjustments
# Change bitrate
ffmpeg -i input.mp3 -c:a libmp3lame -b:a 128k output_128k.mp3
# Change sample rate (e.g., 44100 Hz, 22050 Hz, 16000 Hz, 8000 Hz)
ffmpeg -i input.wav -ar 16000 output_16k.wav
# Convert to mono
ffmpeg -i input.mp3 -ac 1 output_mono.mp3
# Convert to stereo
ffmpeg -i input.mp3 -ac 2 output_stereo.mp3
# Adjust volume (2.0 = double, 0.5 = half)
ffmpeg -i input.mp3 -af "volume=1.5" output_louder.mp3
# Normalize audio (loudnorm filter)
ffmpeg -i input.mp3 -af loudnorm output_normalized.mp3
# Trim audio (same syntax as video)
ffmpeg -i input.mp3 -ss 00:00:30 -to 00:02:00 -c copy output_clip.mp3
# Fade in/out (fade in 3s, fade out last 3s)
ffmpeg -i input.mp3 -af "afade=t=in:st=0:d=3,afade=t=out:st=57:d=3" output_faded.mp3
# Merge/concatenate audio files
cat > audiolist.txt << 'EOF'
file 'part1.mp3'
file 'part2.mp3'
EOF
ffmpeg -f concat -safe 0 -i audiolist.txt -c copy output_merged.mp3
rm audiolist.txt
Subtitles
# Burn subtitles into video (hardcoded, cannot be turned off)
ffmpeg -i input.mp4 -vf "subtitles=subs.srt" output_subbed.mp4
# Burn ASS/SSA subtitles (preserves styling)
ffmpeg -i input.mp4 -vf "ass=subs.ass" output_subbed.mp4
# Add soft subtitles (can be toggled in player)
ffmpeg -i input.mp4 -i subs.srt -c copy -c:s mov_text output_subbed.mp4
# Extract subtitles
ffmpeg -i input.mkv -map 0:s:0 output_subs.srt
Video to GIF / Animated Images
# Basic video to GIF (10 fps, 480px width)
ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos" output.gif
# High quality GIF with palette generation (two-pass)
ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,palettegen" palette.png
ffmpeg -i input.mp4 -i palette.png -lavfi "fps=10,scale=480:-1:flags=lanczos [x]; [x][1:v] paletteuse" output.gif
# GIF from specific segment
ffmpeg -ss 5 -t 3 -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" output.gif
# Video to animated WebP
ffmpeg -i input.mp4 -vf "fps=15,scale=480:-1" -loop 0 output.webp
Thumbnails / Frame Extraction
# Extract single frame at specific time
ffmpeg -ss 00:00:10 -i input.mp4 -frames:v 1 thumbnail.jpg
# Extract frame every N seconds (e.g., every 10 seconds)
ffmpeg -i input.mp4 -vf "fps=1/10" thumbnails_%03d.jpg
# Extract frame at percentage (e.g., 25% into the video)
# First get duration, then calculate timestamp
duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4)
timestamp=$(echo "$duration * 0.25" | bc)
ffmpeg -ss "$timestamp" -i input.mp4 -frames:v 1 thumbnail_25pct.jpg
# Contact sheet / tile preview (4x4 grid)
ffmpeg -i input.mp4 -vf "select='not(mod(n\,100))',scale=320:-1,tile=4x4" -frames:v 1 contact_sheet.jpg
Watermark / Overlay
# Image watermark (bottom-right corner)
ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=W-w-10:H-h-10" output.mp4
# Text overlay
ffmpeg -i input.mp4 -vf "drawtext=text='Sample':fontsize=24:fontcolor=white:x=10:y=10" output.mp4
# Semi-transparent watermark
ffmpeg -i input.mp4 -i watermark.png -filter_complex "[1:v]format=rgba,colorchannelmixer=aa=0.3[wm];[0:v][wm]overlay=W-w-10:H-h-10" output.mp4
# Picture-in-picture
ffmpeg -i main.mp4 -i pip.mp4 -filter_complex "[1:v]scale=320:-1[pip];[0:v][pip]overlay=W-w-10:10" output.mp4
Add / Replace Audio in Video
# Replace audio track
ffmpeg -i input.mp4 -i new_audio.mp3 -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 output.mp4
# Add audio to video (mix with original)
ffmpeg -i input.mp4 -i bgm.mp3 -filter_complex "[0:a][1:a]amix=inputs=2:duration=first[aout]" -map 0:v -map "[aout]" -c:v copy output.mp4
# Remove audio from video (mute)
ffmpeg -i input.mp4 -an -c:v copy output_muted.mp4
Speed Change
# Speed up video 2x (with audio pitch correction)
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output_2x.mp4
# Slow down video 0.5x
ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=2.0*PTS[v];[0:a]atempo=0.5[a]" -map "[v]" -map "[a]" output_slow.mp4
# Speed up audio only
ffmpeg -i input.mp3 -af "atempo=1.5" output_fast.mp3
Batch Processing
# Convert all AVI to MP4
for f in *.avi; do
ffmpeg -i "$f" -c:v libx264 -c:a aac "${f%.avi}.mp4"
done
# Compress all MP4 in directory
mkdir -p compressed
for f in *.mp4; do
ffmpeg -i "$f" -c:v libx264 -crf 28 -preset slow -c:a aac -b:a 128k "compressed/$f"
done
# Convert all WAV to MP3
for f in *.wav; do
ffmpeg -i "$f" -c:a libmp3lame -b:a 192k "${f%.wav}.mp3"
done
# Convert all FLAC to OGG
for f in *.flac; do
ffmpeg -i "$f" -c:a libvorbis -q:a 6 "${f%.flac}.ogg"
done
Streaming Formats (HLS / DASH)
# Create HLS stream
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls -hls_time 10 -hls_list_size 0 output.m3u8
# Create multiple bitrate HLS (adaptive)
ffmpeg -i input.mp4 \
-map 0:v -map 0:a -c:v libx264 -c:a aac \
-b:v:0 800k -s:v:0 640x360 \
-b:v:1 1400k -s:v:0 1280x720 \
-f hls -hls_time 10 -master_pl_name master.m3u8 \
-var_stream_map "v:0,a:0 v:1,a:0" \
stream_%v/output.m3u8
5) Common Codec Quick Reference
Video Codecs
| Codec |
FFmpeg encoder |
Use case |
| H.264 |
libx264 |
Most compatible, good quality |
| H.265/HEVC |
libx265 |
Better compression, less compatible |
| VP9 |
libvpx-vp9 |
WebM, web playback |
| AV1 |
libaom-av1 |
Best compression, slow encoding |
| Copy |
copy |
No re-encoding (fast, lossless) |
Audio Codecs
| Codec |
FFmpeg encoder |
Extension |
Use case |
| MP3 |
libmp3lame |
.mp3 |
Universal compatibility |
| AAC |
aac |
.m4a, .aac |
Apple/mobile, streaming |
| Vorbis |
libvorbis |
.ogg |
Open format, gaming |
| Opus |
libopus |
.opus |
Best quality-per-bit, VoIP, WebRTC |
| FLAC |
flac |
.flac |
Lossless compression |
| PCM |
pcm_s16le |
.wav |
Uncompressed, editing |
| WMA |
wmav2 |
.wma |
Windows legacy |
6) Performance Tips
# Use hardware acceleration (macOS — VideoToolbox)
ffmpeg -i input.mp4 -c:v h264_videotoolbox -b:v 5M -c:a aac output.mp4
# Use hardware acceleration (NVIDIA — NVENC)
ffmpeg -i input.mp4 -c:v h264_nvenc -preset fast -c:a aac output.mp4
# Use hardware acceleration (Intel — QSV)
ffmpeg -i input.mp4 -c:v h264_qsv -preset faster -c:a aac output.mp4
# Use multiple threads
ffmpeg -threads 0 -i input.mp4 -c:v libx264 -c:a aac output.mp4
# Suppress banner for cleaner output
ffmpeg -hide_banner -i input.mp4 ...
Guidelines
Preserve original files by default. Unless the user explicitly asks to overwrite (e.g. "覆盖", "replace", "in-place"), ALWAYS output to a new filename. Naming conventions:
- Convert:
video.avi → video.mp4
- Compress:
video.mp4 → video_compressed.mp4
- Trim:
video.mp4 → video_clip.mp4
- Resize:
video.mp4 → video_720p.mp4
- Audio extract:
video.mp4 → video.mp3
- Audio convert:
audio.wav → audio.mp3
- Batch: output to a subdirectory (e.g.
compressed/, converted/)
Use -c copy when possible for fast, lossless operations (format remux, trimming at keyframes)
Always quote file paths that might contain spaces
Use -y flag to automatically overwrite output (only when user confirms)
Use -hide_banner to suppress version info for cleaner output
Prefer two-pass encoding for target file size
Use -movflags +faststart for MP4 files intended for web streaming
Test on a sample before running batch operations
For PCM/raw audio, always specify format (-f), sample rate (-ar), and channels (-ac)
Troubleshooting
"ffmpeg: not found" — FFmpeg not installed or not on PATH. Install via brew install ffmpeg (macOS), sudo apt-get install ffmpeg (Linux), or scoop install ffmpeg / choco install ffmpeg (Windows).
"Unknown encoder" — FFmpeg was compiled without that codec. Check available encoders: ffmpeg -encoders | grep <codec>. May need to reinstall with codec support (e.g., brew install ffmpeg includes most codecs by default).
"Invalid data found when processing input" — File may be corrupted or format not recognized. Check with ffprobe input.file.
"Output file is empty" — Likely a timestamp error in trimming. Verify timestamps are within the file's duration.
"height not divisible by 2" — Use -2 instead of -1 in scale filter: scale=1280:-2.
"Avi/mp4 codec not compatible for -c copy" — Source and target container have incompatible codecs. Re-encode instead of copying: remove -c copy and specify codecs explicitly.
1---2name: ffmpeg3description: Process and manipulate video and audio files using FFmpeg CLI (`ffmpeg`, `ffprobe`). Supports video transcoding, format conversion, trimming, merging, resizing, extracting audio, adding subtitles, GIF creation, thumbnails, and audio format conversion (MP3, WAV, PCM, OGG, AAC, FLAC, OPUS, WMA). Trigger whenever the user asks to convert, trim, merge, compress, resize, or transform video or audio files, extract audio from video, add subtitles/watermarks, create GIFs from video, generate video thumbnails, or convert between audio formats, or says phrases like "convert video", "compress video", "trim video", "merge videos", "extract audio", "video to gif", "video thumbnail", "video to mp4", "mp4 to webm", "mp3 to wav", "wav to ogg", "convert audio", "audio to mp3", "pcm to wav", "ogg to mp3", "aac to mp3", "flac to mp3", "compress audio", "change bitrate", "视频处理", "视频转换", "视频压缩", "视频裁剪", "视频合并", "提取音频", "视频转GIF", "音频转换", "音频处理", "音频压缩", "ffmpeg", "ffprobe".4license: Apache-2.05---67# When to Use This Skill89- User asks to convert, transcode, or re-encode video files (MP4, MKV, WebM, AVI, MOV, etc.)10- User asks to trim, cut, split, or merge video/audio files11- User asks to resize, scale, or change video resolution12- User asks to compress video or audio for smaller file size13- User asks to extract audio from video14- User asks to convert between audio formats (MP3, WAV, PCM, OGG, AAC, FLAC, OPUS, WMA, M4A)15- User asks to add subtitles, watermarks, or text overlays to video16- User asks to create GIFs or animated WebP from video17- User asks to generate video thumbnails or screenshot frames18- User asks to get video/audio file info (duration, codec, bitrate, resolution)19- User asks to change audio bitrate, sample rate, or channels20- User asks to add/replace audio tracks in a video21- User mentions "ffmpeg", "ffprobe", or any video/audio manipulation task2223# Prerequisites2425Requires `ffmpeg` and `ffprobe` CLI tools installed and on PATH.2627**Verify installation:**2829```bash30ffmpeg -version31ffprobe -version32```3334**Install if missing:**3536- macOS: `brew install ffmpeg`37- Ubuntu/Debian: `sudo apt-get install ffmpeg`38- Fedora/RHEL: `sudo dnf install ffmpeg-free` (or enable RPM Fusion for full `ffmpeg`)39- Windows (scoop): `scoop install ffmpeg`40- Windows (choco): `choco install ffmpeg`41- Windows (winget): `winget install --id Gyan.FFmpeg`4243# Workflow4445## 1) Pre-flight check4647Verify `ffmpeg` is available before running any operation:4849```bash50command -v ffmpeg &> /dev/null || { echo "FFmpeg not found. Install: brew install ffmpeg (macOS) or sudo apt-get install ffmpeg (Linux)"; exit 1; }51```5253## 2) Determine the operation5455Identify what the user needs from these categories:5657| Category | Operations |58|---|---|59| Info | Duration, codec, bitrate, resolution, metadata |60| Video Convert | Format conversion, codec change, quality settings |61| Video Resize | Scale, change resolution, aspect ratio |62| Video Trim | Cut, split, extract segment |63| Video Merge | Concatenate, join multiple files |64| Video Compress | Reduce file size, CRF tuning |65| Extract Audio | Strip audio track from video |66| Audio Convert | Format conversion (MP3, WAV, OGG, AAC, FLAC, PCM, OPUS) |67| Audio Adjust | Bitrate, sample rate, channels, volume |68| Subtitles | Burn-in, soft subtitles, extract subtitles |69| GIF/Animated | Video to GIF, video to animated WebP |70| Thumbnail | Extract frame, generate preview thumbnails |71| Overlay | Watermark, picture-in-picture |72| Streaming | HLS, DASH segmentation |7374## 3) Determine output filename7576**Default: write to a new file, never overwrite the original.**7778- If the user did NOT say to overwrite → generate a descriptive output filename in the same directory (e.g. `video_compressed.mp4`, `audio_converted.mp3`).79- If the user explicitly asks to overwrite (e.g. "覆盖", "replace", "in-place", "same name", "overwrite") → use a temp file then move, since ffmpeg cannot read and write to the same file.80- For format conversion the extension naturally changes, so `video.avi` → `video.mp4` already preserves the original.81- For batch operations, prefer outputting to a subdirectory (`mkdir -p output`).8283## 4) Execute the operation8485### File Information8687```bash88# Full info89ffprobe -v quiet -print_format json -show_format -show_streams input.mp49091# Duration only92ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp49394# Resolution only95ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp49697# Codec info98ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 input.mp499100# Audio info101ffprobe -v error -select_streams a:0 -show_entries stream=codec_name,sample_rate,channels,bit_rate -of json input.mp4102103# Human-readable summary104ffprobe -hide_banner input.mp4105```106107### Video Format Conversion108109```bash110# Basic conversion (codec inferred from extension)111ffmpeg -i input.avi output.mp4112113# AVI/MKV to MP4 (H.264 + AAC, widely compatible)114ffmpeg -i input.avi -c:v libx264 -c:a aac -b:a 192k output.mp4115116# MP4 to WebM (VP9 + Opus)117ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus output.webm118119# MP4 to MKV (copy streams without re-encoding — fast)120ffmpeg -i input.mp4 -c copy output.mkv121122# MOV to MP4 (copy if compatible codecs)123ffmpeg -i input.mov -c copy output.mp4124125# Re-encode with H.265/HEVC for better compression126ffmpeg -i input.mp4 -c:v libx265 -crf 28 -c:a aac -b:a 128k output_h265.mp4127```128129### Video Compression130131```bash132# Good quality, reasonable size (CRF 23 is default, lower = better quality)133ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4134135# Smaller file (higher CRF = more compression, lower quality)136ffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset slow -c:a aac -b:a 96k output_small.mp4137138# Fast compression (lower quality, fast encoding)139ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset ultrafast -c:a copy output_fast.mp4140141# Target file size (e.g., ~50MB for a 10-minute video)142# bitrate = target_size_bits / duration_seconds143# 50MB = 400Mbit; 400Mbit / 600s ≈ 667kbit/s video (subtract ~128k for audio)144ffmpeg -i input.mp4 -c:v libx264 -b:v 540k -c:a aac -b:a 128k -pass 1 -f null /dev/null && \145ffmpeg -i input.mp4 -c:v libx264 -b:v 540k -c:a aac -b:a 128k -pass 2 output.mp4146```147148CRF reference (H.264): 0 = lossless, 18 = visually lossless, 23 = default, 28 = noticeable loss, 51 = worst.149150Preset reference: `ultrafast`, `superfast`, `veryfast`, `faster`, `fast`, `medium` (default), `slow`, `slower`, `veryslow`. Slower = better compression ratio.151152### Video Resizing153154```bash155# Scale to 1280x720 (force exact, may distort)156ffmpeg -i input.mp4 -vf "scale=1280:720" -c:a copy output_720p.mp4157158# Scale width to 1280, auto height (maintain aspect ratio)159ffmpeg -i input.mp4 -vf "scale=1280:-2" -c:a copy output.mp4160161# Scale to 50%162ffmpeg -i input.mp4 -vf "scale=iw/2:ih/2" -c:a copy output_half.mp4163164# Scale height to 720, auto width165ffmpeg -i input.mp4 -vf "scale=-2:720" -c:a copy output_720p.mp4166```167168Use `-2` instead of `-1` to ensure dimensions are divisible by 2 (required by most codecs).169170### Video Trimming / Cutting171172```bash173# Extract from 00:01:30 to 00:03:00 (copy without re-encoding — fast, may have keyframe imprecision)174ffmpeg -i input.mp4 -ss 00:01:30 -to 00:03:00 -c copy output_clip.mp4175176# Extract with re-encoding (precise cuts)177ffmpeg -i input.mp4 -ss 00:01:30 -to 00:03:00 -c:v libx264 -c:a aac output_clip.mp4178179# First 30 seconds180ffmpeg -i input.mp4 -t 30 -c copy output_first30s.mp4181182# Skip first 10 seconds183ffmpeg -i input.mp4 -ss 10 -c copy output_skip10s.mp4184```185186Time formats: `HH:MM:SS`, `HH:MM:SS.mmm`, or seconds (e.g. `90` = 1m30s).187188### Video Merging / Concatenation189190```bash191# Create file list192cat > filelist.txt << 'EOF'193file 'part1.mp4'194file 'part2.mp4'195file 'part3.mp4'196EOF197198# Concatenate (same codec, resolution, frame rate — fast)199ffmpeg -f concat -safe 0 -i filelist.txt -c copy output_merged.mp4200201# Concatenate with re-encoding (different formats/resolutions)202ffmpeg -f concat -safe 0 -i filelist.txt -c:v libx264 -c:a aac output_merged.mp4203204# Clean up file list205rm filelist.txt206```207208### Extract Audio from Video209210```bash211# Extract as MP3212ffmpeg -i input.mp4 -vn -c:a libmp3lame -b:a 192k output.mp3213214# Extract as WAV (lossless)215ffmpeg -i input.mp4 -vn -c:a pcm_s16le output.wav216217# Extract as AAC (copy if already AAC — fast)218ffmpeg -i input.mp4 -vn -c:a copy output.aac219220# Extract as OGG221ffmpeg -i input.mp4 -vn -c:a libvorbis -q:a 6 output.ogg222223# Extract as FLAC224ffmpeg -i input.mp4 -vn -c:a flac output.flac225```226227### Audio Format Conversion228229```bash230# MP3 to WAV231ffmpeg -i input.mp3 -c:a pcm_s16le output.wav232233# WAV to MP3 (CBR 320k)234ffmpeg -i input.wav -c:a libmp3lame -b:a 320k output.mp3235236# WAV to MP3 (VBR, quality 0 = best ~245kbps, 9 = lowest ~65kbps)237ffmpeg -i input.wav -c:a libmp3lame -q:a 0 output.mp3238239# MP3 to OGG (Vorbis)240ffmpeg -i input.mp3 -c:a libvorbis -q:a 6 output.ogg241242# WAV to OGG243ffmpeg -i input.wav -c:a libvorbis -q:a 6 output.ogg244245# OGG to MP3246ffmpeg -i input.ogg -c:a libmp3lame -b:a 256k output.mp3247248# WAV to AAC249ffmpeg -i input.wav -c:a aac -b:a 256k output.m4a250251# WAV to FLAC (lossless)252ffmpeg -i input.wav -c:a flac output.flac253254# FLAC to MP3255ffmpeg -i input.flac -c:a libmp3lame -b:a 320k output.mp3256257# WAV to OPUS (excellent quality-to-size ratio)258ffmpeg -i input.wav -c:a libopus -b:a 128k output.opus259260# Any format to WAV (universal intermediate)261ffmpeg -i input.any -c:a pcm_s16le -ar 44100 -ac 2 output.wav262263# PCM raw to WAV (must specify format, sample rate, channels)264ffmpeg -f s16le -ar 44100 -ac 2 -i input.pcm output.wav265266# WAV to PCM raw267ffmpeg -i input.wav -f s16le -acodec pcm_s16le output.pcm268269# WMA to MP3270ffmpeg -i input.wma -c:a libmp3lame -b:a 256k output.mp3271272# M4A to MP3273ffmpeg -i input.m4a -c:a libmp3lame -b:a 256k output.mp3274```275276### Audio Adjustments277278```bash279# Change bitrate280ffmpeg -i input.mp3 -c:a libmp3lame -b:a 128k output_128k.mp3281282# Change sample rate (e.g., 44100 Hz, 22050 Hz, 16000 Hz, 8000 Hz)283ffmpeg -i input.wav -ar 16000 output_16k.wav284285# Convert to mono286ffmpeg -i input.mp3 -ac 1 output_mono.mp3287288# Convert to stereo289ffmpeg -i input.mp3 -ac 2 output_stereo.mp3290291# Adjust volume (2.0 = double, 0.5 = half)292ffmpeg -i input.mp3 -af "volume=1.5" output_louder.mp3293294# Normalize audio (loudnorm filter)295ffmpeg -i input.mp3 -af loudnorm output_normalized.mp3296297# Trim audio (same syntax as video)298ffmpeg -i input.mp3 -ss 00:00:30 -to 00:02:00 -c copy output_clip.mp3299300# Fade in/out (fade in 3s, fade out last 3s)301ffmpeg -i input.mp3 -af "afade=t=in:st=0:d=3,afade=t=out:st=57:d=3" output_faded.mp3302303# Merge/concatenate audio files304cat > audiolist.txt << 'EOF'305file 'part1.mp3'306file 'part2.mp3'307EOF308ffmpeg -f concat -safe 0 -i audiolist.txt -c copy output_merged.mp3309rm audiolist.txt310```311312### Subtitles313314```bash315# Burn subtitles into video (hardcoded, cannot be turned off)316ffmpeg -i input.mp4 -vf "subtitles=subs.srt" output_subbed.mp4317318# Burn ASS/SSA subtitles (preserves styling)319ffmpeg -i input.mp4 -vf "ass=subs.ass" output_subbed.mp4320321# Add soft subtitles (can be toggled in player)322ffmpeg -i input.mp4 -i subs.srt -c copy -c:s mov_text output_subbed.mp4323324# Extract subtitles325ffmpeg -i input.mkv -map 0:s:0 output_subs.srt326```327328### Video to GIF / Animated Images329330```bash331# Basic video to GIF (10 fps, 480px width)332ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos" output.gif333334# High quality GIF with palette generation (two-pass)335ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos,palettegen" palette.png336ffmpeg -i input.mp4 -i palette.png -lavfi "fps=10,scale=480:-1:flags=lanczos [x]; [x][1:v] paletteuse" output.gif337338# GIF from specific segment339ffmpeg -ss 5 -t 3 -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" output.gif340341# Video to animated WebP342ffmpeg -i input.mp4 -vf "fps=15,scale=480:-1" -loop 0 output.webp343```344345### Thumbnails / Frame Extraction346347```bash348# Extract single frame at specific time349ffmpeg -ss 00:00:10 -i input.mp4 -frames:v 1 thumbnail.jpg350351# Extract frame every N seconds (e.g., every 10 seconds)352ffmpeg -i input.mp4 -vf "fps=1/10" thumbnails_%03d.jpg353354# Extract frame at percentage (e.g., 25% into the video)355# First get duration, then calculate timestamp356duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4)357timestamp=$(echo "$duration * 0.25" | bc)358ffmpeg -ss "$timestamp" -i input.mp4 -frames:v 1 thumbnail_25pct.jpg359360# Contact sheet / tile preview (4x4 grid)361ffmpeg -i input.mp4 -vf "select='not(mod(n\,100))',scale=320:-1,tile=4x4" -frames:v 1 contact_sheet.jpg362```363364### Watermark / Overlay365366```bash367# Image watermark (bottom-right corner)368ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=W-w-10:H-h-10" output.mp4369370# Text overlay371ffmpeg -i input.mp4 -vf "drawtext=text='Sample':fontsize=24:fontcolor=white:x=10:y=10" output.mp4372373# Semi-transparent watermark374ffmpeg -i input.mp4 -i watermark.png -filter_complex "[1:v]format=rgba,colorchannelmixer=aa=0.3[wm];[0:v][wm]overlay=W-w-10:H-h-10" output.mp4375376# Picture-in-picture377ffmpeg -i main.mp4 -i pip.mp4 -filter_complex "[1:v]scale=320:-1[pip];[0:v][pip]overlay=W-w-10:10" output.mp4378```379380### Add / Replace Audio in Video381382```bash383# Replace audio track384ffmpeg -i input.mp4 -i new_audio.mp3 -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 output.mp4385386# Add audio to video (mix with original)387ffmpeg -i input.mp4 -i bgm.mp3 -filter_complex "[0:a][1:a]amix=inputs=2:duration=first[aout]" -map 0:v -map "[aout]" -c:v copy output.mp4388389# Remove audio from video (mute)390ffmpeg -i input.mp4 -an -c:v copy output_muted.mp4391```392393### Speed Change394395```bash396# Speed up video 2x (with audio pitch correction)397ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output_2x.mp4398399# Slow down video 0.5x400ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=2.0*PTS[v];[0:a]atempo=0.5[a]" -map "[v]" -map "[a]" output_slow.mp4401402# Speed up audio only403ffmpeg -i input.mp3 -af "atempo=1.5" output_fast.mp3404```405406### Batch Processing407408```bash409# Convert all AVI to MP4410for f in *.avi; do411 ffmpeg -i "$f" -c:v libx264 -c:a aac "${f%.avi}.mp4"412done413414# Compress all MP4 in directory415mkdir -p compressed416for f in *.mp4; do417 ffmpeg -i "$f" -c:v libx264 -crf 28 -preset slow -c:a aac -b:a 128k "compressed/$f"418done419420# Convert all WAV to MP3421for f in *.wav; do422 ffmpeg -i "$f" -c:a libmp3lame -b:a 192k "${f%.wav}.mp3"423done424425# Convert all FLAC to OGG426for f in *.flac; do427 ffmpeg -i "$f" -c:a libvorbis -q:a 6 "${f%.flac}.ogg"428done429```430431### Streaming Formats (HLS / DASH)432433```bash434# Create HLS stream435ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls -hls_time 10 -hls_list_size 0 output.m3u8436437# Create multiple bitrate HLS (adaptive)438ffmpeg -i input.mp4 \439 -map 0:v -map 0:a -c:v libx264 -c:a aac \440 -b:v:0 800k -s:v:0 640x360 \441 -b:v:1 1400k -s:v:0 1280x720 \442 -f hls -hls_time 10 -master_pl_name master.m3u8 \443 -var_stream_map "v:0,a:0 v:1,a:0" \444 stream_%v/output.m3u8445```446447## 5) Common Codec Quick Reference448449### Video Codecs450451| Codec | FFmpeg encoder | Use case |452|---|---|---|453| H.264 | `libx264` | Most compatible, good quality |454| H.265/HEVC | `libx265` | Better compression, less compatible |455| VP9 | `libvpx-vp9` | WebM, web playback |456| AV1 | `libaom-av1` | Best compression, slow encoding |457| Copy | `copy` | No re-encoding (fast, lossless) |458459### Audio Codecs460461| Codec | FFmpeg encoder | Extension | Use case |462|---|---|---|---|463| MP3 | `libmp3lame` | `.mp3` | Universal compatibility |464| AAC | `aac` | `.m4a`, `.aac` | Apple/mobile, streaming |465| Vorbis | `libvorbis` | `.ogg` | Open format, gaming |466| Opus | `libopus` | `.opus` | Best quality-per-bit, VoIP, WebRTC |467| FLAC | `flac` | `.flac` | Lossless compression |468| PCM | `pcm_s16le` | `.wav` | Uncompressed, editing |469| WMA | `wmav2` | `.wma` | Windows legacy |470471## 6) Performance Tips472473```bash474# Use hardware acceleration (macOS — VideoToolbox)475ffmpeg -i input.mp4 -c:v h264_videotoolbox -b:v 5M -c:a aac output.mp4476477# Use hardware acceleration (NVIDIA — NVENC)478ffmpeg -i input.mp4 -c:v h264_nvenc -preset fast -c:a aac output.mp4479480# Use hardware acceleration (Intel — QSV)481ffmpeg -i input.mp4 -c:v h264_qsv -preset faster -c:a aac output.mp4482483# Use multiple threads484ffmpeg -threads 0 -i input.mp4 -c:v libx264 -c:a aac output.mp4485486# Suppress banner for cleaner output487ffmpeg -hide_banner -i input.mp4 ...488```489490# Guidelines4914921. **Preserve original files by default.** Unless the user explicitly asks to overwrite (e.g. "覆盖", "replace", "in-place"), ALWAYS output to a new filename. Naming conventions:493 - Convert: `video.avi` → `video.mp4`494 - Compress: `video.mp4` → `video_compressed.mp4`495 - Trim: `video.mp4` → `video_clip.mp4`496 - Resize: `video.mp4` → `video_720p.mp4`497 - Audio extract: `video.mp4` → `video.mp3`498 - Audio convert: `audio.wav` → `audio.mp3`499 - Batch: output to a subdirectory (e.g. `compressed/`, `converted/`)5005012. **Use `-c copy` when possible** for fast, lossless operations (format remux, trimming at keyframes)5023. **Always quote file paths** that might contain spaces5034. **Use `-y` flag** to automatically overwrite output (only when user confirms)5045. **Use `-hide_banner`** to suppress version info for cleaner output5056. **Prefer two-pass encoding** for target file size5067. **Use `-movflags +faststart`** for MP4 files intended for web streaming5078. **Test on a sample** before running batch operations5089. **For PCM/raw audio**, always specify format (`-f`), sample rate (`-ar`), and channels (`-ac`)509510# Troubleshooting511512**"ffmpeg: not found"** — FFmpeg not installed or not on PATH. Install via `brew install ffmpeg` (macOS), `sudo apt-get install ffmpeg` (Linux), or `scoop install ffmpeg` / `choco install ffmpeg` (Windows).513514**"Unknown encoder"** — FFmpeg was compiled without that codec. Check available encoders: `ffmpeg -encoders | grep <codec>`. May need to reinstall with codec support (e.g., `brew install ffmpeg` includes most codecs by default).515516**"Invalid data found when processing input"** — File may be corrupted or format not recognized. Check with `ffprobe input.file`.517518**"Output file is empty"** — Likely a timestamp error in trimming. Verify timestamps are within the file's duration.519520**"height not divisible by 2"** — Use `-2` instead of `-1` in scale filter: `scale=1280:-2`.521522**"Avi/mp4 codec not compatible for -c copy"** — Source and target container have incompatible codecs. Re-encode instead of copying: remove `-c copy` and specify codecs explicitly.