Spaces:
Sleeping
Sleeping
from moviepy.editor import * | |
import cv2 | |
import numpy as np | |
import subprocess | |
def test(): | |
# 执行命令 | |
subprocess.run(["apt", "install", "imagemagick"]) | |
# 图片素材和字幕 | |
image_files = ['6.jpg', '7.jpg'] | |
subtitles = ['Subtitle 1', 'Subtitle 2'] | |
# 视频分辨率和帧率 | |
width, height = 1280, 720 | |
fps = 30 | |
# 创建视频编辑器 | |
video = VideoFileClip(image_files[0]).set_duration(1) # 创建一个视频片段 | |
video = video.resize((width, height)) | |
##https://blog.csdn.net/qq_19409845/article/details/117629593 参考配置imageMagick | |
##根据报错提示位置在 D:\临时\venv\Lib\site-packages\moviepy config_default | |
# 添加字幕 | |
txt_clip = TextClip(subtitles[0], fontsize=70, color='white', bg_color='black') | |
txt_clip = txt_clip.set_pos(('center', 'bottom')).set_duration(1) | |
video = CompositeVideoClip([video, txt_clip]) | |
# 逐个添加图片和字幕 | |
for i in range(1, len(image_files)): | |
img = cv2.imread(image_files[i]) | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
clip = ImageClip(img).set_duration(1).resize((width, height)) | |
txt_clip = TextClip(subtitles[i], fontsize=70, color='white', bg_color='black') | |
txt_clip = txt_clip.set_pos(('center', 'bottom')).set_duration(1) | |
clip = CompositeVideoClip([clip, txt_clip]) | |
video = concatenate_videoclips([video, clip]) | |
# 保存视频 | |
video.write_videofile('output_video.mp4', fps=fps) | |
return "保存成功" | |