如何使用FFmpeg实现视频长文本的自动换行添加?
FFmpeg drawtext长文本自动换行解决方案
FFmpeg的drawtext滤镜默认不支持自动换行,针对你的问题,提供两种可行解决方法:
方法1:手动插入换行符
在文本内容中用\n手动拆分长文本,让内容分多行显示:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='I tried adding text in the middle of the video,\nbut the results are not as desired,':fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2" -codec:a copy output.mp4
方法2:自动换行(按宽度限制)
如果需要自动根据画面宽度换行,可通过textfile加载文本,并设置textwidth参数限制文本最大宽度:
- 创建文本文件
text_content.txt,写入目标文本:
I tried adding text in the middle of the video, but the results are not as desired,
- 执行FFmpeg命令,设置
textwidth为画面宽度的90%(避免贴边),超出部分自动换行:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:textfile=text_content.txt:fontcolor=white:fontsize=24:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2:textwidth=0.9*w" -codec:a copy output.mp4
内容的提问来源于stack exchange,提问作者Joe Cola
相关产品推荐
相关产品推荐

