Ryanus commited on
Commit
51bb5d2
·
verified ·
1 Parent(s): 88c8194

Update split_video.py

Browse files
Files changed (1) hide show
  1. split_video.py +14 -13
split_video.py CHANGED
@@ -1,25 +1,26 @@
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
 
1
  # split_video.py
2
+ import subprocess
3
+ import math
4
  import os
5
 
6
  def split_video(input_path, output_dir, segment_duration=60):
7
  if not os.path.exists(input_path):
8
  raise ValueError("輸入影片不存在")
9
+
 
 
 
10
  os.makedirs(output_dir, exist_ok=True)
11
+ cmd_duration = ['ffprobe', '-v', 'quiet', '-show_entries', 'format=duration', '-of', 'csv=p=0', input_path]
12
+ result = subprocess.run(cmd_duration, capture_output=True, text=True)
13
+ total_duration = float(result.stdout.strip())
14
 
15
+ num_segments = math.ceil(total_duration / segment_duration)
16
+ outputs = []
17
  for i in range(num_segments):
18
+ start_time = i * segment_duration
 
 
19
  output_file = os.path.join(output_dir, f"segment_{i+1}.mp4")
20
+ cmd_split = [
21
+ 'ffmpeg', '-i', input_path, '-ss', str(start_time), '-t', str(segment_duration),
22
+ '-c', 'copy', '-avoid_negative_ts', 'make_zero', output_file, '-y'
23
+ ]
24
+ subprocess.run(cmd_split, check=True)
25
  outputs.append(output_file)
 
 
 
26
  return outputs