forked from martymfly/youtube-trim-download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
277 lines (241 loc) · 9.28 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
import json
import os
import redis
import yt_dlp
from flask import Flask
from flask import abort
from flask import jsonify
from flask import render_template
from flask import request
from flask import send_from_directory
from flask_cors import CORS
from celery import Celery
from dotenv import load_dotenv
from werkzeug.utils import secure_filename
load_dotenv()
app = Flask(__name__, static_url_path="/templates/static/")
CORS(app)
UPLOAD_FOLDER = "videos"
ON_HEROKU = "ON_HEROKU" in os.environ
TRIM_TASK_NAME = "worker.tasks.trim" if ON_HEROKU else "tasks.trim"
REDIS_LOCAL_URL = "redis://localhost:6379"
REDIS_URL = os.environ.get("REDIS_URL", REDIS_LOCAL_URL)
UPLOAD_SECRET_KEY = os.environ.get("UPLOAD_SECRET_KEY")
TRIMMED_FILE_SIZE_LIMIT_MB = os.environ.get("TRIMMED_FILE_SIZE_LIMIT_MB", 100)
VIDEO_FILE_SIZE_LIMIT_MB = os.environ.get("VIDEO_FILE_SIZE_LIMIT_MB", 400)
ALLOWED_VIDEO_SIZES = ("360", "480", "720")
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
celery = Celery(
"tasks",
broker=REDIS_URL,
backend=REDIS_URL,
)
redis_instance = redis.from_url(REDIS_URL)
ydl_opts = {}
ydlr = yt_dlp.YoutubeDL(ydl_opts)
def create_videos_folder():
try:
if not os.path.exists("videos"):
os.mkdir("videos")
except Exception as e:
print(e)
create_videos_folder()
def process_video_request(data):
videos = []
selected = {}
for x in data["formats"]:
if x["ext"] == "m4a":
x["format_note"] = "m4a"
videos.append(x)
res = x["format"].split("-")[1].strip().split(" ")[0]
if (
any(s in x["format_note"] for s in ALLOWED_VIDEO_SIZES)
and "throttled" not in x["format"].lower()
and x["filesize"] is not None
):
videos.append(x)
for v in videos:
try:
res = v["format"].split("-")[1].strip().split(" ")[0]
if res in selected:
if (
v["filesize"] < selected[res]["filesize"]
or v["fps"] > selected[res]["fps"]
):
selected[res] = v
if res == "m4a":
selected[res] = v
else:
selected[res] = v
except:
pass
return selected
def json_response(success, data, message, status_code):
return (
jsonify({"success": success, "data": data, "message": message}),
status_code,
)
def has_requester_active_task(ip):
has_active_task = False
for task in redis_instance.scan_iter(match="celery-trim-task*"):
task_id = task.decode("utf-8")
task_details = json.loads(redis_instance.get(task_id).decode("utf-8"))
if task_details["ip"] == ip:
if (
celery.AsyncResult(task_details["task_id"], app=celery).status
== "PENDING"
):
has_active_task = True
return has_active_task
def calculate_trimmed_file_size(video_info, quality, start, end):
final_size_is_below_limit = False
try:
total_length = video_info["duration"]
for format in video_info['formats']:
if format['format_id'] == quality:
video_file_size = format['filesize']/1024/1024
video_size_per_sec = video_file_size/total_length
trimmed_video_size = int((end - start) * video_size_per_sec)
if trimmed_video_size > int(TRIMMED_FILE_SIZE_LIMIT_MB):
final_size_is_below_limit = False
else:
final_size_is_below_limit = True
except Exception as e:
print(e)
final_size_is_below_limit = False
return final_size_is_below_limit
def video_size_below_limit(video_info, quality):
video_size_below_limit = False
try:
for format in video_info['formats']:
if format['format_id'] == quality:
video_file_size = format['filesize']/1024/1024
if video_file_size > int(VIDEO_FILE_SIZE_LIMIT_MB):
video_size_below_limit = False
else:
video_size_below_limit = True
except Exception as e:
print(e)
video_size_below_limit = False
return video_size_below_limit
@app.route("/")
def home():
return render_template("trimpage.html")
@app.route("/static/<path:path>")
def send_static(path):
return send_from_directory("templates/static/", path)
@app.route("/getvideodetails", methods=["POST"])
def get_video_details():
if request.json is not None:
url = request.json["url"]
if url is not None:
try:
request_video_result = ydlr.extract_info(url, download=False)
response = process_video_request(request_video_result)
return json_response(
True,
{"formats": response, "videoID": request_video_result["id"]},
None,
200,
)
except:
return json_response(False, None, f"{url} is not a valid URL", 400)
return json_response(False, None, "Please provide a valid URL", 400)
else:
json_response(False, None, "Please provide a valid URL", 400)
@app.route("/trim", methods=["POST"])
def trim():
requester_ip = request.remote_addr
if has_requester_active_task(requester_ip):
return json_response(
False,
None,
"You have an active task in the queue, please wait for it to complete!",
400,
)
else:
url = request.args.get("url")
quality = request.args.get("quality")
start = request.args.get("start")
end = request.args.get("end")
if url and quality and start and end is not None:
try:
video_info = ydlr.extract_info(url, download=False)
is_video_below_limit = video_size_below_limit(video_info, quality)
if not is_video_below_limit:
return json_response(
False,
None,
f"The file size of the video is above the limit of {VIDEO_FILE_SIZE_LIMIT_MB}MB. Please try again with a lower quality or a shorter video.",
400,
)
is_trimmed_below_limit = calculate_trimmed_file_size(video_info, quality, int(start), int(end))
if not is_trimmed_below_limit:
return json_response(
False,
None,
f"The file size of the trimmed video is going to be above the limit of {TRIMMED_FILE_SIZE_LIMIT_MB}MB. Please try again with a smaller range or lower quality.",
400,
)
elif is_trimmed_below_limit:
task = celery.send_task(
TRIM_TASK_NAME,
kwargs={
"url": url,
"quality": quality,
"start": start,
"end": end,
"ip": requester_ip,
},
)
redis_instance.set(
"celery-trim-task-" + task.id,
json.dumps({"ip": requester_ip, "task_id": task.id}),
)
return json_response(True, task.id, "Task successfully added!", 200)
except Exception as e:
print(e)
return json_response(
False,
None,
f"Something went wrong, please try again later!",
400,
)
return json_response(
False, None, "Please provide 'url, quality, start and end' data!", 400
)
@app.route("/uploadfromworker", methods=["POST"])
def upload_file():
if request.method == "POST":
secret_key = request.headers.get("secret_key")
if secret_key == UPLOAD_SECRET_KEY:
if "file" not in request.files:
return json_response(False, None, "No file part", 400)
file = request.files["file"]
if file:
file_name = secure_filename(file.filename)
save_path = os.path.join(app.config["UPLOAD_FOLDER"], file_name)
if os.path.isfile(save_path):
os.remove(save_path)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], file_name))
return json_response(True, file_name, "File uploaded successfully", 200)
else:
return json_response(False, None, "Invalid secret key", 400)
@app.route("/dlvideo/<task_id>")
def get_video(task_id):
try:
status = celery.AsyncResult(task_id, app=celery)
if status.state.lower() == "success":
return send_from_directory(
"videos/", path=status.result["data"], as_attachment=True
)
else:
abort(404)
except FileNotFoundError:
abort(404)
@app.route("/status/<task_id>")
def get_status(task_id):
status = celery.AsyncResult(task_id, app=celery)
return json_response(True, status.state, None, 200)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8000)), debug=True)