guest @ doryx3d.github.io :$ ~
INTRO

Here you will find a cheat sheet of some useful commands for ffmpeg.
ffmpeg is a powerful CLI tool able to edit and convert countless video/audio/image formats, but at times its commands are hard to remember.
To help myself and others, I will collect and explain here the commands that I use most often to create animations and change file formats.
Please note that these are for Windows, but you can easly change paths and formats for Unix systems.
Open your terminal of choice and let's begin!

========================


guest @ doryx3d.github.io :$ ~
FRAMES

# Extract frames from a video.

R:\ffmpeg.exe -i input.mp4 -filter:v fps=fps=15/1 R:\frames\frame%04d.png

========================


guest @ doryx3d.github.io :$ ~
GIF CREATION

# Extract Color Palette from a video. This is needed for GIF creation and must be used before the next command.

R:\ffmpeg.exe -i input.mp4 -vf fps=15,scale=-1:-1:flags=lanczos,palettegen palette.png


# Generate GIF from a video and its Color Palette.

R:\ffmpeg.exe -i input.mp4 -i R:\palette.png -filter_complex "fps=15,scale=-1:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif


# How to use Dither control to change quality and size of the output GIF.

R:\ffmpeg.exe -i input.mp4 -i R:\palette.png -filter_complex "fps=15,scale=-1:-1:flags=lanczos[x];[x][1:v]paletteuse=dither=bayer:bayer_scale=2:diff_mode=rectangle" output.gif

R:\ffmpeg.exe -i input.mp4 -i R:\palette.png -filter_complex "fps=15,scale=-1:-1:flags=lanczos[x];[x][1:v]paletteuse=dither=sierra2_4a:diff_mode=rectangle" output.gif

========================


guest @ doryx3d.github.io :$ ~
VIDEO CONVERSION

# Change Container. This conversion change the container of the file, leaving the codecs untouched. This has a lot of limitations because every container supports only a limited quantity of codecs. Choose the output container accordingly. This kind of conversion should be almost instantaneous, limited by memory copy speed.

R:\ffmpeg -i input.mkv -vcodec copy -acodec copy output.mp4

R:\ffmpeg -i input.mkv -vcodec copy -acodec aac output.mp4


# Codec Conversion - Quality Target. This is an actual codec change. I will cover only the most basic instructions since this is the most vaste and versatile part of FFMPEG.

R:\ffmpeg.exe -i input.mp4 -c:v libx264 -crf 22 -preset fast -c:a aac -b:a 194k output.mp4


# Codec Conversion - Bitrate Target. You often need to target a specific file size, since bitrate(kbps) = file size(kb) / duration(seconds).

R:\ffmpeg.exe -i input.mp4 -c:v libx264 -b:v 2600k -preset fast -c:a aac -b:a 194k output.mp4

========================


guest @ doryx3d.github.io :$ ~
OTHER TIPS AND USEFUL COMMANDS

# Compare two video files. You can use this to highlight the differences between the source file and the compressed output.

R:\ffmpeg.exe -i source.mp4 -i encoded.mkv -filter_complex "blend=all_mode=difference,format=yuv420p" -c:v libx264 -crf 20 -c:a copy output.mp4


# Quick Tips about the terminal usage.
Remember that you can drag and drop ffmpeg.exe from its folder to the terminal. You can do the same with the input files so you don't have to write the path.
You can press TAB to auto-complete path and file names.
You can change the work folder with the command cd C:\work\path
You can press ↑ (up) to recall the previous command and edit it, so you don't have to rewrite it from start.
You can paste text in Windows Terminal with Right Mouse Click inside the window.
You can stop the current conversion with CTRL+C

========================


guest @ doryx3d.github.io :$ ~