Tutorial on FFMPEG (Draft)

Bensuperpc February 16, 2025 #Features #FFMPEG #AV1 #x265 #x264 #SVT-AV1 #Rav1e #Audio #Video #Image #Metadata

About FFMPEG

Convert video to AV1 CRF (SVT-AV1)

OptiondefaultExampleDescription
-vf--vf scale=1920:-1Downscale the video to 1080p
-g?-g 60Set the keyframe interval of x frames, lower value increase quality but increase file size
-pix_fmtDepends on source-pix_fmt yuv420pSet the pixel format, yuv420p for 8-bit, yuv420p10le for 10-bit ect...
-crf--Constant Rate Factor, options depend on the encoder
-preset--Set the encoding speed, options depend on the encoder
-tune--Set the encoding tune, options depend on the encoder
-c:v--c:v libx265Set the video codec, options depend on installed codecs
-c:a--c:a copySet the audio codec, options depend on installed codecs
-c:s--c:s copySet the subtitle codec, options depend on installed codecs
-map--map 0Map the input stream to the output
-map_metadata--map_metadata 0Map the metadata to the output
-map_chapters--map_chapters 0Map the chapters to the output
-fps_modeauto-fps_mode passthroughSet the frame rate mode, passthrough, cfr, vfr, auto
-loglevel--loglevel errorSet the log level, error, warning, info, verbose, debug, trace
-hide_banner--hide_bannerHide the banner
-y--yOverwrite output files without asking

This table shows the most common options for SVT-AV1, like the preset, crf, and svtav1-params.

For svtav1-params each option is separated by a colon :, option and value are separated by =, like tune=0:enable-qm=1:qm-min=0.

OptiondefaultMin/MaxExampleDescription
-preset4-preset 40-120 for slowest, 12 for fastest encoding
-crf18-crf 300-63Constant Rate Factor, lower value increase quality
-svtav1-params---svtav1-params tune=0:enable-qm=1SVT-AV1 specific options
tune10-1-svtav1-params tune=00 for subjective quality, 1 for objective quality (PSNR)
enable-qm00-1-svtav1-params enable-qm=1Enable quantization matrices
qm-min80-15-svtav1-params qm-min=0Minimum quantization matrix
qm-max150-15-svtav1-params qm-max=10Maximum quantization matrix
aq-mode20-2-svtav1-params aq-mode=2Adaptive quantization mode
enable-overlays00-1-svtav1-params enable-overlays=1Enable overlays
film-grain00-12-svtav1-params film-grain=8Add film grain to the video

More information about the options can be found in the SVT-AV1 documentation

Example of encoding CRF for very high quality (Maybe little overkill, preset 4 and crf 20 is a good start for 1080p).

ffmpeg -i input.mkv -c:v libsvtav1 -preset 1 -crf 14 -svtav1-params tune=0:enable-qm=1:qm-min=0:qm-max=8 -c:a copy -c:s copy -map 0 -map_metadata 0 -map_chapters 0 output.mkv

With AV1AN, it usefull if you have move than 16 threads, SVT-AV1 is not well optimized for over 16 threads, AV11AN encode the video in parallel per scene.

av1an -i input.mkv --encoder svt-av1 --video-params "--rc 0 --crf 16 --preset 1 --tune 0 --enable-qm=1 --qm-min=0 --qm-max=8" --audio-params "-c:a copy" -o temp_output.mkv

Copy metadata and chapters:

ffmpeg -i temp_output.mkv -i input.mkv -map 0 -map_metadata 1 -map_chapters 1 -c copy output.mkv

Optional for AV1AN:

Convert video to AV1 CRF(AOM)

ffmpeg -i input.mkv -c:v libaom-av1 -crf 18 -cpu-used 3 -row-mt 1 -c:a copy -c:s copy -map 0 -map_metadata 0 -map_chapters 0 output.mkv

Convert video to AV1 CRF (Rav1e)

ffmpeg -i input.mkv -y -c:v librav1e -crf 18 -speed 3 -c:a copy -c:s copy -map 0 -map_metadata 0 -map_chapters 0 output.mkv

Convert video to h265 CRF (x265)

ffmpeg -i input.mkv -y -c:v libx265 -crf 18 -preset slower -c:a copy -c:s copy -map 0 -map_metadata 0 -map_chapters 0 output.mkv

Convert video to h265 ABR 2 pass and scale to 720p (x265)

ffmpeg -i input.mkv -y -c:v libx265 -preset slow -vf scale=1280:-1 -b:v 2000k -minrate 500k -maxrate 6000k -bufsize 12000k -pass 1 -an -f null /dev/null && ffmpeg -i input.mkv -preset slow -vf scale=1280:-1 -c:v libx265 -b:v 2000k -minrate 500k -maxrate 6000k -bufsize 12000k -pass 2 -c:a copy -c:s copy -map 0 -map_metadata 0 -map_chapters 0 output.mkv

