rogerxavier commited on
Commit
03bba3d
1 Parent(s): c4358dd

Create moviepyTest

Browse files
Files changed (1) hide show
  1. moviepyTest +40 -0
moviepyTest ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from moviepy.editor import *
2
+ import cv2
3
+ import numpy as np
4
+
5
+ def test():
6
+ # 图片素材和字幕
7
+ image_files = ['6.jpg', '7.jpg']
8
+ subtitles = ['Subtitle 1', 'Subtitle 2']
9
+
10
+ # 视频分辨率和帧率
11
+ width, height = 1280, 720
12
+ fps = 30
13
+
14
+ # 创建视频编辑器
15
+ video = VideoFileClip(image_files[0]).set_duration(1) # 创建一个视频片段
16
+ video = video.resize((width, height))
17
+
18
+
19
+ ##https://blog.csdn.net/qq_19409845/article/details/117629593 参考配置imageMagick
20
+ ##根据报错提示位置在 D:\临时\venv\Lib\site-packages\moviepy config_default
21
+ # 添加字幕
22
+ txt_clip = TextClip(subtitles[0], fontsize=70, color='white', bg_color='black')
23
+ txt_clip = txt_clip.set_pos(('center', 'bottom')).set_duration(1)
24
+
25
+ video = CompositeVideoClip([video, txt_clip])
26
+
27
+ # 逐个添加图片和字幕
28
+ for i in range(1, len(image_files)):
29
+ img = cv2.imread(image_files[i])
30
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
31
+ clip = ImageClip(img).set_duration(1).resize((width, height))
32
+
33
+ txt_clip = TextClip(subtitles[i], fontsize=70, color='white', bg_color='black')
34
+ txt_clip = txt_clip.set_pos(('center', 'bottom')).set_duration(1)
35
+
36
+ clip = CompositeVideoClip([clip, txt_clip])
37
+ video = concatenate_videoclips([video, clip])
38
+
39
+ # 保存视频
40
+ video.write_videofile('output_video.mp4', fps=fps)