Skip to main content

ffmpeg-recipes

Tools

Snippets

Audio - increase volume: AudioVolume – FFmpeg Audio Volume Manipulation

ffmpeg -i VID_20211212_130236045.mp4  -c:v copy -filter:a loudnorm VID_20211212_130236045_vol_50.mp4

Add sound wave on top of video (under construction):

ffmpeg -i in.mp4 -filter_complex "[0:a]showwaves=s=1920x300:mode=line:colors=red|yellow[sw];[0][sw]overlay=(W-w)/2:(H-h)/2:format=auto,format=yuv420p[v]" -map "[v]" -map 0:a -movflags +faststart output.mp4

Reduce frame rate:

ffmpeg -i one.mkv -movflags +faststart -filter:v fps=24 two.mp4

Make a clip

ffmpeg -i i.mp4 -ss 00:01:00 -t 00:02:00 -c copy o.mp4

Group photos into video

ffmpeg -framerate 10 -pattern_type glob -i '*.jpg' -c:v libx264 o.mp4

Get a screenshot from a video.

ffmpeg -i o.mp4 -r 1 -q:v 2 -f image2 img-3%d.jpeg

Concatenate videos

file 'video1.mp4'
file 'video2.mp4'
file 'videoN.mp4'
ffmpeg -f concat -i list.txt -c copy o.mp4

Remove audio from video

ffmpeg -i i.mp4 -c:v copy -an o.mp4

Extract audio from video

ffmpeg -i i.mp4 -vn 0.wav

Crop video

ffmpeg -i i.mp4 -filter:v "crop=w:h:x:y" o.mp4

Stack videos horizontaly

ffmpeg -i i0.mp4 -i i1.mp4 -filter_complex hstack=inputs=2 o.mp4

Vertical

ffmpeg -i i0.mp4 -i i1.mp4 -filter_complex vstack=inputs=2 o.mp4

Grid 2x2

ffmpeg \ -i i0.mp4 -i i1.mp4 -i i2.mp4 -i i3.mp4 \ -filter_complex \ "[0:v][1:v]hstack=inputs=2[top]; \ [2:v][3:v]hstack=inputs=2[bottom]; \ [top][bottom]vstack=inputs=2[v]" \ -map "[v]" \ o.mp4

Grid 3x2

ffmpeg \ -i i0.mp4 -i i1.mp4 \ -i i2.mp4 -i i3.mp4 \ -i i4.mp4 -i i5.mp4 \ -filter_complex \ "[0:v][1:v][2:v]hstack=inputs=3[top];\ [3:v][4:v][5:v]hstack=inputs=3[bottom];\ [top][bottom]vstack=inputs=2[v]" \ -map "[v]" \ o.mp4

Rotate (re-encode):

ffmpeg -i i.mp4 -vf "transpose=0" o.mp4

How To Rotate Videos Using FFMpeg From Commandline

transpose=...

  • 0 - Rotate by 90 degrees counter-clockwise and flip vertically. This is the default.
  • 1 - Rotate by 90 degrees clockwise.
  • 2 - Rotate by 90 degrees counter-clockwise.
  • 3 - Rotate by 90 degrees clockwise and flip vertically.

Rotate 180 dec:

ffmpeg -i input.mp4 -vf "transpose=2,transpose=2" output.mp4

Rotate (metadata only):

ffmpeg -i inp.mp4  -c copy -metadata:s:v:0 rotate=180 output.mp4

Audio filter loudnorm

ffmpeg -i input.mp3 -af loudnorm=I=-16:LRA=11:TP=-1.5 output.mp3
loudnorm
Indicates the name of the normalization filter

I, i
Indicates the integrated loudness (-70 to -5.0 with default -24.0)

LRA, lra
Indicates the loudness range (1.0 to 20.0 with default 7.0)

TP, tp
Indicates the max true peak (-9.0 to 0.0 with default -2.0)

See also: loudnorm

