-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplitra.py
More file actions
212 lines (181 loc) · 7.1 KB
/
splitra.py
File metadata and controls
212 lines (181 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
import os
import sys
import json
import subprocess
import threading
import time
def install_dependencies():
try:
from colorama import init
from termcolor import colored
import pyfiglet
from tqdm import tqdm
return True
except ImportError:
import urllib.request
try:
urllib.request.urlopen('https://pypi.org', timeout=3)
subprocess.check_call([sys.executable, "-m", "pip", "install", "colorama", "pyfiglet", "termcolor", "tqdm"])
return True
except Exception:
print("No internet connection. Running in basic mode without colors.")
return False
has_colors = install_dependencies()
if has_colors:
from colorama import init, Fore, Style
from termcolor import colored
import pyfiglet
from tqdm import tqdm
init(autoreset=True)
else:
Fore = type("Fore", (), {"GREEN": "", "YELLOW": "", "RED": "", "CYAN": ""})()
Style = type("Style", (), {"BRIGHT": "", "RESET_ALL": ""})()
def colored(text, *args, **kwargs): return text
def tqdm(x, **kwargs): return x
class pyfiglet:
@staticmethod
def figlet_format(text): return text
def print_banner():
banner = pyfiglet.figlet_format("Splitra")
print(colored(banner, "cyan"))
print(colored("Created by Akashdip Mahapatra".center(80, " "), "yellow"))
print("-" * 80)
def list_videos():
return [f for f in os.listdir('.') if f.lower().endswith(('.mp4', '.avi', '.mkv'))]
def split_file(filename, chunk_size_mb):
chunk_size = int(chunk_size_mb * 1024 * 1024)
folder_name = os.path.splitext(filename)[0]
os.makedirs(folder_name, exist_ok=True)
part_files = []
with open(filename, 'rb') as f:
part_num = 0
while True:
chunk = f.read(chunk_size)
if not chunk:
break
part_filename = os.path.join(folder_name, f'part_{part_num:05d}')
with open(part_filename, 'wb') as chunk_file:
chunk_file.write(chunk)
part_files.append(f'part_{part_num:05d}')
part_num += 1
with open(os.path.join(folder_name, 'header.json'), 'w') as header:
json.dump({'original_filename': filename, 'parts': part_files}, header, indent=4)
with open(os.path.join(folder_name, 'export_video.py'), 'w') as export_script:
export_script.write(f"""import os
import json
def recombine():
with open("header.json", "r") as f:
data = json.load(f)
with open("output_" + data['original_filename'], "wb") as outfile:
for part in data['parts']:
with open(part, "rb") as infile:
outfile.write(infile.read())
print("Video recombined successfully as output_" + data['original_filename'])
if __name__ == "__main__":
recombine()
""")
print(Fore.GREEN + f"Video successfully split into folder: {folder_name}")
print(Fore.YELLOW + f"Use '{folder_name}/export_video.py' to recombine without this tool.")
def recombine_video():
folders = [d for d in os.listdir('.') if os.path.isdir(d) and 'header.json' in os.listdir(d)]
if not folders:
print("No valid chunk folders found.")
return
print("Select folder to export video from:")
for i, f in enumerate(folders):
print(f"{i + 1}. {Fore.CYAN + Style.BRIGHT}{f}{Style.RESET_ALL}")
while True:
try:
choice = int(input("Enter number: ")) - 1
if 0 <= choice < len(folders):
break
else:
print(f"Please enter a number between 1 and {len(folders)}.")
except ValueError:
print("Please enter a valid number.")
folder = folders[choice]
with open(os.path.join(folder, 'header.json'), 'r') as f:
data = json.load(f)
output_path = os.path.join('output_' + data['original_filename'])
with open(output_path, 'wb') as outfile:
for part in tqdm(data['parts'], desc="Recombining"):
part_path = os.path.join(folder, part)
with open(part_path, 'rb') as infile:
outfile.write(infile.read())
print(Fore.GREEN + f"Video exported successfully to {output_path}")
delete = input("Do you want to delete the chunk files? (y/N): ").strip().lower()
if delete == 'y':
for part in data['parts']:
os.remove(os.path.join(folder, part))
os.remove(os.path.join(folder, 'header.json'))
export_script = os.path.join(folder, 'export_video.py')
if os.path.exists(export_script):
os.remove(export_script)
if not os.listdir(folder):
os.rmdir(folder)
print(Fore.RED + f"Chunk folder '{folder}' removed.")
else:
print(Fore.YELLOW + f"Folder '{folder}' not empty, manual check recommended.")
print(Fore.GREEN + "Chunk files deleted successfully.")
# This function waits for user input with a timeout
def input_with_timeout(prompt, timeout=3):
user_input = [None]
def get_input():
user_input[0] = input(prompt)
thread = threading.Thread(target=get_input)
thread.daemon = True
thread.start()
thread.join(timeout)
if thread.is_alive():
print("\nNo input received. Defaulting to 'no'.")
return 'n'
return user_input[0]
def main():
print_banner()
print("What would you like to do?")
print("1. Split a video")
print("2. Export a video from chunks")
while True:
choice = input("Enter choice (1/2): ").strip()
if choice == '1':
videos = list_videos()
if not videos:
print("No video files found.")
return
print("Select a video to split:")
for i, v in enumerate(videos):
print(f"{i + 1}. {Fore.CYAN + Style.BRIGHT}{v}{Style.RESET_ALL}")
while True:
try:
v_choice = int(input("Enter number: ")) - 1
if 0 <= v_choice < len(videos):
break
else:
print(f"Please enter a number between 1 and {len(videos)}.")
except ValueError:
print("Please enter a valid number.")
selected_video = videos[v_choice]
while True:
try:
size = float(input("Enter chunk size in MB (e.g. 25): "))
if size > 0:
break
else:
print("Size must be greater than 0.")
except ValueError:
print("Please enter a valid number.")
split_file(selected_video, size)
break
elif choice == '2':
recombine_video()
break
else:
print("Please enter 1 or 2.")
if has_colors:
cleanup = input_with_timeout("Do you want to remove installed libraries (to save space)? (y/N): ", 3).strip().lower()
if cleanup == 'y':
os.system(f"{sys.executable} -m pip uninstall -y colorama pyfiglet termcolor tqdm")
print("Dependencies removed.")
if __name__ == "__main__":
main()