-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
47 lines (39 loc) · 1.47 KB
/
app.py
File metadata and controls
47 lines (39 loc) · 1.47 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
from flask import Flask, request, jsonify, render_template
import os
import yt_dlp
app = Flask(__name__)
DOWNLOAD_FOLDER = os.path.join(os.getcwd(), 'downloads')
# Ensure the download folder exists
os.makedirs(DOWNLOAD_FOLDER, exist_ok=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/download', methods=['POST'])
def download_media():
data = request.get_json()
url = data.get('url')
download_type = data.get('type', 'video') # "audio" or "video"
if not url:
return jsonify({'status': 'error', 'message': 'No URL provided.'}), 400
# Select options based on download type, without any merging or postprocessing.
if download_type == 'audio':
ydl_opts = {
'format': 'bestaudio[ext=m4a]/bestaudio',
'outtmpl': os.path.join(DOWNLOAD_FOLDER, '%(title)s.%(ext)s'),
'noplaylist': True,
}
else: # video
ydl_opts = {
'format': 'best[ext=mp4]/best',
'outtmpl': os.path.join(DOWNLOAD_FOLDER, '%(title)s.%(ext)s'),
'noplaylist': True,
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info)
return jsonify({'status': 'success', 'filename': os.path.basename(filename)})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)