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. This book uses powerful learning cues to make concepts clear and interesting to the reader.
The book can be used as an FFmpeg tutorial, a hack collection and a desk-side reference.
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. It is neatly designed and richly illustrated like any other traditionally published book. Check the excerpts document if you need to. (It does have unsightly watermarks, also applied using FOSS:-) And, in the spirit of open source, I have decided to make the ebook free in April 2021, one year after publication.
Format | Store | Price |
---|---|---|
Paperback (full colour) | Amazon | $26, £16, €24, ₹3,600 |
eBook (Kindle) (Print replica) | Amazon | $3, £3, €3 |
eBook (Nook) (Reflow EPUB) | BN.com | $4 |
eBook (all) (Reflow EPUB) | Apple Books, Kobo, Scribd... | $4 |
https://books2read.com/ffmpeg |
- 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.
Free PDF samples of several other books are listed in my newsletter blogpost. (Do not worry. You do not have to subscribe.)
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
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.
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
ffmpeg -y -i beto.mp4 -i fallon.mp4 \ -filter_complex \ "[0:v:0]fade=t=out:st=8:d=6:alpha=1[betofade]; [1:v:0][betofade]overlay[v]; [0:a:0]afade=t=out:st=8:d=6,channelsplit=channel_layout=mono[left]; [1:a:0]afade=t=in:st=8:d=6,channelsplit=channel_layout=mono[right]; [left][right]join=inputs=2:channel_layout=stereo[a]" \ -map '[v]' -map '[a]' \ -ac 1 -t 0:0:20 \ beto-fifo.mp4
- 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