[@messageCenter]

FFMPEG Examples and Notes

Everything I learnedUpdated 3 years ago

Notes:

  • To Watch your Video Immediately Use: ffplay
  • To Save your Video to a File: ffmpeg -i INPUT.mp4 OUTPUT.mp4
  • To Get Video Info: ffprobe INPUT.mp4
  • Is -vf the same as filter_complex ?

Params:

  • -ss 13:21SKIP TO
  • -t 2:11DURATION
  • -loglevel panicDON"T SHOW ANY PRINTOUTS

Ideas on How to use these:

  • Trivia: Ask Questions, then count down Timer, then reveal
  • Guess the Movie: Show Video Blurred, while giving Hints
  • HELP me think of others.

#1- Crop Video Between Times:

ffmpeg -y -ss 00:10:14.6 -to 00:10:39.1 -i "Family Guy S19E10.mkv" -vf scale=1920:1080,setsar=1:1 ~temp1.mp4 ffmpeg1.mp4

#2- DrawText with a Font, Color, Position:

ffmpeg -y \
-i 3DoorsDownKryptonite.mp4 \
-filter_complex "color=blue:s=480x360,loop=-1:size=2,drawtext='fontfile=Simpsons.otf:font=sans-serif: fontcolor=yellow:fontsize=50:x=20:y=20:text=Simpsons Meme \\\\\\#1'" -t 5 \
ffmpeg2.mp4

#3- Show Date + Time:

ffmpeg -y \
-i ABBADancingQueen.mp4 \
-f lavfi -t 0.1 -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-filter_complex "color=blue:s=480x360,loop=-1:size=2,drawtext='font=sans-serif:fontcolor=red:fontsize=30:x=20:y=20:text=%{localtime \: \%Y / \%m / \%d \%H \\\\\\: \%M \\\\\\: \%S}'" -t 5 \
ffmpeg3.mp4

#4- Bouncey Text:

ffmpeg -y \
-i AllenShermanCampGrenada.mp4 \
-f lavfi -t 0.1 -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-filter_complex "color=blue:s=480x360,loop=-1:size=2,drawtext='font=sans-serif:fontcolor=yellow@1.0:fontsize=35:x=(sin(t)+1)/2*(W-tw):y=(cos(t/0.625)+1)/2*(H-th):text=Bouncey Text'" -t 10 \
ffmpeg4.mp4

#5- Colour Changing Text:

ffmpeg -y \
-i AmyWinehouseRehab.mp4 \
-f lavfi -t 0.1 -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-filter_complex "color=green:s=480x360,drawtext='font=sans-serif:fontcolor_expr=0x5090FF%{eif\: mod(t * 100, 255) \: x \: 2}:fontsize=35:x=20:y=20:text=Color Changing'" -t 10 \
ffmpeg5.mp4

#6- 6 Second Fade in Text With Shadow, Then Repeat:

ffmpeg -y -i BeastieBoysFightForYourRight.mp4 -filter_complex "[0:v]
drawtext='fontsize=50:x=50:y=50:fontcolor=white:
shadowcolor=blue:shadowx=5:shadowy=5:box=1:boxcolor=0xAAAAAA:boxborderw=32:
bordercolor=black:borderw=5:alpha=mod(t / 6, 1):text=6 Sec Fade-in'" \
ffmpeg6.mp4

#7- Show Video Counter:

ffmpeg -y -i BillyJoelPianoMan.mp4 -filter_complex "drawtext='font=monospace:fontcolor=red:fontsize=40:text=%{pts \: gmtime \: 0 \: \%H\\\\\\:\%M\\\\\\:\%S}'" \
ffmpeg7.mp4

#8- Show A Date Code:

ffmpeg -y -i ChumpaWambaTubThumping.mp4 -vf "drawtext=fontsize=36:fontcolor=red:text='%{pts\:gmtime\:1575526882\:%d/%m/%y %R}'" ffmpeg8.mp4

#9- Frame Counter:

