网友回复
步骤:
1. 提取PPT中的文本和图片2. 使用Google Text-to-Speech生成语音3. 为每页PPT创建视频片段4. 自动排版图片和文字5. 合成最终视频
参考代码:
from pptx import Presentation
from moviepy.editor import ImageClip, TextClip, CompositeVideoClip, concatenate_videoclips, AudioFileClip
from gtts import gTTS
import os
import tempfile
import shutil
class PPTVideoGenerator:
def __init__(self,
ppt_path: str,
output_path: str,
slide_duration: float = 5.0,
language: str = 'zh-cn',
size: tuple = (1920, 1080)):
"""
初始化PPT视频生成器
Args:
ppt_path: PPT文件路径
output_path: 输出视频路径
slide_duration: 每页PPT的默认时长(秒)
language: 文字转语音的语言
size: 视频尺寸
"""
self.ppt_path = ppt_path
self.output_path = output_path
self.slide_duration = slide_duration
self.language = language
self.size = size
self.temp_dir = tempfile.mkdtemp()
def extract_slide_content(self, slide):
"""提取幻灯片中的文本和图片"""
texts = []
images = []
# 提取文本
for shape in slide.shapes:
if hasattr(shape, "text"):
if shape.text.strip():
texts.append(shape.text.strip())
# 提取图片
if shape.shape_type == 13: # MSO_SHAPE_TYPE.PICTURE
image_path = os.path.join(self.temp_dir, f"image_{len(images)}.png")
with open(image_path, 'wb') as f:
f.write(shape.image.blob)
images.append(image_path)
return texts, images
def create_slide_video(self, texts: list, images: list, audio_pat...点击查看剩余70%