Loudness Normalization. The algorithm goes like this: Measure the integrated loudness of the source file, calculate an appropriate offset gain, and then apply makeup gain. It’s a pretty simple algorithm, but what about when there’s nowhere near enough headroom for a simple upwards linear gain adjustment? And how should we handle the loudness normalization of livestreams?

FFmpeg: convert for android

ffmpeg -i <input> -c:v libx264 -c:a aac -movflags +faststart output.mp4
ffmpeg -i <input> -s 480x320 -vcodec mpeg4 -acodec aac -strict -2 -ac 1 -ar 16000 -r 13 -ab 32000 -aspect 3:2 <output>
ffmpeg -y -i input_file.mp4 -s 352x288 -b:v 384k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8 -subq 6 -trellis 0 -refs 5 -bf 0 -coder 0 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10 -qmax 51 -qdiff 4 -c:a aac -ac 1 -ar 16000 -r 13 -ab 32000 -aspect 3:2 -strict experimental output_file.3gp
ffmpeg -i <INPUT_VIDEO> -c:v libx264 -profile:v baseline -c:a libvo_aacenc -ar 44100 -ac 2 -b:a 128k -movflags faststart output.mp4

Powershell: Convert files in the current folder for mobile upload

$files = Get-ChildItem
New-Item -Name "out" -ItemType "directory"
foreach($file in $files) {
$file.Extension
if ($file.Extension -cin ('.webm', '.mp4', '.mkv')) {
$fn = '"out/' + $file.BaseName + '.mp4'
ffmpeg -i "$file" -c:v libx264 -c:a aac -movflags +faststart $fn
}
}

Webcam Streaming

Stream camera video and audio with FFmpeg

ffmpeg -list_devices true -f dshow -i dummy

"HD Pro Webcam C920" "Microphone (TKGOU PnP USB Microphone)"

ffmpeg -f dshow -i video="HD Pro Webcam C920":audio="Microphone (TKGOU PnP USB Microphone)" -profile:v high -pix_fmt yuvj420p -level:v 4.1 -preset ultrafast -tune zerolatency -vcodec libx264 -r 10 -b:v 512k -s 640x360 -acodec aac -ac 2 -ab 32k -ar 44100 -f mpegts -flush_packets 0 udp://192.168.1.101:5666?pkt_size=1316

VLC -> Open Network Stream: udp://@192.168.1.101:5666

GoPRO Camera

Reddit Thread: No longer free: Windows 10 HEVC Video Extensions from Device Manufacturer : Windows10 workaround

start ms-windows-store://pdp/?ProductId=9n4wgh0z6vhq

Downscale 4K video example

ffmpeg -i input.MP4 -c:v libx264 -crf 19 -preset slow -c:a aac -b:a 192k -ac 2 -vf scale=1920:1080 output_4k_to_1080p_converted.MP4

Convert h.264 to h.265 (no change in resolution)

ffmpeg -i input.mp4 -c:v libx265 -vtag hvc1 -vf scale=1920:1080 -crf 20 -c:a copy output.mp4

crf:

  • -crf 0 high-quality, low compression, large file
  • -crf 23 default
  • -crf 51 low-quality, high compression, small file

GoPro Tools and Code

Youtube

Due to the encoding ladders that Youtube and other streaming sites utilise to re-encode uploaded videos in different qualities, higher resolution videos receive a higher bitrate. It's possible to use upscaling to exploit this for higher peak viewing quality.

The following will use nearest neighbor scaling to upscale a video by 2x in each direction for 4 times the total resolution and minimal scaling artifacts:

ffmpeg -i input.mkv -vf scale=iw*2:ih*2:flags=neighbor -c:v libx264 -preset slow -crf 18 output.mkv

To upscale by 4x or 8x, use scale=iw4:ih4:flags=neighbor and scale=iw8:ih8:flags=neighbor respectively. This can be useful for low-resolution material such as VHS captures, DVD video, and gameplay capture of old video games that would otherwise suffer from excessive loss of detail if uploaded in 480p or lower resolution.

Tools / Other