ffmpeg -y -i Blink182SayItAintSo2.mp4 -filter_complex "drawtext='font=monospace:fontcolor=red:fontsize=40:text=%{pts \: hms}'" ffmpeg9.mp4

#10- Two Popups:

ffmpeg -y -i BlondieOnceIHadALove.mp4 -vf "[in]drawtext=fontsize=80:fontcolor=red:text=Found Love:enable='between(t,1,2)', drawtext=fontsize=80:fontcolor=red:text=Soon it turned out:enable='between(t,6,8)'[out]" -t 10 ffmpeg10.mp4

#11- Alter Volume:

ffmpeg -y -i BloodhoundGangTheBadTouch.mp4 -filter:a "volume=2" ffmpeg11.mp4

#12- Blur Whole-Screen Little:

ffmpeg -y -i BonJoviBadMedicine.mp4 -filter_complex "[0:V]boxblur=3[fg];[0:v] [fg]overlay=(0):0" ffmpeg12.mp4

#13- Blur Whole-Screen a Lot. Then Reveal after 5 secs:

ffmpeg -y -i CardiBWAP1.mp4 -filter_complex "[0:V]boxblur=20[fg];[0:v] [fg]overlay=(0):0:enable='between(t,0,5)'" ffmpeg13.mp4

#14- CROP the bottom-left 2/3s of the Video:

ffmpeg -i CelineDionMyHeartWillGoOn.mp4 -filter:v "crop=w=2/3*in_w:h=2/3*in_h:x=0:y=in_h" ffmpeg14.mp4

#15- 100x100 pixel blur:

ffmpeg -y -i ChumpaWambaTubThumping.mp4 -filter_complex "[0:V]crop=100:100,boxblur=20[fg];[0:v] [fg]overlay=(main_w-200):30" ffmpeg15.mp4

#16- Make Vertical video 16:9 Horizontal:

function ffmpeg-fill-blur(){
ffmpeg -y -i $1 -vf 'split[original][copy];[copy]scale=ih*16/9:-1,crop=h=iw*9/16,gblur=sigma=20[blurred];[blurred][original]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2' $2
}
ffmpeg-fill-blur CyndiLauperTimeAfterTime.mp4 ffmpeg16.mp4

#17- Motion Blur:

ffmpeg -i ElChomboChacarron.mp4 -vf tmix=frames=8:weights="1 1 1 1 1 1 1 1" ffmpeg17.mp4

#18- Combine 4 Videos:

ffmpeg -y \
-i CypressHillYouCantUnderstand.mp4 \
-i DivinylsITouchMyself.mp4 \
-i EminemMyNameis.mp4 \
-i EurythmicsSweetDreams.mp4 \
-f lavfi -t 0.1 -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-filter_complex "[0:v][0:a][1:v][1:a][2:v][2:a][3:v][3:a]concat=n=4:v=1:a=1" \
ffmpeg18.mp4

#19- Alernate Pics and Videos:

ffmpeg -y \
-loop 1 -framerate 24 -t 3 -i simpsonsLyricsGloriaGaynorIWillSurvive.jpg \
-i GloriaGaynorIWillSurvive.mp4 \
-loop 1 -framerate 24 -t 3 -i simpsonsLyricsGunAndRosesSweetChild.jpg \
-i GunAndRosesSweetChild.mp4 \
-loop 1 -framerate 24 -t 3 -i simpsonsLyricsJohnnyCashFolsomPrisonBlues.jpg \
-i JohnnyCashFolsomPrisonBlues.mp4 \
-f lavfi -t 0.1 -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-filter_complex "[0:v][6:a][1:v][1:a][2:v][6:a][3:v][3:a][4:v][6:a][5:v][5:a]concat=n=6:v=1:a=1" \
ffmpeg19.mp4

#20- Zoom:

ffmpeg -y \
-framerate 25 -loop 1 -i "James Brown.jpg" -filter_complex \
"[0:v]scale=500x500,zoompan=z='min(zoom+0.0025,1.5)':x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d=125,trim=duration=5[v]" -map "[v]" ffmpeg20.mp4

