rogerxavier commited on
Commit
25d96aa
1 Parent(s): e1d783a

Update moviepyTest.py

Browse files
Files changed (1) hide show
  1. moviepyTest.py +32 -37
moviepyTest.py CHANGED
@@ -4,42 +4,37 @@ import numpy as np
4
  import subprocess
5
 
6
 
 
 
 
 
 
7
  def test():
8
- # 执行命令
9
- subprocess.run(["apt", "install", "imagemagick"])
10
- # 图片素材和字幕
11
- image_files = ['6.jpg', '7.jpg']
12
- subtitles = ['Subtitle 1', 'Subtitle 2']
13
-
14
- # 视频分辨率和帧率
15
- width, height = 1280, 720
16
- fps = 30
17
-
18
- # 创建视频编辑器
19
- video = VideoFileClip(image_files[0]).set_duration(1) # 创建一个视频片段
20
- video = video.resize((width, height))
21
-
22
-
23
- ##https://blog.csdn.net/qq_19409845/article/details/117629593 参考配置imageMagick
24
- ##根据报错提示位置在 D:\临时\venv\Lib\site-packages\moviepy config_default
25
- # 添加字幕
26
- txt_clip = TextClip(subtitles[0], fontsize=70, color='white', bg_color='black')
27
- txt_clip = txt_clip.set_pos(('center', 'bottom')).set_duration(1)
28
-
29
- video = CompositeVideoClip([video, txt_clip])
30
-
31
- # 逐个添加图片和字幕
32
- for i in range(1, len(image_files)):
33
- img = cv2.imread(image_files[i])
34
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
35
- clip = ImageClip(img).set_duration(1).resize((width, height))
36
-
37
- txt_clip = TextClip(subtitles[i], fontsize=70, color='white', bg_color='black')
38
- txt_clip = txt_clip.set_pos(('center', 'bottom')).set_duration(1)
39
-
40
- clip = CompositeVideoClip([clip, txt_clip])
41
- video = concatenate_videoclips([video, clip])
42
-
43
- # 保存视频
44
- video.write_videofile('output_video.mp4', fps=fps)
45
  return "保存成功"
 
4
  import subprocess
5
 
6
 
7
+ # 函数用于获取图片的持续时间
8
+ def get_duration(image_name) ->str:
9
+ return '0:00:02.875000'
10
+
11
+
12
  def test():
13
+ # 执行命令
14
+ subprocess.run(["apt", "install", "imagemagick"])
15
+ # 图片素材和字幕
16
+ image_files = ['6.jpg', '7.jpg']
17
+ subtitles = ['我我我,我帝者战神呀!', '嘻嘻!不要笑,不可以笑啦!']
18
+
19
+ # 视频分辨率和帧率
20
+ width, height = 1280, 720
21
+ fps = 30
22
+ font_path = '1.ttf'#设置字体以防默认字体无法同时处理中英文
23
+ # 创建视频编辑器
24
+ video_clips = []
25
+ for idx, image_file in enumerate(image_files):
26
+ img = cv2.imread(image_file)
27
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
28
+ clip = ImageClip(img).set_duration(get_duration(image_file)).resize((width, height))
29
+
30
+ txt_clip = TextClip(subtitles[idx], fontsize=70, color='white', bg_color='black',font = font_path)
31
+ txt_clip = txt_clip.set_pos(('center', 'bottom')).set_duration(get_duration(image_file))
32
+
33
+ clip = CompositeVideoClip([clip, txt_clip])
34
+ video_clips.append(clip)
35
+
36
+ video = concatenate_videoclips(video_clips)
37
+
38
+ # 保存视频
39
+ video.write_videofile('output_video.mp4', fps=fps)
 
 
 
 
 
 
 
 
 
 
40
  return "保存成功"