视频添加字幕

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'

相关推荐
Q_Q19632884754 分钟前
python的电影院座位管理可视化数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
BYSJMG16 分钟前
计算机大数据毕业设计推荐:基于Hadoop+Spark的食物口味差异分析可视化系统【源码+文档+调试】
大数据·hadoop·分布式·python·spark·django·课程设计
杜子不疼.31 分钟前
《Python学习之第三方库:开启无限可能》
开发语言·python·学习
青衫客361 小时前
用 Python 实现一个“小型 ReAct 智能体”:思维链 + 工具调用 + 环境交互
python·大模型·llm·react
AI视觉网奇2 小时前
音频分类模型笔记
人工智能·python·深度学习
阿赵3D2 小时前
Unity引擎播放HLS自适应码率流媒体视频
unity·游戏引擎·音视频·流媒体·hls
sukalot2 小时前
window显示驱动开发—在混合系统中使用跨适配器资源
数据库·驱动开发·音视频
Ratten3 小时前
【Python 实战】---- 实现一个可选择、配置操作的批量文件上传工具(四)配置管理界面和逻辑实现
python
Ratten3 小时前
【Python 实战】---- 实现一个可选择、配置操作的批量文件上传工具(五)打包成 exe 应用
python
跟橙姐学代码3 小时前
写 Python 函数别再死抠参数了,这招让代码瞬间灵活
前端·python