#21- Combine MP3s Quick!:

ffmpeg -i "concat:music1.mp3|music2.mp3" -acodec copy ffmpeg21.mp4

#22- Cut / Crop MP3s Quick!:

ffmpeg -ss 0 -t 76 -i music.mp3 -acodec copy ffmpeg22.mp4

#23- Test Zoom2:

ffmpeg -y \
-i "James Brown.jpg" -ss 0 -t 5 \ -r 1 -loop 1 -i avengers.jpg -ss 0 -t 5 \ -filter_complex " [1:v] scale=w=1920:h=1080 [fg]; \ [0:v] scale=w=-2:h=3*1080 , crop=w=3*1920:h=3*1080, \ zoompan=z=if(lte(zoom\,1.0)\,1/0.9\,max(1.0\,zoom+-0.0008)):d=25*5:x='iw/2-(iw/zoom/2)':\ y='ih/2-(ih/zoom/2)':s=1920x1080, setsar=1 [bg] ; \ [bg][fg]overlay=shortest=1[v] " \ -map "[v]" -c:v h264 -crf 18 -preset veryfast ffmpeg23.mp4

#24- Crossfade:

ffmpeg -y -i video1.mp4 -i video2.mp4 -filter_complex " \
[0:v] scale=w=min(iw*720/ih\,1280):h=min(720\,ih*1280/iw),pad=w=1280:h=720:x=(1280-iw)/2:y=(720-ih)/2,fade=out:st=3:d=2:alpha=1, setpts=PTS-STARTPTS [video1]; \
[1:v] scale=w=min(iw*720/ih\,1280):h=min(720\,ih*1280/iw),pad=w=1280:h=720:x=(1280-iw)/2:y=(720-ih)/2,fade=in :st=0:d=2:alpha=1, setpts='PTS+3/TB' [video2]; \
[video1][video2]overlay[v] " -map "[v]" -c:v h264 -crf 18 \
-preset veryfast ffmpeg24.mp4

#25- Crossfade WITH audio(0.1 is audio overlap) but causes gap:

ffmpeg -y -i video1.mp4 -i video2.mp4 -filter_complex " \
[0:v] scale=w=min(iw*720/ih\,1280):h=min(720\,ih*1280/iw),pad=w=1280:h=720:x=(1280-iw)/2:y=(720-ih)/2,fade=out:st=3:d=2:alpha=1, setpts=PTS-STARTPTS [video1]; \
[1:v] scale=w=min(iw*720/ih\,1280):h=min(720\,ih*1280/iw),pad=w=1280:h=720:x=(1280-iw)/2:y=(720-ih)/2,fade=in :st=0:d=2:alpha=1, setpts='PTS+3/TB' [video2]; \
[0:a] anull [audio1]; [1:a] anull [audio2]; \
[video1][audio1][video2][audio2] concat=n=2:v=1:a=1 [v][a] \
" -map "[v]" -map "[a]" -c:a aac -c:v h264 -crf 18 \
-preset veryfast -f mp4 ffmpeg25.mp4

#26- Zoom on Specific Spot (x27:x:81):

ffmpeg -y -i "James Brown.jpg" \
-vf "zoompan=z='min(zoom+0.01,2.5)': \
x='iw/2-iw*(1/2-27/100)*on/150-iw/zoom/2': \
y='ih/2-ih*(1/2-81/100)*on/150-ih/zoom/2': \
d=150" -c:v libx264 ffmpeg26.mp4

#27- Zoom on Specific Spot (x1011:x:568):

ffmpeg -y -i "James Brown.jpg" \
-vf "zoompan=z='min(zoom+0.01,2.5)': \
x='(iw-(iw/zoom))*(1011/1024)': \
y='(ih-(ih/zoom))*(568/576)',scale=640x480" \
-c:v libx264 ffmpeg27.mp4

#28- Fade Between Images to SLIDES:

