ffmpeg:如何将文本从右下角移动到屏幕中心

ffmpeg:如何将文本从右下角移动到屏幕中心

我正在尝试将文本从右下角(背景外)移动到屏幕中央。黑色文本应准确移动到绿色文本所在的位置。

这是我尝试过的:

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)'"

动态图

答案1

数学可能有点复杂,但这是可能的......

假设我们预先知道:W= 800,H= 400,tw= 170,th= 54
我们可以预先计算以下参数:

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 命令:

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 创建命令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


替换所有常量,仅使用 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


结果:
在此处输入图片描述

相关内容