ffmpeg: So verschieben Sie Text von unten rechts in die Bildschirmmitte

ffmpeg: So verschieben Sie Text von unten rechts in die Bildschirmmitte

Ich versuche, einen Text von unten rechts (äußerer Hintergrund) in die Bildschirmmitte zu verschieben. Der schwarze Text sollte genau dort stehen, wo der grüne Text ist.

Folgendes habe ich versucht:

ffplay -f lavfi -loop 0 -i "color=color=yellow:size=800x400,drawtext=text='Test':fontsize=70:font='Arial Black':fontcolor=green:x=(W-tw)/2:y=(H-th)/2,drawtext=text='Test':fontcolor=black:fontsize=70:font='Arial Black':x=W-(t)*((W+tw)/2)/6:y=x*(H/W):enable='between(t,0,6)'"

gif

Antwort1

Die Mathematik mag etwas kompliziert sein, aber es ist möglich …

Angenommen, wir wissen im Voraus: W= 800, H= 400, tw= 170, th= 54.
Wir können die folgenden Parameter im Voraus berechnen:

alpha = atan2(H, W) = atan2(400, 800)   
sin_alpha = sin(alpha) = 0.4472136 alternately: sin_alpha = H/sqrt(W*W+H*H)  
cos_alpha = cos(alpha) = 0.8944272 alternately: cos_alpha = W/sqrt(W*W+H*H)

tw = 170
th = 54

radius = sqrt((W/2+tw/2)^2 + (H/2+th/2)^2) = sqrt((400+85)^2 + (200+27)^2) = 535.5
step = radius / duration = radius / 6 = 89.249

r = radius - t*step = 535.5 - t*3.57
center_x = W/2 + r*cos_alpha
center_y = H/2 + r*sin_alpha
x = center_x - tw/2 = W/2 + r*cos_alpha - tw/2 = 400+(535.5-t*89.249)*0.8944272-85
y = center_y - th/2 = H/2 + r*sin_alpha - th/2 = 200+(535.5-t*89.249)*0.4472136-27

FFplay-Befehl:

ffplay -f lavfi -loop 0 -i "color=color=yellow:size=800x400,drawtext=text='Test':fontsize=70:fontfile=ariblk.ttf:fontcolor=green:x=(W-tw)/2:y=(H-th)/2,drawtext=text='Test':fontcolor=black:fontsize=70:fontfile=ariblk.ttf:x=(400+(535.5-t*89.249)*0.8944272-85):y=(200+(535.5-t*89.249)*0.4472136-27):enable='between(t,0,6)'"


FFmpeg-Befehl zum Erstellen von test.gif:

ffmpeg -y -f lavfi -i "color=color=yellow:size=800x400:duration=6,format=rgb24,drawtext=text='Test':fontsize=70:fontfile=ariblk.ttf:fontcolor=green:x=(W-tw)/2:y=(H-th)/2,drawtext=text='Test':fontcolor=black:fontsize=70:fontfile=ariblk.ttf:x=(400+(535.5-t*89.249)*0.8944272-85):y=(200+(535.5-t*89.249)*0.4472136-27):enable='between(t,0,6)',split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" test.gif


Ersetzen aller Konstanten und Verwenden von nur W, H, tw, th:

ffmpeg -y -f lavfi -i "color=color=yellow:size=800x400:duration=6,format=rgb24,drawtext=text='Test':fontsize=70:fontfile=ariblk.ttf:fontcolor=green:x=(W-tw)/2:y=(H-th)/2,drawtext=text='Test':fontcolor=black:fontsize=70:fontfile=ariblk.ttf:x=(W/2+(sqrt((W/2+tw/2)*(W/2+tw/2)+(H/2+th/2)*(H/2+th/2))-t*sqrt((W/2+tw/2)*(W/2+tw/2)+(H/2+th/2)*(H/2+th/2))/6)*W/sqrt(W*W+H*H)-tw/2):y=(H/2+(sqrt((W/2+tw/2)*(W/2+tw/2)+(H/2+th/2)*(H/2+th/2))-t*sqrt((W/2+tw/2)*(W/2+tw/2)+(H/2+th/2)*(H/2+th/2))/6)*H/sqrt(W*W+H*H)-th/2):enable='between(t,0,6)',split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" test.gif


Ergebnis:
Bildbeschreibung hier eingeben

verwandte Informationen