ffmpeg -loop 1 -i "James Brown.jpg" -loop 1 -i "tattoo6IX9INE.jpg" \
-filter_complex "[1:v][0:v]blend=all_expr='A*(if(gte(T,3),1,T/3))+B*(1-(if(gte(T,3),1,T/3)))'" -t 4 frames_%04d.png ffmpeg28.mp4

#29- Fade Between 5 Images to MP4:

ffmpeg -y \
-loop 1 -t 3 -i "1.jpg" \
-loop 1 -t 3 -i "2.jpg" \
-loop 1 -t 3 -i "3.jpg" \
-loop 1 -t 3 -i "4.jpg" \
-loop 1 -t 3 -i "5.jpg" \
-filter_complex \
"[1:v][0:v]blend=all_expr='A*(if(gte(T,3),1,T/3))+B*(1-(if(gte(T,3),1,T/3)))'[v0]; \
[2:v][1:v]blend=all_expr='A*(if(gte(T,3),1,T/3))+B*(1-(if(gte(T,3),1,T/3)))'[v1]; \
[3:v][2:v]blend=all_expr='A*(if(gte(T,3),1,T/3))+B*(1-(if(gte(T,3),1,T/3)))'[v2]; \
[4:v][3:v]blend=all_expr='A*(if(gte(T,3),1,T/3))+B*(1-(if(gte(T,3),1,T/3)))'[v3]; \
[v0][v1][v2][v3]concat=n=4:v=1:a=0[v]" \
-map "[v]" ffmpeg29.mp4

#30- Fade Between 3 Images to MP4:

ffmpeg -y \
-loop 1 -t 8 -i "1.jpg" \
-loop 1 -t 8 -i "2.jpg" \
-loop 1 -t 8 -i "3.jpg" \
-filter_complex \
"[1:v][0:v]blend=all_expr='A*(if(gte(T,3),1,T/3))+B*(1-(if(gte(T,3),1,T/3)))'[v0]; \
[2:v][1:v]blend=all_expr='A*(if(gte(T,3),1,T/3))+B*(1-(if(gte(T,3),1,T/3)))'[v1]; \
[v0][v1]concat=n=2:v=1:a=0[v]" \
-map "[v]" ffmpeg30.mp4

#31- Remove ALL Audio from a clip:

ffmpeg -i "adamDylanVideo.mp4" -map 0 -map -0:a -c copy \
ffmpeg31.mp4

#32- Put Music over an Mp4:

ffmpeg -y -i "ffmpeg29.mp4" -i "TakeIt2.mp3" -c copy -shortest -map 0:0 -map 1:0 -map 0:1 \
ffmpeg32.mp4

#33- Fade Between 6 Images for 2 secs, Holding for 5 to MP4:

ffmpeg -y \
-loop 1 -t 7 -i "0.jpg" \
-loop 1 -t 7 -i "1.jpg" \
-loop 1 -t 7 -i "2.jpg" \
-loop 1 -t 7 -i "3.jpg" \
-loop 1 -t 7 -i "4.jpg" \
-loop 1 -t 7 -i "5.jpg" \
-filter_complex \
"[1:v][0:v]blend=all_expr='A*(if(gte(T,2),1,T/2))+B*(1-(if(gte(T,2),1,T/2)))'[v0]; \
[2:v][1:v]blend=all_expr='A*(if(gte(T,2),1,T/2))+B*(1-(if(gte(T,2),1,T/2)))'[v1]; \
[3:v][2:v]blend=all_expr='A*(if(gte(T,2),1,T/2))+B*(1-(if(gte(T,2),1,T/2)))'[v2]; \
[4:v][3:v]blend=all_expr='A*(if(gte(T,2),1,T/2))+B*(1-(if(gte(T,2),1,T/2)))'[v3]; \
[5:v][4:v]blend=all_expr='A*(if(gte(T,2),1,T/2))+B*(1-(if(gte(T,2),1,T/2)))'[v4]; \
[v0][v1][v2][v3][v4]concat=n=5:v=1:a=0[v]" \
-map "[v]" ffmpeg33.mp4

