Askthewind’s diary

個人的なメモ

FFmpegでテキストをフェードインフェードアウトする

Show text fading in and out (appearing/disappearing)

f:id:Askthewind:20181210160155g:plain

 

alpha command

FFmpegでプレビュー

ffmpeg -f lavfi -i "color='aliceblue:480*360'" -filter_complex "drawtext=fontfile='C\:/Windows/Fonts/impact.ttf':fontsize='30':fontcolor='white':bordercolor='dodgerblue':borderw='5':text='%{localtime\:%a/%b/%d/%Y}':x='(w-tw)/2':y='(h-th)/2':alpha='if(gt(t,2)\,1\,if(gt(t\,1)\,(1-(2-t))/1\,0))*if(lt(t,5)\,1\,if(lt(t\,6)\,(1-(t-5))/1\,0))'" -f matroska - | ffplay -i -

FFmpegエンコード

ffmpeg -i "input.mp4" -filter_complex "drawtext=fontfile='C\:/Windows/Fonts/impact.ttf':fontsize='30':fontcolor='white':bordercolor='dodgerblue':borderw='5':text='%{localtime\:%a/%b/%d/%Y}':x='(w-tw)/2':y='(h-th)/2':alpha='if(gt(t,2)\,1\,if(gt(t\,1)\,(1-(2-t))/1\,0))*if(lt(t,5)\,1\,if(lt(t\,6)\,(1-(t-5))/1\,0))'" -movflags +faststart -pix_fmt yuv420p -c:v libx264 -crf 20 "output.mp4"

 drawtext=fontfile='C\:/Windows/Fonts/impact.ttf'
:fontsize='30':fontcolor='white'
:bordercolor='dodgerblue':borderw='5'
:text='%{localtime\:%a/%b/%d/%Y}'
:x='(w-tw)/2':y='(h-th)/2'でテロップのフォントスタイルとフォントサイズ、フォントカラーとボーダーカラー(枠線)、ボーダーの太さを指定してテキストに引数strftime()関数が使えるdrawtextのオプションText expansion localtime(ローカルタイムゾーン)を設定後、テキストを真ん中の位置に表示(正確にはオーバーレイ合成)出力する設定しています。

:alpha='if(gt(t,2)\,1\,if(gt(t\,1)\,(1-(2-t))/1\,0))*if(lt(t,5)\,1\,if(lt(t\,6)\,(1-(t-5))/1\,0))'でalphaコマンドで使えるStatement formula(計算式)コマンドを指定してテキストをフェードインフェードアウト出力する設定をしています。

 

fade command

FFmpegでプレビュー

ffmpeg -f lavfi -i "color=aliceblue:480*360" -filter_complex "split[a][b];[b]drawtext=fontfile='C\:/Windows/Fonts/impact.ttf':fontsize='30':fontcolor='white':bordercolor='dodgerblue':borderw='5':text='%{localtime\:%a/%b/%d/%Y}':x=(w-tw)/2:y=(h-th)/2,format=yuva444p,fade=t=in:st=2:d=1:alpha=1,fade=t=out:st=9:d=1:alpha=1[c];[a][c]overlay,format=yuv420p" -f matroska - | ffplay -i -

FFmpegエンコード

ffmpeg -i "input.mp4" -filter_complex "split[a][b];[b]drawtext=fontfile='C\:/Windows/Fonts/impact.ttf':fontsize='30':fontcolor='white':bordercolor='dodgerblue':borderw='5':text='%{localtime\:%a/%b/%d/%Y}':x=(w-tw)/2:y=(h-th)/2,format=yuva444p,fade=t=in:st=2:d=1:alpha=1,fade=t=out:st=9:d=1:alpha=1[c];[a][c]overlay,format=pix_fmts=yuv420p" -movflags +faststart -c:v libx264 -crf 20 "output.mp4"

 

blend command

FFmpegでプレビュー

ffmpeg -f lavfi -i "color=aliceblue:480*360" -filter_complex "drawtext=fontfile='C\:/Windows/Fonts/impact.ttf':fontsize='30':fontcolor='white':bordercolor='dodgerblue':borderw='5':text='%{localtime\:%a/%b/%d/%Y}':x='(w-tw)/2':y='(h-th)/2':enable='between(t,3,10)'[a];[a][0] blend=all_expr='A * (if(between(T, 3, 4), (T-3),if(between(T, 9, 10), (10-T), 1))) + B * (1 - ( if(between(T, 3, 4), (T-3),if(between(T, 9, 10), (10-T), 1))))'" -f matroska - | ffplay -i -

FFmpegエンコード

ffmpeg -i "input.mp4" -filter_complex "drawtext=fontfile='C\:/Windows/Fonts/impact.ttf':fontsize='30':fontcolor='white':bordercolor='dodgerblue':borderw='5':text='%{localtime\:%a/%b/%d/%Y}':x='(w-tw)/2':y='(h-th)/2':enable='between(t,3,10)'[a];[a][0] blend=all_expr='A * (if(between(T, 3, 4), (T-3),if(between(T, 9, 10), (10-T), 1))) + B * (1 - ( if(between(T, 3, 4), (T-3),if(between(T, 9, 10), (10-T), 1))))'" -movflags +faststart -pix_fmt yuv420p -c:v libx264 -crf 20 "output.mp4"

 

FFmpegでテキストをフェードイン、フェードアウトするblendコマンド。

blend=all_expr='A * (if(between(T, 3, 4), (T-3),if(between(T, 9, 10), (10-T), 1))) + B * (1 - ( if(between(T, 3, 4), (T-3),if(between(T, 9, 10), (10-T), 1))))'

 

Apply a fade-in/out effect to the text

f:id:Askthewind:20181212070653g:plain

 

fontcolor_expr command

FFmpegでプレビュー

ffmpeg -f lavfi -i "testsrc=s=480*360" -filter_complex "format=gbrp,drawtext=fontfile='C\:/Windows/Fonts/impact.ttf':fontsize='30':x=(w-tw)/2:y=(h-th)/1.2:text='Show text fading in and out':fontcolor_expr=ffffff%{eif\\:clip(255*(1*between(t\,1+1.5\,6-1.5)+((t-1)/1.5)*between(t\,1\,1+1.5)+(-(t-6)/1.5)*between(t\,6-1.5\,6) )\,0\,255)\\:x\\:2},format=yuv420p" -f matroska - | ffplay -i -

FFmpegエンコード

ffmpeg -i "input.mp4" -filter_complex "format=gbrp,drawtext=fontfile='C\:/Windows/Fonts/impact.ttf':fontsize='30':x=(w-tw)/2:y=(h-th)/1.2:text='Show text fading in and out':fontcolor_expr=ffffff%{eif\\:clip(255*(1*between(t\,1+1.5\,6-1.5)+((t-1)/1.5)*between(t\,1\,1+1.5)+(-(t-6)/1.5)*between(t\,6-1.5\,6) )\,0\,255)\\:x\\:2},format=pix_fmts=yuv420p" -movflags +faststart -c:v libx264 -crf 20 "output.mp4"

 

FFmpegでテキストをフェードイン、フェードアウトするfontcolor_exprコマンド。

fontcolor_expr=ffffff%{eif\\:clip(255*(1*between(t\,3+1.5\,10-1.5)+((t-3)/1.5)*between(t\,3\,3+1.5)+(-(t-10)/1.5)*between(t\,10-1.5\,10) )\,0\,255)\\:x\\:2}

 

 

参考にさせていただきました。Special Thanks 

stackoverflow.com
superuser.com

Also see

www.ffmpeg.org