Skip to content

Commit 4f13640

Browse files
authored
Add files via upload
1 parent 1a71950 commit 4f13640

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

splitVidoe.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import os
2+
import sys
3+
import subprocess
4+
import math
5+
6+
7+
8+
9+
def get_file_size(file_path):
10+
result = subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=size', '-of', 'default=nw=1:nk=1', file_path],
11+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
12+
return int(result.stdout.strip())
13+
14+
15+
16+
17+
def get_file_duration(file_path):
18+
result = subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', file_path],
19+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
20+
return float(result.stdout.strip())
21+
22+
23+
24+
25+
def split_video(input_file):
26+
input_folder = os.path.dirname(input_file)
27+
input_filename = os.path.splitext(os.path.basename(input_file))[0]
28+
29+
original_file = f"{input_folder}/{input_filename}.mp4"
30+
output_filename = f"{input_folder}/{input_filename}"
31+
32+
33+
original_file_size_byte = get_file_size(original_file)
34+
block_number = math.ceil(original_file_size_byte / 2000000000)
35+
36+
37+
original_file_duration_second = get_file_duration(original_file)
38+
segment_duration = math.ceil(original_file_duration_second / block_number)
39+
40+
41+
print(f"Processing: {original_file}")
42+
print(f"original_file_size_byte: {original_file_size_byte}")
43+
print(f"block_number: {block_number}")
44+
print(f"original_file_duration_second: {original_file_duration_second}")
45+
print(f"segment_duration: {segment_duration}")
46+
47+
48+
if block_number > 1:
49+
print(f"block_number: {block_number} is greater than 1, continue executing the script")
50+
else:
51+
print(f"block_number: {block_number} is less than or equal to 1, the script exits")
52+
return
53+
54+
55+
# Split the file
56+
split_command = [
57+
'ffmpeg', '-i', original_file, '-c', 'copy', '-map', '0',
58+
'-segment_time', str(segment_duration), '-f', 'segment',
59+
'-reset_timestamps', '1', '-movflags', '+faststart',
60+
f"{output_filename}_%03d.mp4"
61+
]
62+
63+
64+
result = subprocess.run(split_command)
65+
if result.returncode != 0:
66+
print("Second part processing failed")
67+
return
68+
69+
70+
print("Processing completed for:", original_file)
71+
72+
73+
74+
75+
if __name__ == "__main__":
76+
if len(sys.argv) < 2:
77+
print("Usage: python script.py <input_file1> <input_file2> ... <input_fileN>")
78+
sys.exit(1)
79+
80+
81+
# Process each input file
82+
for input_file in sys.argv[1:]:
83+
split_video(input_file)

0 commit comments

Comments
 (0)