#34- Margaret Xmas Lights:

cd ~/Downloads/margaret
ffmpeg -y \
-i "Clip1(121sec).mp4" -i "Clip2(145sec).mp4" -i "Clip3(42sec).mp4" -i "Clip4(47sec).mp4" -i "Clip5(25sec).mp4" -i "Clip6(38sec).mp4" -i "Clip7(43sec).mp4" -i "Clip8(60sec).mp4" -i "Clip9(40sec).mp4" -i "Clip10(55sec).mp4" -i "Clip11(49sec).mp4" -i "Clip12(53sec).mp4" -i "Clip13(63sec).mp4" -i "Clip14(60sec).mp4" -i "Clip15(62sec).mp4" -i "Clip16(61sec).mp4" -i "Clip17(78sec).mp4" -i "Clip18(67sec).mp4" -i "Clip19(70sec).mp4" -i "Clip20(54sec).mp4" -i "Clip21(59sec).mp4" -i "Clip22(51sec).mp4" -i "Clip23(60sec).mp4" -i "Clip24(90sec).mp4" -i "Clip25(71sec).mp4" -i "Clip26(90sec).mp4" -i "Clip27(79sec).mp4" -i "Clip28(116sec).mp4" -i "Clip29(93sec).mp4" -i "Clip30(69sec).mp4" -i "ClipLastOne(17sec).mp4" -f lavfi -t 0.1 -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-filter_complex "[0:v][0:a][1:v][1:a][2:v][2:a][3:v][3:a][4:v][4:a][5:v][5:a][6:v][6:a][7:v][7:a][8:v][8:a][9:v][9:a][10:v][10:a][11:v][11:a][12:v][12:a][13:v][13:a][14:v][14:a][15:v][15:a][16:v][16:a][17:v][17:a][18:v][18:a][19:v][19:a][20:v][20:a][21:v][21:a][22:v][22:a][23:v][23:a][24:v][24:a][25:v][25:a][26:v][26:a][27:v][27:a][28:v][28:a][29:v][29:a][30:v][30:a]concat=n=31:v=1:a=1" \
ffmpeg34.mp4

#35- Margaret Xmas Music1:

ffmpeg -y -i "ffmpeg19.mp4" -i "music1.mp3" -i "music2.mp3" -c copy -shortest -map 0:0 -map 1:0 -map 0:1 ffmpeg20.mp4
ffmpeg -i video.avi -i audio1.mp3 -i audio2.mp3 -map 0 -map 1 -map 2 -codec copy output
ffmpeg -y -i "ffmpeg19.mp4" -i "music1.mp3" -i "music2.mp3" -map 0 -map 1 -map 2 -codec copy ffmpeg20.mp4
ffmpeg35.mp4

#36- Ummmm:

ffplay -i JohnnyCashHurt.mp4 -vf "drawtext='font=monospace:fontcolor=white:fontsize=40:text=%{localtime \: \%Y-\%m-\%d \%H\\\\\\:\%M\\\\\\:\%S}'"

#37- List All the Fonts you have:

(
exec > listFonts.cmd
t=0
for fon in `(cd /home/adam/.local/share/fonts/ ; ls *.tt[fc])` ; do
echo "${t}-$((${t} + 3)) [enter] drawtext reinit 'fontfile=/home/adam/.local/share/fonts/${fon}';"
echo "${t}-$((${t} + 3)) [enter] drawtext reinit 'text=${fon}';"
echo "${t}-$((${t} + 3)) [enter] drawtext reinit 'y=(${t}*10)';"
t=$((${t} + 3))
done
)
ffplay -f lavfi "color=white:s=660x660,loop=-1:size=2" -vf "sendcmd=f=listFonts.cmd,drawtext='fontsize=40:x=50:y=50:fontcolor=blue:text=Overriden Later'"

Videos:

Working with Audio, Defaults, Multi Language