Ryanus commited on
Commit
cb2e520
·
verified ·
1 Parent(s): 8134861

Create split_video.py

Browse files
Files changed (1) hide show
  1. split_video.py +25 -0
split_video.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # split_video.py
2
+ from moviepy.editor import VideoFileClip
3
+ import os
4
+
5
+ def split_video(input_path, output_dir, segment_duration=60):
6
+ if not os.path.exists(input_path):
7
+ raise ValueError("輸入影片不存在")
8
+
9
+ video = VideoFileClip(input_path)
10
+ total_duration = video.duration
11
+ num_segments = int(total_duration / segment_duration) + (1 if total_duration % segment_duration > 0 else 0)
12
+ os.makedirs(output_dir, exist_ok=True)
13
+ outputs = []
14
+
15
+ for i in range(num_segments):
16
+ start = i * segment_duration
17
+ end = min((i + 1) * segment_duration, total_duration)
18
+ subclip = video.subclip(start, end)
19
+ output_file = os.path.join(output_dir, f"segment_{i+1}.mp4")
20
+ subclip.write_videofile(output_file, codec='libx264', audio_codec='aac', verbose=False, logger=None)
21
+ outputs.append(output_file)
22
+ subclip.close()
23
+
24
+ video.close()
25
+ return outputs