Skip to content

ffmpeg

October 27, 2023
January 13, 2015

A complete, cross-platform solution to record, convert and stream audio and video.

FFmpeg - Wikiwand
FFmpeg

libavcodec - What is ffmpeg, avcodec, x264? - Stack Overflow

ffmpeg [global options] [[infile options][-i infile]]... {[outfile options] outfile}...
ParameterDescription
-c:v, -c:aSet video/audio codec
-b:v, -b:aSet video/audio bitrates
-c codece.g. -c copy
-vn, -anSkip video/audio
-t durationSpecify duration, preempts -to
-to positionSpecify stop position
-ss positionSpecify stop position (before -i)
-frames:v framecountSpecify frame count limit
-filter:v, -vfVideo filters
-filter:a, -afAudio filters

screenshot

# capture 10s of frames starting from 5 minute mark
ffmpeg -ss 05:00 -to 05:10 -i <input> screenshot%05d.png

screen capture

Capture/Desktop – FFmpeg
FFmpeg Devices Documentation

# default encoding is yuv444 at H.264 Main profile, which is not compatible with Da Vinci Resolve

# Windows
ffmpeg -f gdigrab -framerate 30 -i title=App -pix_fmt yuv420p -vcodec h264 -profile main out.mp4

# you can use `Capture` UI to select the rect to get the offset and video size
ffmpeg -f gdigrab -framerate 30 -offset_x 0 -offset_y 0 -video_size 1920x1080 -show_region 1 -i desktop -pix_fmt yuv420p -vcodec h264 -profile main out.mp4

trim clip

TODO: add tool to do diff of hh:mm:ss[.xxx] format time

# keyframe seek, time is reset
ffmpeg -ss <second/hh:mm:ss[.xxx]> -i <infile> -to <second/hh:mm:ss[.xxx]> -c copy <outfile>

transcode clips

# list codecs
ffmpeg -formats -E
# list private options
ffmpeg -h encoder=<encoder>

# use `h264_nvenc` for NVIDIA card.
ffmpeg \
  -i 'Top Gear - [06x01] - 2005.05.22 [m00tv].avi' \
  -c:v h264_nvenc -b:v 750k -g 240 \
  -c:a aac -vbr:a on -b:a 128k \
  'Top Gear - [06x01] - 2005.05.22 [m00tv].mp4'

# SD encoding
ffmpeg \
  -i 'Top Gear - [06x01] - 2005.05.22 [m00tv].avi' \
  -c:v libvpx-vp9 -b:v 750k -quality good -speed 4 -crf 23 -g 240 -threads 12 \
  -c:a libopus -vbr:a on \
  'Top Gear - [06x01] - 2005.05.22 [m00tv].webm'

HWAccelIntro – FFmpeg

Media | Google Developers VP9 encoder
Hardware/VAAPI – FFmpeg
Simple hardware encoding for VP9 with semi-recent Intel processors and FFmpeg · Abel Stern
mp4 - Best settings for FFMpeg with NVENC - Super User

merge clips

ffmpeg -i VTS_01_2.VOB -ss 463 -c copy -vframes 325 2-manuchoisit.vob
ffmpeg -i VTS_01_2.VOB -ss 353 -t 16 -c copy 3-manutombe.vob

# use `-vn`/`-an` to skip encoding
# `-b:a 192k` may be smaller than `aq`
# `crf` controls the video quality. 18-28 is acceptable range, default is 23.
ffmpeg -analyzeduration 200M -probesize 150M \
  -i concat:"2-manuchoisit.vob|3-manutombe.vob" \
  -vcodec libx264  -preset veryslow -crf 23 \
  -acodec aac -aq 100 -ac 2 -vol 2048 1990-Manu_Redpants.mp4

color format conversion

ffmpeg -pix_fmt yuv420p -s 352x288 -i in.yuv -pix_fmt nv12 out.yuv

Overlay text

.\ffmpeg  -i test2.wmv \
  -vf "drawtext=fontfile=C\\:/Windows/fonts/consola.ttf:text=%{n}:fontsize=72:r=60:x=(w-tw)/2: y=h-(2*lh):fontcolor=white:box=1:boxcolor=0x00000099" \
  test2.mp4

GIF generation

# generate 1 FPS for the first minute
ffmpeg -i input.mp4 -vf "scale=320:-1,fps=1" -to 60 output.gif

# using per frame palettes
ffmpeg -i input.mp4 -vf "scale=320:-1:flags=lanczos,fps=1,palettegen" -to 60  -y /tmp/palette.png
ffmpeg -i input.mp4 -i /tmp/palette.png -to 60 -lavfi "scale=320:-1:flags=lanczos,fps=1[x];[x][1:v]paletteuse=dither=sierra2_4a" output.gif

# same with Imagemagic
ffmpeg -i input.mp4 -vf scale=320:-1,fps=1 -to 60 -f image2pipe -vcodec ppm - | convert -delay 100 -loop 0 - output.gif
# with optimize (~15% reduction)
ffmpeg -i input.mp4 -vf scale=320:-1,fps=1 -to 60 -f image2pipe -vcodec ppm - | convert - gif:- | convert -layers Optimize  -delay 100 -loop 0 - output.gif

Using per frame palettes:
High quality GIF with FFmpeg
lukechilds/gifgen: Simple high quality GIF encoding
How do I convert a video to GIF using ffmpeg, with reasonable quality? - Super User

Reference

Documentation
ffmpeg Documentation

FFmpeg - ArchWiki
FFmpeg wiki
Seeking – FFmpeg

ffmpeg audio/video manipulation
19 FFmpeg Commands for Your Needs (2023 Edition)
ffmpeg | Video Encoding & Streaming Technologies
using ffmpeg to extract audio from video files
A quick guide to using FFmpeg to convert media files | Opensource.com

Filters

FFmpeg Filters Documentation
FilteringGuide – FFmpeg

Libav/avconv kerfuffle

Libav/avconv are dead as of 2022

Libav - Wikiwand
Libav is a fork of FFmpeg in 2011 with more transparent governance.
But the maintainers failed to maintain their original intention and rejected to merge changes from FFmpeg.
They managed to keep their popularity as Debian/Ubuntu used it as default and claiming FFMpeg that is obsolete. That changed in 2015 and momentum to Libav is lost.

The FFmpeg/Libav situation

ubuntu - What are the differences and similarities between ffmpeg, libav, and avconv? - Stack Overflow