Python MoviePy将PPT转为视频
工作中遇到了需要将PPT转换成视频的情况,查询了资料,发现python的moviepy库比较好用#!/usr/bin/python# -*- coding: UTF-8 -*-import osfrom win32com.client import Dispatch, gencachefrom time import sleepfrom shutil import rmtreefrom re im
·
工作中遇到了需要将PPT转换成视频的情况,查询了资料,发现python的moviepy库比较好用
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
from win32com.client import Dispatch, gencache
from time import sleep
from shutil import rmtree
from re import findall
from os.path import join,splitext,dirname
from moviepy.editor import ImageSequenceClip
def ppt2mp4(ppt_fn):
picture_dir = splitext(ppt_fn)[0]
powerpoint = Dispatch("PowerPoint.Application")
powerpoint.Visible = True
#将PPT拆分为一张张图片
ppt1 = powerpoint.Presentations.Open(ppt_fn)
gencache.EnsureDispatch('PowerPoint.Application')
ppt1.SaveAs(f"{picture_dir}.jpg",17)
ppt1.Close()
if not powerpoint.Presentations:
powerpoint.Quit()
pictures = [
join(picture_dir,fn)
for fn in os.listdir(picture_dir)
if fn.endswith(".jpg")
]
pictures.sort(key=lambda fn:int(findall(r'\d+',fn)[-1]))
#设置视频时长为60秒
num = len(pictures)
durations = [float(str(60/num)[:4])] * num
(ImageSequenceClip(pictures,durations=durations).write_videofile(rf'{splitext(ppt_fn)[0]}_video.mp4',codec='libx264',fps=24))
# clip = ImageSequenceClip(pictures,durations=durations,fps=24)
# clip.write_videofile("test.mp4")
rmtree(picture_dir)
if __name__ == '__main__':
ppt2mp4(“PPT路径”)
批量处理后发现有一些视频生成之后无法播放,目前还不确定是什么原因,待处理
更多推荐
已为社区贡献1条内容
所有评论(0)