-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (56 loc) · 2.02 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
import os
import re
import threading
from flask import Flask, render_template, request, send_file, url_for
from yt_dlp import YoutubeDL
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def sanitize_filename(filename):
return re.sub(r'[\/:*?"<>|]', '_', filename)
def delete_file_after_delay(file_path, delay_seconds):
def delete_file():
if os.path.exists(file_path):
os.remove(file_path)
print(f"Deleted file: {file_path}")
timer = threading.Timer(delay_seconds, delete_file)
timer.start()
@app.route('/download', methods=['POST'])
def download():
video_url = request.form['video_url']
download_format = request.form['download_format']
if download_format == 'mp3':
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '0',
}],
'outtmpl': '%(title)s.%(ext)s',
}
file_extension = 'mp3'
elif download_format == 'mp4':
ydl_opts = {
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'merge_output_format': 'mp4',
'postprocessors': [{
'key': 'EmbedThumbnail',
}],
'outtmpl': '%(title)s.%(ext)s',
}
file_extension = 'mp4'
else:
return "Invalid download format"
with YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(video_url, download=True)
downloaded_file = ydl.prepare_filename(info)
base_filename = os.path.splitext(downloaded_file)[0]
final_filename = f"{base_filename}.{file_extension}"
final_filename_sanitized = sanitize_filename(final_filename)
os.rename(final_filename, final_filename_sanitized)
delete_file_after_delay(final_filename_sanitized, 300)
return send_file(final_filename_sanitized, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)