FFMPEG Quick Hacks
::: by V. Subhash :::
An FFmpeg tutorial, hack collection and quick-reference
FFMPEG Quick Hacks can help you quickly learn to use the free command-line video-editing utility FFmpeg - cut, copy, record, edit, tag, convert, rotate, flip, resize, crop, combine, compose, blur, sharpen, smoothen, side-by-side split, PIP inset, fade in/out... Also learn to use subtitles, audio/image files and metadata with video.
The book is a novel attempt to bring this great software program to the masses. It uses powerful learning cues to make concepts clear and interesting to the reader. It is neatly designed and richly illustrated better than any other traditionally published book. Check the ebook or the excerpts PDF document (scroll down) to see what I mean.
This book was created entirely using Free and Open-Source Software (FOSS). And, by that, I do not just mean 'not exported from Microsoft word'. I have written, illustrated, formatted and designed the book using a variety of freedom-oriented software.
This book is being replaced with a better one and has become temporarily unavailable.
Notes
- Videos: 'FFMPEG Quick Hacks' book playlist
- Intro article: Cool FFmpeg Tips and Tricks (from 2016)
- Follow-up article: Open Source For You magazine September 2020 edition.
- Sample PDF: This sample is a low-resolution watermarked PDF but it is fully functional.
What you can expect from FFMPEG Quick Hacks
- A simple introduction to FFmpeg and related multimedia concepts - containers, streams, channels, maps, metadata...
- Learn to convert from one format to another - video-to-video, video-to-audio, video-to image, image-to-video, audio-to-video...
- Get ready to edit video - cut videos with and without re-encoding, append (concatenate) videos, resize videos, change contrast...
- Go bonkers with filters - rotate, flip, crop, side-by-side split, PIP inset, remove logo, blur, smoothen/sharpen, draw box, draw text, speed up, slow down, fade in/out...
- Go all in with audio - convert, change volume, mix channels, detect silence, display waveforms...
- Get subversive with subtitles - place them anywhere on the screen, use custom fonts and colors, specify languages, burn them into the video...
- Get mental with metadata - add MP3 tags including album art, set global and stream-specific metadata, remove metadata...
- Learn several useful tips that makes tough tasks easy
Overall, the book can be used as an FFmpeg tutorial, a hack collection and a desk-side reference.
About FFmpeg
You can read the article 'Cool FFmpeg Tips and Tricks' that I wrote for Open Source For You magazine some years ago for an easy introduction to FFmpeg. You can then buy my book for more.
Subscribe to my newsletter
The details are mentioned in my author newsletter blogpost. Full/sample PDF ebooks of several books are listed in it. You do not have to subscribe to download them. However, if you do subscribe, you get a FREE 200-puzzle combined ebook version of my World of Word Ladders books.
Videos for the code examples in the book
The following videos are available in this online video playlist.
-
All-in-one video demo at the end of the book
This video uses several FFmpeg techniques described in the book.
-
How to fade in fade out audio and video
# Create the fade-in-fade-out video ffmpeg -y \ -i Do-Aliens-Exist-We-Asked-a-NASA-Scientist.mp4 \ -i irs-tax-advice-for-alien-mates.mp4 \ -filter_complex \ "[0:v:0]trim=start=0:end=6, setpts=PTS-STARTPTS[v1]; [1:v:0]trim=start=3:end=9, setpts=PTS-STARTPTS[v2]; [0:v:0]trim=start=6:end=9, setpts=PTS-STARTPTS[v3]; [1:v:0]trim=start=0:end=3, setpts=PTS-STARTPTS[v4]; [v3]fade=t=out:d=3:alpha=1, setpts=PTS-STARTPTS[nasafade]; [v4]fade=t=in:d=3:alpha=1, setpts=PTS-STARTPTS[irsfade]; [nasafade][irsfade]overlay, setpts=PTS-STARTPTS[fading]; [v1][fading][v2]concat=n=3:v=1:a=0[v]" \ -map '[v]' \ aliens-r-us.m4v # Create the fade-in-fade-out audio ffmpeg -y \ -i Do-Aliens-Exist-We-Asked-a-NASA-Scientist.mp4 \ -i irs-tax-advice-for-alien-mates.mp4 \ -filter_complex \ "[0:a:0]atrim=start=0:end=6, asetpts=N/SAMPLE_RATE/TB[a1]; [1:a:0]atrim=start=3:end=9, asetpts=N/SAMPLE_RATE/TB[a2]; [0:a:0]atrim=start=6:end=9, asetpts=N/SAMPLE_RATE/TB[a3]; [1:a:0]atrim=start=0:end=3, asetpts=N/SAMPLE_RATE/TB[a4]; [a3]afade=t=out:d=3, asetpts=N/SAMPLE_RATE/TB[nasafade]; [a4]afade=t=in:d=3, asetpts=N/SAMPLE_RATE/TB[irsfade]; [nasafade][irsfade]amix[afading]; [a1][afading][a2]concat=n=3:v=0:a=1[a]" \ -map '[a]' \ aliens-r-us.m4a # Mixes the fade-in-fade-out video and audio ffmpeg -i aliens-r-us.m4v -i aliens-r-us.m4a aliens-r-us.mp4
The video for this example will be available soon.
-
How to convert a video into a GIF?
This is my software implementation of Chandler Bing's Run, Yasmine! Run! problem.ffmpeg -y -i baywatch.m4v \ -filter_complex "fps=7,scale=w=320:h=-1:flags=lanczos,split[v1][v2]; [v1]palettegen=stats_mode=diff[p]; [v2][p]paletteuse=dither=bayer:bayer_scale=4" \ run-yasmine-run-4.gif
This gives you good frame quality and file size, but the motion is not smooth.
If you change the filter to
paletteuse=dither=bayer:bayer_scale=0
, you get smoother motion but bigger file size and a dotted texture. - How to generate noise in audio and video
ffmpeg -y -i barbara.mp4 \ -filter_complex \ "[0:v:0]noise=alls=100:allf=a+t:enable='between(t,6,12)'[v]; [0:a:0]atrim=start=0:end=6, asetpts=N/SAMPLE_RATE/TB[fa]; anoisesrc=color=brown:d=6[ma]; [0:a:0]atrim=start=12:end=20, asetpts=N/SAMPLE_RATE/TB[la]; [fa][ma][la]concat=n=3:v=0:a=1[a]" \ -map "[v]" -map "[a]" \ -t 0:0:20 \ barb-intermission.mp4
- How to bleep part of a video
ffmpeg -y -i barbara.mp4 \ -filter_complex \ "[0:a:0]atrim=start=0:end=5, asetpts=N/SAMPLE_RATE/TB[a1]; sine=frequency=1000:duration=2[a2]; [0:a:0]atrim=start=7:end=10, asetpts=N/SAMPLE_RATE/TB[a3]; [a1][a2][a3]concat=n=3:v=0:a=1[a]" \ -map 0:v:0 -map '[a]' \ -t 0:0:10 \ barb-bleep.mp4; totem barb-bleep.mp
- How to add audio waveforms to video
ffmpeg -i ace-ventura-reversed.mp4 \ -filter_complex \ "[0:a:0]showfreqs=s=200x100:mode=bar[chartf]; [0:a:0]showvolume=w=200:h=40[chartv]; [0:a:0]showwaves=s=200x40[chartw]; [0:v:0][chartw]overlay=x=20:y=60[v1]; [v1][chartv]overlay=x=20:y=120[v2]; [v2][chartf]overlay=x=20:y=200[v]" \ -map '[v]' -map 0:a:0 \ ace-charts.mp4
- How to reverse audio and video
ffmpeg -y -i ace-ventura-reverse.mp4 \ -filter_complex \ "[0:v:0]reverse[v]; [0:a:0]areverse[a]" \ -map '[v]' -map '[a]' \ ace-ventura-reverse-reversed.mp4
- MP3-to-MP4 or audio-to-video conversion using audio power spectrum filter
ffmpeg -i ace-ventura-reverse-reversed.mp3 \ -filter_complex "showfreqs=s=640x320:mode=bar[v]" \ -map '[v]' -map 0:a:0 \ -vcodec mpeg4 -b:v 466k -r:v 24 \ ace-freq.mp4
- How to add echo to a video
ffmpeg -y -i barbara.mp4 \ -filter_complex \ "[0:a:0]atrim=start=0:end=5, asetpts=N/SR/TB[a1]; [0:a:0]atrim=start=6:end=12,, asetpts=N/SR/TB, aecho=0.8:0.9:1000:0.3[a2]; [0:a:0]atrim=start=13:end=16, asetpts=N/SR/TB[a3]; [a1][a2][a3]concat=n=3:v=0:a=1[a]" \ -map 0:v:0 -map '[a]' \ -t 0:0:16 \ barb-echo.mp4
- Blurring a portion of a video
ffmpeg -y -i fallon.mp4 \ -filter_complex \ "[0:v]crop=220:220:340:20[cropped]; [cropped]boxblur=6:6[blurred]; [0:v][blurred]overlay=340:20[overlaid]" \ -map '[overlaid]' -map 0:a -acodec copy \ -ss 0:0:10 -t 0:0:10 \ fallonbb.mp4
- Write text on video
ffmpeg -y -i tl.mp4 \ -filter:v \ "drawtext=x=30:y=100:fontcolor=yellow:alpha=0.6:shadowx=7:shadowy=7:text='THEY LIVE:fontsize=112:fontfile=FingerPaint.ttf" \ tlt.mp4
- Render transparent GIF on video with infinite looping
ffmpeg -y \ -i fallon.mp4 \ -ignore_loop 0 -i world.gif \ -shortest \ -filter_complex \ "[0:v:0]overlay=300:60[v]" \ -map '[v]' -map 0:a:0 -t 0:0:10 \ -vcodec libx264 -acodec copy \ beto-animated.mp4
- Replace green-screen background on another video
ffmpeg -y -i fallon.mp4 -i van-damme.mp4 \ -filter_complex \ "[0:v:0]pad=1920:360[frame]; [frame][1:v:0]overlay=640:0[v1]; [1:v]colorkey=0x008000:0.2:0.2[v2]; [0:v:0][v2]overlay[v3]; [v1][v3]overlay=1280:0[v4]; [v4]scale=960:180[v]; [0:a:0][1:a:0]amerge=inputs=2[a]" \ -map "[v]" -map '[a]' \ -ac 2 -t 0:0:20 \ green-screen-eliminated.mp4
- Fade into another video using a transition effect
ffmpeg -y -i beto.mp4 -i fallon.mp4 \ -filter_complex \ "[0:v:0][1:v:0]xfade=hrslice:offset=10:duration=6[v]; [0:a:0]atrim=start=0:end=16, asetpts=N/SR/TB[a1]; [a1][1:a:0]acrossfade=duration=6[a]" \ -c:v libx264 -crf 31 -preset slow -pix_fmt yuv420p \ -c:a libmp3lame \ -map '[v]' -map '[a]' \ beto-fallon-slice.mp4
-
Slow down a video
ffmpeg -y -i Laurie-Lennon-Original.mp4 \ -filter_complex \ "[0:v]setpts=PTS*1.1228[v]; [0:a]atempo=0.8906[a]" \ -map '[v]' -map '[a]' \ Laurie-Lennon-Slow.mp4
-
Subtitle a video
ffmpeg -y -i duffy.mp4 \ -filter_complex "subtitles=duffy.ass" \ -c:a copy \ -t 0:1:0 \ duffys.mp4
In the subtitle file (SSA), I used the following style for using my own custom font and other subtitle display settings.
Style: Default,Florentia,30,&H2200CCCC,&H000000FF,&H220000EE,&HAA00CCCC,-1,-1,0,0,100,100,0,30.00,3,2,3,1,20,20,40,1
Acknowledgements & Thanks
- "Rowdy" Roddy Piper/John Carpenter (They Live)
- Amazing Lucas, Alaska Granny, Barbara 2.0, Sara Gonzales Unfiltered, Joking.com,
- Laurie Lennon
- Pamela Anderson/Yasmine Bleeth (Baywatch)
- Jack Benny (The Jack Benny Program)
- Jimmy Fallon (Tonight Show)
- 'Beto' O'Rourke
- Jean-Claude Van Damme