File size: 1,336 Bytes
03bba3d
 
 
aee382f
 
03bba3d
25d96aa
 
 
 
 
03bba3d
cd8e7f1
25d96aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd8e7f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 "保存成功"