You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MoviePy实现文本换行或添加边距避免触碰视频边缘

问题:如何给视频中的文本添加左右边距避免紧贴边缘?

我现有代码可从CSV文件读取两组文本并显示在视频中,CSV示例行如下:

This is the first text to display,This is the second text to display

但文本会紧贴视频左右边缘,想知道怎么通过添加边缘边距或设置文本换行来在两侧留出间隙?试了多种方法都没搞定,不确定该加边距元素还是自定义换行。以下是当前未设置边距的代码:

with open('text.csv', newline='') as csvfile:
    text_reader = csv.reader(csvfile, delimiter=',')
    for j, row in enumerate(text_reader):
        if j == i:
            text1 = row[0]
            text_clip1 = TextClip(text1, fontsize=50, color='white', font="Roboto-Bold.ttf", align='Center', method='caption', size=clip.size, stroke_color='black', stroke_width=1)
            text_clip1 = text_clip1.set_position(('center', 'bottom')).set_start(2).set_duration(4)
            if len(row) > 1:
                text2 = row[1]
                text_clip2 = TextClip(text2, fontsize=50, color='white', font="Roboto-Bold.ttf", align='Center', method='caption', size=clip.size, stroke_color='black', stroke_width=1)
                text_clip2 = text_clip2.set_position(('center', 'bottom')).set_start(7).set_duration(4)
                text_clip = mymovie.CompositeVideoClip([text_clip1, text_clip2])
            else:
                text_clip = text_clip1
            clip = mymovie.CompositeVideoClip([clip, text_clip])

解决方案

方法1:调整文本区域尺寸(推荐)

创建TextClip时,不要直接用视频尺寸clip.size作为文本区域大小,而是设置一个比视频宽度小的尺寸,比如左右各留100像素间隙:

# 计算带边距的文本区域尺寸,左右各减100px
caption_size = (clip.size[0] - 200, clip.size[1])

这样文本会在这个缩小的区域内自动换行,再通过set_position(('center', 'bottom'))居中后,自然就和视频边缘留出了间隙。

修改后的完整代码:

with open('text.csv', newline='') as csvfile:
    text_reader = csv.reader(csvfile, delimiter=',')
    for j, row in enumerate(text_reader):
        if j == i:
            # 左右各留100px的文本区域
            caption_size = (clip.size[0] - 200, clip.size[1])
            
            text1 = row[0]
            text_clip1 = TextClip(text1, fontsize=50, color='white', font="Roboto-Bold.ttf", 
                                  align='Center', method='caption', size=caption_size, 
                                  stroke_color='black', stroke_width=1)
            text_clip1 = text_clip1.set_position(('center', 'bottom')).set_start(2).set_duration(4)
            
            if len(row) > 1:
                text2 = row[1]
                text_clip2 = TextClip(text2, fontsize=50, color='white', font="Roboto-Bold.ttf", 
                                      align='Center', method='caption', size=caption_size, 
                                      stroke_color='black', stroke_width=1)
                text_clip2 = text_clip2.set_position(('center', 'bottom')).set_start(7).set_duration(4)
                text_clip = mymovie.CompositeVideoClip([text_clip1, text_clip2])
            else:
                text_clip = text_clip1
            
            clip = mymovie.CompositeVideoClip([clip, text_clip])

方法2:给文本剪辑直接加边距

如果不想调整文本区域尺寸,也可以在创建TextClip后,用margin方法给文本添加左右边距:

text_clip1 = TextClip(text1, fontsize=50, color='white', font="Roboto-Bold.ttf", 
                      align='Center', method='caption', size=clip.size, 
                      stroke_color='black', stroke_width=1)
# 左右各加100px边距,上下边距设为0
text_clip1 = text_clip1.margin(left=100, right=100, top=0, bottom=0).set_position(('center', 'bottom')).set_start(2).set_duration(4)

注意:添加边距后文本剪辑的总尺寸会变大,需确保后续位置设置正确,避免超出视频范围。


内容的提问来源于stack exchange,提问作者Mitch C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 04:17:42