-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
337 lines (267 loc) · 13.1 KB
/
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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from asyncio.windows_events import NULL
from posixpath import split
from flask import Flask, render_template, request
import os
from ffmpy import FFmpeg
from pathlib import Path
from random import sample
videos_folder = "/static/Videos/"
items_per_page = 15
app = Flask(__name__)
def get_files(directory):
path = os.getcwd() + directory
filess = os.listdir(path)
return filess
def get_files_with_extension(files, extension):
final_file_list = []
for file in files:
file_split = file.split('.')
if file_split[len(file_split) - 1] == extension:
final_file_list.append(file)
return final_file_list
def get_folders(current_folder):
path = os.getcwd() + "/static/" + current_folder
folder_list = []
for folder in next(os.walk(path, '.'))[1]:
if folder != 'thumbnails':
folder_list.append(folder)
return folder_list
def get_file_name(file):
return Path(file).stem
get_video_name = {}
if os.path.exists(os.getcwd() + videos_folder) == False:
os.mkdir(os.getcwd() + videos_folder)
# folder_path is the folder that contains the videos
# By default, thumbnails folder automatically created in static/Videos
# Returns the thumbnails of the videos inside the folder that is given to folder_path argument
def get_thumbnails(folder_path):
thumb_folder = os.getcwd() + folder_path + "/thumbnails"
if os.path.exists(thumb_folder) == False:
os.mkdir(thumb_folder)
filess = os.listdir(thumb_folder)
final_file_list = []
for file in filess:
file_split = file.split('.')
if file_split[len(file_split) - 1] == "png":
final_file_list.append(file)
return final_file_list
# folder_path is the folder that contains the videos
# Function cleans up unnecessary thumbnails
def cleanup_thumbs(folder_path):
for thumbnail in get_thumbnails(folder_path):
path = os.getcwd() + folder_path
video_name = path + get_file_name(thumbnail) + ".mp4"
if os.path.exists(video_name) == False:
os.remove(path + "thumbnails/" + thumbnail)
# Function generates thumbnails for the videos inside the given folder_path
# Thumbnails are stored in the folder "thumbnails"
def generate_thumbs(folder_path):
filesList = get_files(folder_path)
videos = get_files_with_extension(filesList, 'mp4')
path = os.getcwd() + folder_path
for video in videos:
p = Path(video)
thumbnail_name = path + 'thumbnails/' + str(p.with_suffix('.png'))
if os.path.exists(thumbnail_name) == False:
videoN = path + video
print(thumbnail_name)
ff = FFmpeg(inputs={videoN: None}, outputs={
thumbnail_name: ['-ss', '00:00:20', '-vframes', '1']})
ff.run()
# First we have to clean up thumbnails of the old videos and generate thumbnails for new videos
for folder in get_folders('Videos'):
cleanup_thumbs(videos_folder + folder + "/")
generate_thumbs(videos_folder + folder + "/")
array_thumbnails = get_thumbnails(videos_folder + folder + "/")
for thumbName in array_thumbnails:
get_video_name[thumbName] = get_file_name(thumbName)
cleanup_thumbs(videos_folder)
generate_thumbs(videos_folder)
for thumbName in get_thumbnails(videos_folder):
get_video_name[thumbName] = get_file_name(thumbName)
# Search for every file in the directory
list_of_every_file = []
for root, dirs, files in os.walk(os.getcwd() + "/" + videos_folder):
for file in files:
# append the file name to the list
list_of_every_file.append(os.path.join(root, file))
# clean up files list to only contain .png fies
every_thumbnail_paths = get_files_with_extension(list_of_every_file, 'png')
# Get a list that contains only the file names
every_thumbnail_names = []
for curr_path in every_thumbnail_paths:
every_thumbnail_names.append(str(get_file_name(curr_path)))
# Get the path of the video
every_video_path = []
for curr_path in every_thumbnail_paths:
splitPath = curr_path.split("/")
if (splitPath[len(splitPath)-3] == 'Videos'):
every_video_path.append("Videos/")
else:
every_video_path.append("Videos/" + splitPath[len(splitPath)-3] + "/")
# Add the file name and relevent path to a dictionary
file_links_dictionary = {}
for curr_ind in range(0, len(every_thumbnail_names)):
file_links_dictionary[every_thumbnail_names[curr_ind]
] = every_video_path[curr_ind]
@app.route("/")
@app.route("/home")
def hello_world():
cleanup_thumbs(videos_folder)
generate_thumbs(videos_folder)
array_thumbnails = get_thumbnails(videos_folder)
for thumbName in get_thumbnails("/static/Videos"):
get_video_name[thumbName] = get_file_name(thumbName)
return render_template('index.html', files=array_thumbnails, video_names=get_video_name, folders=get_folders('Videos'))
@app.route("/videos/<video_name>")
def video_display(video_name):
if video_name in file_links_dictionary:
try:
thumbs_mini = sample(every_thumbnail_names, 4)
return render_template('video_display.html', video_name=video_name, video_names=get_video_name, video_thumbs=thumbs_mini, file_links=file_links_dictionary, folders=get_folders('Videos'), is_display_Recommendations = True)
except:
return render_template('video_display.html', video_name=video_name, video_names=get_video_name, video_thumbs=[], file_links=file_links_dictionary, folders=get_folders('Videos'), is_display_Recommendations = False)
else:
return render_template('404_error.html')
@app.route("/folders/<folder_name>")
def folder_display(folder_name):
try:
cleanup_thumbs(videos_folder + folder_name + "/")
generate_thumbs(videos_folder + folder_name + "/")
array_thumbnails = get_thumbnails(videos_folder + folder_name + "/")
for thumbName in array_thumbnails:
get_video_name[thumbName] = get_file_name(thumbName)
return render_template('folder_view.html', name=folder_name, files=array_thumbnails, video_names=get_video_name, folders=get_folders('Videos'))
except:
return render_template('404_error.html')
@app.route("/m")
def m_index():
cleanup_thumbs(videos_folder)
generate_thumbs(videos_folder)
array_thumbnails = get_thumbnails(videos_folder)
thumb_groups = [array_thumbnails[x:x+items_per_page]
for x in range(0, len(array_thumbnails), items_per_page)]
print(thumb_groups)
for thumbName in get_thumbnails("/static/Videos"):
get_video_name[thumbName] = get_file_name(thumbName)
try:
return render_template('index_m.html', files=thumb_groups[0], video_names=get_video_name, folders=get_folders('Videos'), pages=len(thumb_groups) + 1, curr_page=1)
except:
return render_template('index_m.html', files=[], video_names=get_video_name, folders=get_folders('Videos'), pages=1, curr_page=1)
@app.route("/m/folders/<folder_name>")
def folder_display_m(folder_name):
try:
cleanup_thumbs(videos_folder + folder_name + "/")
generate_thumbs(videos_folder + folder_name + "/")
array_thumbnails = get_thumbnails(videos_folder + folder_name + "/")
thumb_groups = [array_thumbnails[x:x+items_per_page]
for x in range(0, len(array_thumbnails), items_per_page)]
for thumbName in array_thumbnails:
get_video_name[thumbName] = get_file_name(thumbName)
try:
return render_template('folder_view_m.html', name=folder_name, files=thumb_groups[0], video_names=get_video_name, folders=get_folders('Videos'), pages=len(thumb_groups) + 1, curr_page=1)
except:
return render_template('folder_view_m.html', name=folder_name, files=[], video_names=get_video_name, folders=get_folders('Videos'), pages=1, curr_page=1)
except:
return render_template('404_error.html')
@app.route("/m/videos/<video_name>")
def video_display_m(video_name):
if video_name in file_links_dictionary:
try:
thumbs_mini = sample(every_thumbnail_names, 4)
return render_template('video_display_m.html', video_name=video_name, video_names=get_video_name, video_thumbs=thumbs_mini, file_links=file_links_dictionary, folders=get_folders('Videos'), is_display_Recommendations = True)
except:
return render_template('video_display_m.html', video_name=video_name, video_names=get_video_name, video_thumbs=[], file_links=file_links_dictionary, folders=get_folders('Videos'), is_display_Recommendations = False)
else:
return render_template('404_error.html')
@app.route("/m/page<number>")
def pages_home(number):
array_thumbnails = get_thumbnails(videos_folder)
thumb_groups = [array_thumbnails[x:x+items_per_page]
for x in range(0, len(array_thumbnails), items_per_page)]
try:
if int(number) > len(thumb_groups):
return render_template('404_error.html')
else:
cleanup_thumbs(videos_folder)
generate_thumbs(videos_folder)
for thumbName in get_thumbnails("/static/Videos"):
get_video_name[thumbName] = get_file_name(thumbName)
return render_template('index_m.html', files=thumb_groups[int(number) - 1], video_names=get_video_name, folders=get_folders('Videos'), pages=len(thumb_groups) + 1, curr_page=int(number))
except:
return render_template('404_error.html')
@app.route("/m/folders/<folder_name>/page<number>")
def pages_folders(folder_name, number):
try:
cleanup_thumbs(videos_folder + folder_name + "/")
generate_thumbs(videos_folder + folder_name + "/")
array_thumbnails = get_thumbnails(videos_folder + folder_name + "/")
thumb_groups = [array_thumbnails[x:x+items_per_page]
for x in range(0, len(array_thumbnails), items_per_page)]
for thumbName in array_thumbnails:
get_video_name[thumbName] = get_file_name(thumbName)
return render_template('folder_view_m.html', name=folder_name, files=thumb_groups[int(number) - 1], video_names=get_video_name, folders=get_folders('Videos'), pages=len(thumb_groups) + 1, curr_page=int(number))
except:
return render_template('404_error.html')
@app.route("/about")
def about():
return render_template('about.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
folder = request.form['curr_folder']
split_fname = f.filename.split('.')
extension = split_fname[len(split_fname) - 1]
folder_path = "Videos/" if folder == 'home555412581__7aas' else "Videos/" + folder + "/"
print(folder_path)
split_fname = f.filename.split('.')
extension = split_fname[len(split_fname) - 1]
if extension == 'mp4' or extension == 'mkv':
f.save('static/' + folder_path + str(get_file_name(f.filename)) + '.mp4')
file_links_dictionary[str(get_file_name(f.filename))] = folder_path
return render_template('upload.html', files=[], video_names=get_video_name, folders=get_folders('Videos'), isValid=2)
else:
print("Invalid file")
return render_template('upload.html', files=[], video_names=get_video_name, folders=get_folders('Videos'), isValid=1)
else:
return render_template('upload.html', files=[], video_names=get_video_name, folders=get_folders('Videos'), isValid=0)
@app.route('/m/upload', methods=['GET', 'POST'])
def upload_file_m():
if request.method == 'POST':
f = request.files['file']
folder = request.form['curr_folder']
split_fname = f.filename.split('.')
extension = split_fname[len(split_fname) - 1]
folder_path = "Videos/" if folder == 'home555412581__7aas' else "Videos/" + folder + "/"
print(folder_path)
if extension == 'mp4' or extension == 'mkv':
f.save('static/' + folder_path + str(get_file_name(f.filename)) + '.mp4')
file_links_dictionary[str(get_file_name(f.filename))] = folder_path
return render_template('upload_m.html', files=[], video_names=get_video_name, folders=get_folders('Videos'), isValid=2)
else:
print("Invalid file")
return render_template('upload_m.html', files=[], video_names=get_video_name, folders=get_folders('Videos'), isValid=1)
else:
return render_template('upload_m.html', files=[], video_names=get_video_name, folders=get_folders('Videos'), isValid=0)
@app.route('/newfolder', methods=['GET', 'POST'])
def newFolder():
if request.method == 'POST':
f = request.form['fname']
print(f)
if os.path.exists(os.getcwd() + videos_folder + f) == False:
os.mkdir(os.getcwd() + videos_folder + f)
return """<script>
window.onload = function () {
if (
navigator.userAgent.match(/Android/i) ||
navigator.userAgent.match(/iPhone/i)
) {
console.log("You are on mobile view")
location.href = "../m/upload"
}
else{
location.href = "../upload"
}
}
</script>"""