视频添加字幕

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'

相关推荐
l木本I6 分钟前
大模型低秩微调技术 LoRA 深度解析与实践
python·深度学习·自然语言处理·lstm·transformer
哆啦A梦的口袋呀10 分钟前
基于Python学习《Head First设计模式》第七章 适配器和外观模式
python·学习·设计模式
十月狐狸13 分钟前
Python字符串进化史:从青涩到成熟的蜕变
python
狐凄1 小时前
Python实例题:Python计算线性代数
开发语言·python·线性代数
西猫雷婶1 小时前
pytorch基本运算-导数和f-string
人工智能·pytorch·python
述雾学java1 小时前
深入理解 transforms.Normalize():PyTorch 图像预处理中的关键一步
人工智能·pytorch·python
要努力啊啊啊1 小时前
使用 Python + SQLAlchemy 创建知识库数据库(SQLite)—— 构建本地知识库系统的基础《一》
数据库·人工智能·python·深度学习·自然语言处理·sqlite
Andrew_Xzw2 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
凤头百灵鸟3 小时前
Python语法基础篇(包含类型转换、拷贝、可变对象/不可变对象,函数,拆包,异常,模块,闭包,装饰器)
python
多多*4 小时前
LUA+Reids实现库存秒杀预扣减 记录流水 以及自己的思考
linux·开发语言·redis·python·bootstrap·lua