1001Ferramentas
🎞️Generators

ffmpeg Command Builder

Build common ffmpeg commands: convert format, compress, trim, extract audio, GIF and resize.


  

ffmpeg in depth: codecs, filters, CRF, presets and hardware acceleration

ffmpeg β€” Fast Forward MPEG β€” was started by Fabrice Bellard in December 2000 and grew into the universal swiss army knife of audio and video. It is both a C library (libavcodec, libavformat, libavfilter, libswscale) and a CLI, and behind essentially every browser, every video editor, every streaming platform and every desktop media player is a layer of ffmpeg code processing pixels and samples. The CLI follows a single pattern: ffmpeg -i input.ext [options] output.ext. Everything between input and output β€” codec choice, bitrate, filters, container, metadata β€” is expressed through flags.

Containers vs codecs: what each piece actually means

A common confusion: MP4 is not a codec. A container (MP4, MKV, AVI, MOV, WebM, FLV, TS) is an envelope that holds one or more streams. A codec (H.264, H.265/HEVC, AV1, VP9, MPEG-2 for video; AAC, MP3, Opus, FLAC, AC3 for audio) is the algorithm that compresses each stream. The same MP4 file can hold H.264+AAC, H.265+AAC or AV1+Opus, and ffmpeg lets you mix any compatible combination. -c:v copy -c:a copy tells ffmpeg to remux without re-encoding, finishing in seconds even on a 10 GB source.

Common operations

# Cut a segment without re-encoding (fast, keyframe-aligned)
ffmpeg -ss 00:01:00 -to 00:02:00 -i in.mp4 -c copy clip.mp4

# Re-encode to H.264 with CRF
ffmpeg -i in.mp4 -c:v libx264 -preset medium -crf 23 \
       -c:a aac -b:a 128k out.mp4

# Resize to 720p
ffmpeg -i in.mp4 -vf scale=1280:720 -c:a copy out.mp4

# Extract audio without re-encoding
ffmpeg -i in.mp4 -vn -acodec copy out.aac

# Create a 320 px wide GIF at 15 fps
ffmpeg -i in.mp4 -vf "fps=15,scale=320:-1:flags=lanczos" out.gif

# Concatenate (same codec/container)
printf "file 'a.mp4'\nfile 'b.mp4'\n" > list.txt
ffmpeg -f concat -safe 0 -i list.txt -c copy joined.mp4

# Add a watermark in the top-left corner
ffmpeg -i in.mp4 -i logo.png -filter_complex \
       "overlay=10:10" -c:a copy out.mp4

# Encode to WebM (VP9 + Opus) for the web
ffmpeg -i in.mp4 -c:v libvpx-vp9 -crf 32 -b:v 0 \
       -c:a libopus -b:a 96k out.webm

CRF, presets and how to size your output

CRF (Constant Rate Factor) is the quality knob for libx264 and libx265. Lower is better:

  • 0 β€” mathematically lossless (huge files).
  • 18 β€” visually lossless: indistinguishable from the source on a 4K TV.
  • 23 β€” H.264 default; web-quality 1080p.
  • 28 β€” H.265 default; equivalent file size to H.264 CRF 23 at the same quality.

Presets trade encoding speed against compression efficiency, from ultrafast to veryslow. medium is the default; slow gives ~10% smaller files for double the CPU time; veryslow is the limit before diminishing returns. CRF and preset are independent: -crf 23 -preset slow produces a smaller file than -crf 23 -preset fast at the same visual quality.

Hardware acceleration: NVENC, VAAPI, VideoToolbox, AMF

Software encoding pegs the CPU; hardware encoders push the work to dedicated silicon at the cost of compression efficiency (~10-20% larger files at the same visual quality).

  • NVIDIA NVENC β€” -c:v h264_nvenc or -c:v hevc_nvenc. 5-10x faster than libx264.
  • Intel/AMD VAAPI (Linux) β€” -vaapi_device /dev/dri/renderD128 -vf 'format=nv12,hwupload' -c:v h264_vaapi.
  • Apple VideoToolbox (macOS) β€” -c:v h264_videotoolbox or hevc_videotoolbox.
  • AMD AMF (Windows) β€” -c:v h264_amf.

Filters: -vf, -af and the filtergraph syntax

Filters are mini-programs applied per frame. -vf takes video filters, -af takes audio filters. Chain them with commas, run parallel branches with semicolons inside -filter_complex. Examples: -vf "scale=1280:720,fps=30" (resize then resample); -af "loudnorm=I=-16:TP=-1.5" (broadcast-loudness normalisation); -vf "setpts=2*PTS" (half-speed slow motion).

Pitfalls

Argument order matters. -ss before -i is fast seek (jumps to the nearest keyframe, may be imprecise); -ss after -i is accurate seek (slow on long inputs). For frame-accurate cuts, use accurate seek and re-encode; for fast trims, use fast seek and -c copy.

Licensing. ffmpeg ships under LGPL by default; enabling --enable-gpl at build time (for libx264, libx265, libxvid) makes the binary GPL. Static builds you download from popular mirrors are usually GPL.

Audio drift. Some camera files have variable frame rate audio. Add -fflags +genpts -async 1 or re-encode the audio to PCM to resynchronise.

FAQ

GIF or MP4/WebM for the web? Almost always MP4 or WebM. A 5-second 720p GIF can weigh 20 MB; the same clip as MP4/H.264 is ~600 KB and autoplays muted on every browser.

Can ffmpeg do slow motion? Yes β€” setpts=2*PTS doubles the duration (half-speed); pair with atempo=0.5 on audio (chainable up to 0.5x per filter). For smooth 60 fps slow-mo, interpolate with minterpolate.

Is GPU encoding worth it? For batch H.264/HEVC on a GPU-equipped machine, yes β€” 5-10x throughput at slightly larger files. For maximum quality at a fixed bitrate, libx264/libx265 still win.

How do I stream to RTMP/HLS? -f flv rtmp://server/app/key for RTMP, -f hls -hls_time 4 -hls_list_size 6 out.m3u8 for HLS. Add -c:v copy if the source codec is already H.264.

Why is my output green/pink? Pixel format mismatch β€” many encoders require yuv420p. Add -pix_fmt yuv420p to force the format.

Related Tools