svjack commited on
Commit
10a0408
·
verified ·
1 Parent(s): 6afce9f

Create video_tooklit.py

Browse files
Files changed (1) hide show
  1. video_tooklit.py +67 -0
video_tooklit.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #### 视频重复代码
2
+ #### 视频长度超过音频
3
+
4
+ from moviepy.editor import VideoFileClip, concatenate_videoclips
5
+
6
+ def repeat_video_to_duration(input_path, output_path, target_duration):
7
+ # 加载视频
8
+ clip = VideoFileClip(input_path)
9
+
10
+ # 获取视频的时长
11
+ clip_duration = clip.duration
12
+
13
+ # 如果目标时长小于视频时长,则对视频进行切片
14
+ if target_duration < clip_duration:
15
+ final_clip = clip.subclip(0, target_duration)
16
+ else:
17
+ # 计算需要重复的次数
18
+ repeat_times = int(target_duration // clip_duration)
19
+ remaining_duration = target_duration % clip_duration
20
+
21
+ # 重复视频
22
+ repeated_clips = [clip] * repeat_times
23
+
24
+ # 如果有剩余的时长,则添加一部分视频
25
+ if remaining_duration > 0:
26
+ remaining_clip = clip.subclip(0, remaining_duration)
27
+ repeated_clips.append(remaining_clip)
28
+
29
+ # 拼接所有视频片段
30
+ final_clip = concatenate_videoclips(repeated_clips)
31
+
32
+ # 保存结果
33
+ final_clip.write_videofile(output_path, codec='libx264')
34
+
35
+ # 示例使用
36
+ input_path = 'taizong.mp4'
37
+ output_path = 'taizong_15.mp4'
38
+ target_duration = 15 # 目标时长为30秒
39
+
40
+ repeat_video_to_duration(input_path, output_path, target_duration)
41
+
42
+ from moviepy.editor import ImageClip
43
+
44
+ def create_static_video_from_image(image_path, output_path, duration, fps=30):
45
+ """
46
+ 将图片生成一个指定时长的静态视频
47
+
48
+ :param image_path: 图片路径
49
+ :param output_path: 输出视频路径
50
+ :param duration: 视频时长(秒)
51
+ :param fps: 视频帧率(默认30帧/秒)
52
+ """
53
+ # 加载图片并创建静态视频片段
54
+ clip = ImageClip(image_path, duration=duration)
55
+
56
+ # 设置帧率
57
+ clip.fps = fps
58
+
59
+ # 保存视频
60
+ clip.write_videofile(output_path, codec='libx264')
61
+
62
+ # 示例使用
63
+ image_path = 'input_image.jpg' # 输入图片路径
64
+ output_path = 'output_video.mp4' # 输出视频路径
65
+ duration = 10 # 视频时长为10秒
66
+
67
+ create_static_video_from_image(image_path, output_path, duration)