FFmpeg:使用drawtext滤镜实现文本自动换行与视频内边距设置
解决FFmpeg drawtext文本溢出与添加内边距的问题
当然可以实现!FFmpeg的drawtext滤镜完全支持自动换行和内边距设置,咱们直接调整你的命令就能解决问题:
核心参数说明
- 自动换行:通过
text_wrap=1启用换行功能,配合max_width限制文本块的最大宽度,超过阈值就自动换行 - 内边距:直接调整
x和y的取值,让文本和视频边缘保持固定间距
修改后的完整命令
ffmpeg.exe -f lavfi -i color=c=white:s=640x480:d=5.396 -vf "[in] \ drawtext=fontfile=font.ttf:fontsize=20:fontcolor=black:x=20:y=20:max_width=600:text_wrap=1:text='this is new whiteboard te':enable='between(t,6.634,6.818)', \ drawtext=fontfile=font.ttf:fontsize=20:fontcolor=black:x=20:y=20:max_width=600:text_wrap=1:text='this is new whiteboard testing':enable='between(t,0.0,2.032)', \ drawtext=fontfile=font.ttf:fontsize=20:fontcolor=black:x=20:y=20:max_width=600:text_wrap=1:text='this is new whiteboard testing no padding and the text is overflowing from the video frame check this need to acheve word wrapping':enable='between(t,2.032,5.396)' \ [out]" -c:v libx264 -t 30 -crf 30 ../output.mp4
参数细节解释
x=20和y=20:给文本设置上下左右各20像素的内边距,如果需要不同的上下/左右边距,单独调整x、y数值即可max_width=600:因为视频宽度是640,减去左右各20的内边距,所以文本最大宽度设为600像素,确保文本不会超出画面边界text_wrap=1:开启自动换行逻辑,当文本长度超过max_width时会自动拆分成多行显示
这样修改后,你的文本就会在指定宽度内自动换行,同时保持和视频边缘的间距,不会再出现溢出画面的问题啦!
内容的提问来源于stack exchange,提问作者Chaitanya Sai
相关产品推荐
相关产品推荐

