File size: 1,371 Bytes
03bba3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from moviepy.editor import *
import cv2
import numpy as np

def test():
  # 图片素材和字幕
  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)