视频添加字幕

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'

相关推荐
大佬,救命!!!17 分钟前
整理python快速构建数据可视化前端的Dash库
python·信息可视化·学习笔记·dash·记录成长
孔丘闻言19 分钟前
python调用mysql
android·python·mysql
Teletele-Lin38 分钟前
Miniconda安装与VSCode搭建远程Python、Jupyter开发环境
vscode·python·jupyter·环境配置·远程开发
伊玛目的门徒1 小时前
告别 OpenAI SDK:如何使用 Python requests 库调用大模型 API(例如百度的ernie-4.5-turbo)
python·openai·requests·大模型调用·ernie-4.5-turbo
AI360labs_atyun2 小时前
2025世界智博会,揭幕AI触手可及的科幻生活
人工智能·ai·音视频·生活
sinat_602035362 小时前
模块与包的导入
运维·服务器·开发语言·python
计算机学姐2 小时前
基于Python的旅游数据分析可视化系统【2026最新】
vue.js·后端·python·数据分析·django·flask·旅游
恋雨QAQ2 小时前
python函数和面向对象
开发语言·python
天雪浪子3 小时前
Python入门教程之逻辑运算符
开发语言·python
骄傲的心别枯萎3 小时前
RV1126 NO.16:通过多线程同时获取H264和H265码流
linux·c++·音视频·rv1126