Download split_video.py from Ryanus/Py: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/spaces/Ryanus/Py/resolve/51bb5d2d9022052b0b3cfbb63264016276deeea8/split_video.py
- Command line
-
hf download hf://spaces/Ryanus/Py@51bb5d2d9022052b0b3cfbb63264016276deeea8/split_video.py
-
curl -L -o split_video.py https://huggingface.co/spaces/Ryanus/Py/resolve/51bb5d2d9022052b0b3cfbb63264016276deeea8/split_video.py
1.02 kB
| # split_video.py | |
| import subprocess | |
| import math | |
| import os | |
| def split_video(input_path, output_dir, segment_duration=60): | |
| if not os.path.exists(input_path): | |
| raise ValueError("輸入影片不存在") | |
| os.makedirs(output_dir, exist_ok=True) | |
| cmd_duration = ['ffprobe', '-v', 'quiet', '-show_entries', 'format=duration', '-of', 'csv=p=0', input_path] | |
| result = subprocess.run(cmd_duration, capture_output=True, text=True) | |
| total_duration = float(result.stdout.strip()) | |
| num_segments = math.ceil(total_duration / segment_duration) | |
| outputs = [] | |
| for i in range(num_segments): | |
| start_time = i * segment_duration | |
| output_file = os.path.join(output_dir, f"segment_{i+1}.mp4") | |
| cmd_split = [ | |
| 'ffmpeg', '-i', input_path, '-ss', str(start_time), '-t', str(segment_duration), | |
| '-c', 'copy', '-avoid_negative_ts', 'make_zero', output_file, '-y' | |
| ] | |
| subprocess.run(cmd_split, check=True) | |
| outputs.append(output_file) | |
| return outputs |