-
Notifications
You must be signed in to change notification settings - Fork 10
/
cli_app.py
181 lines (160 loc) · 5.37 KB
/
cli_app.py
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
from DataProccessing.zip_module import ZipModule
from DataProccessing.bitify import Bitify
from DataProccessing.frame_generator import FrameGenerator
from DataProccessing.video import FrameVideo
import time
import shutil
import json
from PyInquirer import prompt, style_from_dict, Token
import os
from pytube import YouTube
from pytube.cli import on_progress
from colorama import init as colorama_init, Fore, Style
from art import *
import os
ENCODE = ' Encode file to video'
DECODE = ' Decode video to file'
CLEAN = ' Clean'
RELOAD_TIME = ' Reload time'
EXIT = ' Exit'
def create_dir(settings):
if not os.path.exists(settings['data_dir']):
os.mkdir(settings['data_dir'])
os.mkdir(settings['ip_files'])
os.mkdir(settings['op_generated'])
os.mkdir(settings['op_retrieved'])
os.mkdir(settings['download_dir'])
def delete_dir(settings):
if os.path.exists(settings['data_dir']):
shutil.rmtree(settings['data_dir'])
def encode_to_video(settings):
ip_dir = settings['ip_files']
op_dir = settings['op_generated']
height = settings['height']
width = settings['width']
exp = settings['expansion_factor']
OPz = settings['zip_name']
z = ZipModule()
b = Bitify()
f = FrameGenerator(height, width, exp)
v = FrameVideo()
tic = time.perf_counter()
z.zip(OPz, ip_dir, op_dir)
toc = time.perf_counter()
print(f"zip done: {toc - tic:0.4f} seconds")
tic = time.perf_counter()
fbits = b.fileToBits(f'{op_dir}{OPz}.zip')
toc = time.perf_counter()
print(f"bitify done: {toc - tic:0.4f} seconds")
tic = time.perf_counter()
img_array = f.storeFrames(fbits, op_dir)
toc = time.perf_counter()
print(f"frame generator done: {toc - tic:0.4f} seconds")
tic = time.perf_counter()
v.framesToVideo(op_dir, img_array)
toc = time.perf_counter()
print(f"frames to video done: {toc - tic:0.4f} seconds")
def decode_from_video(settings):
download_dir = settings['download_dir']
op_dir = settings['op_retrieved']
height = settings['height']
width = settings['width']
exp = settings['expansion_factor']
OPz = settings['zip_name']
z = ZipModule()
b = Bitify()
f = FrameGenerator(height, width, exp)
v = FrameVideo()
tic = time.perf_counter()
img_arr = v.extractFrames(download_dir)
toc = time.perf_counter()
print(f"video to frames done: {toc - tic:0.4f} seconds")
tic = time.perf_counter()
fbits = f.framesToBits(download_dir, img_arr)
toc = time.perf_counter()
print(f"frames to bits done: {toc - tic:0.4f} seconds")
tic = time.perf_counter()
b.bitsToFile(fbits, f'{op_dir}{OPz}.zip')
toc = time.perf_counter()
print(f"bit to file done: {toc - tic:0.4f} seconds")
tic = time.perf_counter()
z.unzip('Files', f'{op_dir}{OPz}.zip', op_dir)
toc = time.perf_counter()
print(f"unzip done: {toc - tic:0.4f} seconds")
def download_and_decode_video(url, settings):
yt_obj = YouTube(url, on_progress_callback=on_progress)
yt_stream = yt_obj.streams.get_highest_resolution()
yt_desc = yt_obj.description
try:
print('Downloading...')
tic = time.perf_counter()
yt_stream.download(filename='op.mp4', output_path=settings['download_dir'])
toc = time.perf_counter()
print()
print(f"downloading done: {toc - tic:0.4f} seconds")
with open(f'{settings["download_dir"]}metadata.json', 'w') as f:
f.write(yt_desc)
decode_from_video(settings)
except:
print('An error has occurred')
qs_style = style_from_dict({
Token.Text: '#FF9D00',
Token.QuestionMark: '#FF9D00 bold',
Token.Selected: '#f7c602 bold italic underline',
Token.Pointer: '#FF9D00 bold reverse',
Token.Answer: '#f7c602 bold',
})
questions = [
{
'type': 'list',
'name': 'operation',
'message': 'Choose operation:',
'choices': [
ENCODE,
DECODE,
CLEAN,
RELOAD_TIME,
EXIT
]
}
]
cleaning_confirmation = [
{
'type': 'confirm',
'name': 'confirm',
'message': 'Do you want to continue cleaning?'
}
]
reload_time = 0
while True:
# Clear console
time.sleep(reload_time)
os.system('cls' if os.name=='nt' else 'clear')
# WILD-STORAGE Header
t_wild = text2art(f'WILD', font='4max')
t_storage = text2art(f'STORAGE', font='4max')
print(f"\n\n{Fore.YELLOW}{t_wild}{Style.RESET_ALL}")
print(f"{Fore.CYAN}{t_storage}{Style.RESET_ALL}\n")
settings = json.load(open('settings.json'))
create_dir(settings)
try:
answer = prompt(questions, style=qs_style)['operation']
if answer == EXIT:
break
elif answer == CLEAN:
confirmation = prompt(cleaning_confirmation)['confirm']
if confirmation:
print('Cleaning...')
delete_dir(settings)
create_dir(settings)
print('Done!')
elif answer == ENCODE:
encode_to_video(settings)
elif answer == DECODE:
yt_url = input('Enter the YouTube video URL: ')
if yt_url != '': download_and_decode_video(yt_url, settings)
else: decode_from_video(settings)
elif answer == RELOAD_TIME:
reload_time = int(input('Enter the time in seconds: '))
except:
pass