如何通过FFmpeg脚本在指定时间点按固定增量调整drawtext的y坐标?
Absolutely! You can implement this stepped Y-position change for your text using FFmpeg's built-in expression support in the drawtext filter. The core idea is to use conditional logic based on the video's timestamp to set the Y value at each specified interval.
Step-by-Step Explanation
FFmpeg allows you to use mathematical and conditional expressions (like gte for "greater than or equal") to dynamically calculate filter parameters. For your requirement, we'll build a nested if expression that checks the current video time t (in seconds) and sets the Y coordinate accordingly:
- From 00:00:00 to 00:00:10 (and up to just before 00:00:11): Y = 0
- From 00:00:11 to just before 00:00:43: Y = 10
- From 00:00:43 to just before 00:10:44: Y = 20
- From 00:10:44 onwards: Y = 30
Final Command
First, convert your timecodes to total seconds for easier calculation:
- 00:10:44 = 10*60 + 44 = 644 seconds
- 00:00:43 = 43 seconds
- 00:00:11 = 11 seconds
Here's the working command (adjust for your OS):
Linux/macOS:
ffmpeg -i input.mp4 -vf 'drawtext=textfile=textfile.txt:x=0:y=if(gte(t,644),30,if(gte(t,43),20,if(gte(t,11),10,0))):fontsize=30:fontcolor=white:borderw=3:bordercolor=black:box=0' output.mp4
Windows:
ffmpeg -i input.mp4 -vf "drawtext=textfile=textfile.txt:x=0:y=if(gte(t,644),30,if(gte(t,43),20,if(gte(t,11),10,0))):fontsize=30:fontcolor=white:borderw=3:bordercolor=black:box=0" output.mp4
How the Expression Works
The nested if statements evaluate from the largest time downwards:
gte(t,644): Checks if the current time is ≥ 10:44. If true, Y=30.- If not,
gte(t,43)checks if time is ≥ 00:43. If true, Y=20. - If not,
gte(t,11)checks if time is ≥ 00:11. If true, Y=10. - The final
0is the default value for all times before 00:11 (including 00:10 exactly).
This ensures the text jumps to the correct Y position exactly at your specified time points, with no smooth transition—just the hard step you need.
内容的提问来源于stack exchange,提问作者distro.obs

