视频添加字幕

python 复制代码
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip

from configs.settings import PROJECT_PATH


def add_subtitle(video_config, video_path, out_path):
    # 加载视频文件
    video = VideoFileClip(video_path)

    all_time = 0
    for config in video_config:
        subtitle = config.get('subtitle')
        time = config.get('time')
        # 添加文字
        text = TextClip(subtitle, fontsize=30, font=f"{PROJECT_PATH}/src/font/STXINGKA.TTF", bg_color='white', color='black')

        text = text.set_position(((video.w - text.w) / 2, video.h - text.h - 20)).set_start(all_time).set_duration(time)
        all_time += time

        # 将文字和音乐添加到视频中
        video = CompositeVideoClip([video, text])

    # 导出视频
    video.write_videofile(out_path, codec='mpeg4', audio_codec='aac')

1.其中video_config结构如下

json 复制代码
[{'subtitle': '盼望着,盼望着,东风来了,春天的脚步近了。', 'image': '1.png', 'time': 3}, {'subtitle': '一切都像刚睡醒的样子,欣欣然张开了眼。', 'image': '1.png', 'time': 3}, {'subtitle': '山朗润起来了,水涨起来了,太阳的脸红起来了。', 'image': '1.png', 'time': 3}, {'subtitle': '不好意思打断下,先来介绍下最新的显卡排名。', 'image': '1.png', 'top_image': '9.png', 'time': 4}, {'subtitle': 'xxxxxxx第一名。', 'image': '1.png', 'top_image': '9.png', 'time': 2}, {'subtitle': 'xxxxxxx第2名。', 'image': '2.png', 'top_image': '9.png', 'time': 3}, {'subtitle': 'xxxxxxx第3名。', 'image': '2.png', 'top_image': '9.png', 'time': 3}, {'subtitle': 'xxxxxxx第4名。', 'image': '2.png', 'top_image': '9.png', 'time': 2}, {'subtitle': 'xxxxxxx第5名。', 'image': '2.png', 'top_image': '9.png', 'time': 3}, {'subtitle': '园子里,田野里,瞧去,一大片一大片满是的。', 'image': '2.png', 'time': 3}, {'subtitle': '坐着,躺着,打两个滚,踢几脚球,', 'image': '2.png', 'time': 3}, {'subtitle': '赛几趟跑,捉几回迷藏。风轻悄悄的,草软绵绵的。', 'image': '2.png', 'time': 4}, {'subtitle': '桃树、杏树、梨树,你不让我,我不让你,都开满了花赶趟儿。', 'image': '3.png', 'time': 5}, {'subtitle': '红的像火,粉的像霞,白的像雪。', 'image': '3.png', 'time': 2}, {'subtitle': '花里带着甜味儿,闭了眼,树上仿佛已经满是桃儿、杏儿、梨儿。', 'image': '3.png', 'time': 5}, {'subtitle': '花下成千成百的蜜蜂嗡嗡地闹着,大小的蝴蝶飞来飞去。', 'image': '3.png', 'time': 4}, {'subtitle': '野花遍地是:杂样儿,有名字的,没名字的,', 'image': '3.png', 'time': 3}, {'subtitle': '散在花丛里,像眼睛,像星星,还眨呀眨的。', 'image': '3.png', 'time': 3}, {'subtitle': '"吹面不寒杨柳风",不错的,像母亲的手抚摸着你。', 'image': '4.png', 'time': 4}, {'subtitle': '风里带来些新翻的泥土的气息,混着青草味儿,', 'image': '4.png', 'time': 3}, {'subtitle': '还有各种花的香,都在微微润湿的空气里酝酿。', 'image': '4.png', 'time': 3}, {'subtitle': '鸟儿将巢安在繁花嫩叶当中,高兴起来了,', 'image': '4.png', 'time': 3}, {'subtitle': '呼朋引伴地卖弄清脆的喉咙,唱出宛转的曲子,', 'image': '4.png', 'time': 3}, {'subtitle': '跟轻风流水应和着。牛背上牧童的短笛,', 'image': '4.png', 'time': 3}, {'subtitle': '这时候也成天在嘹亮地响着。', 'image': '4.png', 'time': 2}, {'subtitle': '雨是最寻常的,一下就是三两天。可别恼。', 'image': '5.png', 'time': 4}, {'subtitle': '看,像牛毛,像花针,像细丝,密密地斜织着,', 'image': '5.png', 'time': 3}, {'subtitle': '人家屋顶上全笼着一层薄烟。树叶儿却绿得发亮,', 'image': '5.png', 'time': 4}, {'subtitle': '小草也青得逼你的眼。傍晚时候,上灯了,', 'image': '5.png', 'time': 3}, {'subtitle': '一点点黄晕的光,烘托出一片这安静而和平的夜。', 'image': '5.png', 'time': 4}, {'subtitle': '在乡下,小路上,石桥边,有撑起伞慢慢走着的人;', 'image': '6.png', 'time': 4}, {'subtitle': '还有地里工作的农民,披着蓑戴着笠。他们的草屋,', 'image': '6.png', 'time': 3}, {'subtitle': '稀稀疏疏的,在雨里静默着。', 'image': '6.png', 'time': 2}, {'subtitle': '天上风筝渐渐多了,地上孩子也多了。', 'image': '7.png', 'time': 3}, {'subtitle': '城里乡下,家家户户,老老小小,', 'image': '7.png', 'time': 2}, {'subtitle': '也赶趟儿似的,一个个都出来了。', 'image': '7.png', 'time': 3}, {'subtitle': '舒活舒活筋骨,抖擞抖擞精神,', 'image': '7.png', 'time': 2}, {'subtitle': '各做各的一份儿事去,"一年之计在于春";', 'image': '7.png', 'time': 3}, {'subtitle': '刚起头儿,有的是工夫,有的是希望。', 'image': '7.png', 'time': 2}, {'subtitle': '春天像刚落地的娃娃,从头到脚都是新的,它生长着。', 'image': '8.png', 'time': 4}, {'subtitle': '春天像小姑娘,花枝招展的,笑着,走着。', 'image': '8.png', 'time': 3}, {'subtitle': '春天像健壮的青年,有铁一般的胳膊和腰脚,他领着我们上前去。', 'image': '8.png', 'time': 5}]

2.字母通过TextClip类创建

可以设置字体和大小,还可以设置字母开始显示时间set_start

可以设置字体停留时间set_duration

可以设置字体的显示位置set_position

3.最后通过CompositeVideoClip类,将字幕和视频合并

注意:

有个坑,添加字幕,需要本地先安装magick

并且设置环境变量:

os.environ["IMAGEMAGICK_BINARY"] = r'/opt/homebrew/Cellar/imagemagick/7.1.1-34/bin/magick'

相关推荐
数据智能老司机27 分钟前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机27 分钟前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机27 分钟前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i37 分钟前
drf初步梳理
python·django
每日AI新事件37 分钟前
python的异步函数
python
这里有鱼汤2 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook11 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室11 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三13 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271116 小时前
Python之语言特点
python