python moviepy 图文批量合成带字幕口播视频

最近在研究将图片和文本批量合成为带字幕口播视频

主要是基于python的moviepy库

python 复制代码
from generator import audio, pics, subs, video


def main():
    texts_input = 'example'
    pics_input = 'example'

    # 图片分辨率预处理
    pics.adjust(pics_input)

    # 文字转语音
    audio.text_to_audio(texts_input)

    # 语音转视频
    video.audio_to_video(texts_input, pics_input)

    # 生成字幕
    subs.download_subs(texts_input)

    # 生成字幕视频
    video.attach_subs(texts_input)


if __name__ == "__main__":
    main()

以下是核心的图片+文本转视频逻辑

python 复制代码
import json
from mutagen.mp3 import MP3
from moviepy import editor
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
import generator.pics as pics


def audio_to_video(text_input, pics_input):
    audio_filepath = './output/audios/' + text_input.replace(' ', '_') + '.mp3'
    video_filepath = './output/videos/' + text_input.replace(' ', '_') + '.mp4'
    pics_dir = './output/pics/' + pics_input.replace(' ', '_')

    list_of_images = pics.preprocess_pics(pics_dir)

    audio = MP3(audio_filepath)
    audio_length = audio.info.length
    fps = len(list_of_images) / audio_length
    
    # 生成视频
    video = editor.ImageSequenceClip(pics_dir, fps=fps)
    audio = editor.AudioFileClip(audio_filepath)
    final_video = video.set_audio(audio)

    final_video.write_videofile(video_filepath, codec="libx264", fps=10)

以下是我实现的示例项目,可参考和star一下下哈!

https://github.com/chengxs1994/text-img-generator-vedio

相关推荐
谅望者10 小时前
数据分析笔记07:Python编程语言介绍
大数据·数据库·笔记·python·数据挖掘·数据分析
Geo_V10 小时前
LangChain Memory 使用示例
人工智能·python·chatgpt·langchain·openai·大模型应用·llm 开发
小呀小萝卜儿11 小时前
2025-11-15 学习记录--Python-LSTM模型定义(PyTorch)
python·学习·lstm
百锦再11 小时前
第15章 并发编程
android·java·开发语言·python·rust·django·go
laufing11 小时前
pyinstaller 介绍
python·构建打包
谅望者12 小时前
数据分析笔记09:Python条件语循环
笔记·python·数据分析
Auspemak-Derafru12 小时前
从U盘损坏中恢复视频文件并修复修改日期的完整解决方案
python
techzhi13 小时前
Intellij idea 注释模版
java·python·intellij-idea
李昊哲小课13 小时前
wsl ubuntu24.04 cuda13 cudnn9 pytorch 显卡加速
人工智能·pytorch·python·cuda·cudnn
温暖名字13 小时前
调用qwen3-omni的api对本地文件生成视频文本描述(批量生成)
python·音视频·qwen·qa问答