Convert video to h264 CRF (x264)

ffmpeg -i input.mkv -c:v libx264 -crf 26 -preset slow -c:a copy -c:s copy -map 0 -map_metadata 0 -map_chapters 0 output.mkv

Get SSIM

SSIM Y: For luma (Y) channel, 0-1, 1 is perfect match SSIM U: For chrominance (U) channel, 0-1, 1 is perfect match SSIM V: For chrominance (V) channel, 0-1, 1 is perfect match SSIM All: Average of YUV, 0-1, 1 is perfect match

ffmpeg -i output.mkv -i input.mkv -lavfi ssim -f null –

Get PSNR

PSNR Y: For luma (Y) channel, in dB higher is better PSNR U: For chrominance (U) channel, in dB higher is better PSNR V: For chrominance (V) channel, in dB higher is better PSNR All: Average of YUV, in dB higher is better

ffmpeg -i output.mkv -i input.mkv -lavfi psnr -f null –

Get VMAF

VMAF score, higher is better

ffmpeg -i output.mkv -i input.mkv -lavfi libvmaf -f null –

Output into a json file

ffmpeg -i video1.mp4 -i video2.mp4 -lavfi libvmaf="log_path=vmaf.json:log_fmt=json" -f null -

Cut video without re-encoding

Extract from 125s to 200s

ffmpeg -i input.mp4 -ss 125 -t 75 -copyts -map_metadata 0 -vcodec copy -acodec copy out.mkv

Extract from 125s to 150s

ffmpeg -i input.mp4 -ss 125 -to 150 -c copy -copyts -map_metadata 0 -vcodec copy -acodec copy out.mkv

Import watermark

Import watermark at 10:10 from the top left corner

ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=10:10" -c:a copy -c:s copy -map 0 -map_metadata 0 -map_chapters 0 output.mp4

Change video speed

Speed up the video by 2x

ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" -af "atempo=2.0" -map_metadata 0 output.mp4

Change the video frame rate to 30fps (2x slower if the original is 60fps)

ffmpeg -i input.mp4 -filter:v fps=fps=30 -map_metadata 0 output.mp4

Convert video to gif

ffmpeg -i input.mp4 -vf "fps=24,scale=480:-1:flags=lanczos" -c:v gif -map_metadata 0 output.gif

Add vintage look effect to video and play it with ffplay

ffplay -i input.mp4 -vf "curves=vintage,noise=alls=30:allf=t+u,hue=s=0.7,eq=contrast=0.85:brightness=-0.1:saturation=0.7,gblur=sigma=1.5,colorbalance=rm=0.2:gm=0.1:bm=-0.2,vignette"

Image commands examples

Convert images png to webp (lossless)

find . -name "*.png" | parallel -eta cwebp -metadata all -lossless -exact -z 9 "{}" -o "{.}.webp" && find . -name "*.png" -exec sh -c 'touch -r "${0%.*}.png" "${0%.*}.webp"' "{}" ';'

Get difference between two images

compare -metric AE input1.png input2.webp null:

Or with ImageMagick

magick input1.png ppm:- | magick input2.webp ppm:- | diff -q - <(magick input2.webp ppm:-)

 Audio commands examples

Generate audio spectrogram with sox

You can generate a spectrogram with sox, for example with a flac file to detect "fake" flac files.

sox input_audio.flac -n spectrogram -o output_spectrogram.png

Add -t flac if the input file is not recognized as flac.

Increase audio volume without re-encoding

ffmpeg -i input.mp4 -af "volume=2.0" -c:v copy -c:a copy -c:s copy -map 0 -map_metadata 0 -map_chapters 0 output.mp4

Convert audio

Audio libMax bitrateExtension
libmp3lame320kbpsmp3
aac250kbpsm4a
libopus250kbpsopus
flaclosslessflac

Convert audio to mp3

ffmpeg -i input.flac -c:a libmp3lame -b:a 320k output.mp3

Convert audio to lossy or "fake" flac

ffmpeg -i output.mp3 output.flac

Remove noise from audio

ffmpeg -i input.mp3 -af "highpass=f=200, lowpass=f=3000" output.mp3

Metadata commands examples

Get metadata from audio or video file

ffprobe -v quiet -print_format json -show_format -show_streams input.mp4

Youtube-dlp

Download audio from youtube video without re-encoding:

yt-dlp -f bestaudio --extract-audio --embed-thumbnail --embed-metadata --embed-chapters --output "%(title)s.%(ext)s" "https://www.youtube.com/watch?v=video_playlist_id"

Download video from youtube video without re-encoding:

yt-dlp -f "bestvideo+(251/mergeall[format_id~=251-])" --audio-multistreams --sub-langs "all,-live_chat" --embed-chapters --embed-metadata --merge-output-format mkv --output "%(title)s.%(ext)s" "https://www.youtube.com/watch?v=video_id"

License

MIT