from moviepy.editor import * import cv2 import numpy as np import subprocess # 函数用于获取图片的持续时间 def get_duration(image_name) ->str: return '0:00:02.875000' def test(): # 执行命令 subprocess.run(["apt", "install", "imagemagick"]) # 图片素材和字幕 image_files = ['6.jpg', '7.jpg'] subtitles = ['我我我,我帝者战神呀!', '嘻嘻!不要笑,不可以笑啦!'] # 视频分辨率和帧率 width, height = 1280, 720 fps = 30 font_path = '1.ttf'#设置字体以防默认字体无法同时处理中英文 # 创建视频编辑器 video_clips = [] for idx, image_file in enumerate(image_files): img = cv2.imread(image_file) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) clip = ImageClip(img).set_duration(get_duration(image_file)).resize((width, height)) txt_clip = TextClip(subtitles[idx], fontsize=70, color='white', bg_color='black',font = font_path) txt_clip = txt_clip.set_pos(('center', 'bottom')).set_duration(get_duration(image_file)) clip = CompositeVideoClip([clip, txt_clip]) video_clips.append(clip) video = concatenate_videoclips(video_clips) # 保存视频 video.write_videofile('output_video.mp4', fps=fps) return "保存成功"