Spaces:
Build error
Build error
Update srv.py
Browse files
srv.py
CHANGED
|
@@ -10,14 +10,11 @@
|
|
| 10 |
------------------------------------------------------------
|
| 11 |
"""
|
| 12 |
|
| 13 |
-
import
|
| 14 |
-
import tempfile
|
| 15 |
-
import uuid
|
| 16 |
-
from flask import Flask, request, jsonify, send_file ,render_template
|
| 17 |
from yt_dlp import YoutubeDL
|
|
|
|
| 18 |
|
| 19 |
app = Flask(__name__)
|
| 20 |
-
DOWNLOAD_FOLDER = tempfile.gettempdir()
|
| 21 |
|
| 22 |
@app.route('/')
|
| 23 |
def home():
|
|
@@ -48,49 +45,38 @@ def get_info():
|
|
| 48 |
except Exception as e:
|
| 49 |
return jsonify({'error': str(e)}), 500
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
@app.route('/download', methods=['POST'])
|
| 54 |
def download_media():
|
| 55 |
data = request.json
|
| 56 |
url = data.get('url')
|
| 57 |
-
format_type = data.get('format_type', 'mp3')
|
| 58 |
-
quality = data.get('quality', 'best')
|
| 59 |
|
| 60 |
if not url:
|
| 61 |
return jsonify({'error': 'URL is required'}), 400
|
| 62 |
|
| 63 |
try:
|
| 64 |
-
unique_id = str(uuid.uuid4())
|
| 65 |
-
file_ext = "mp3" if format_type == "mp3" else "mp4"
|
| 66 |
-
file_name = os.path.join(DOWNLOAD_FOLDER, f"{unique_id}.{file_ext}")
|
| 67 |
-
|
| 68 |
if format_type == 'mp4':
|
| 69 |
quality_map = {
|
| 70 |
-
'144': 'bv*[height=144]+ba/b[height=144]',
|
| 71 |
-
'240': 'bv*[height=240]+ba/b[height=240]',
|
| 72 |
-
'360': 'bv*[height=360]+ba/b[height=360]',
|
| 73 |
-
'480': 'bv*[height=480]+ba/b[height=480]',
|
| 74 |
-
'720': 'bv*[height=720]+ba/b[height=720]',
|
| 75 |
-
'1080': 'bv*[height=1080]+ba/b[height=1080]',
|
| 76 |
-
'best': 'bv+ba/
|
| 77 |
}
|
|
|
|
| 78 |
ydl_opts = {
|
| 79 |
-
'format': quality_map.get(quality, 'best'),
|
| 80 |
-
'outtmpl': file_name,
|
| 81 |
-
'noprogress': True,
|
| 82 |
'merge_output_format': 'mp4',
|
| 83 |
-
'
|
| 84 |
-
'external_downloader': 'aria2c',
|
| 85 |
'cookiefile': 'www.youtube.com_cookies.txt'
|
| 86 |
}
|
| 87 |
-
else:
|
| 88 |
ydl_opts = {
|
| 89 |
-
|
| 90 |
-
'outtmpl':
|
| 91 |
-
'noprogress': True,
|
| 92 |
-
'nocheckcertificate': True,
|
| 93 |
-
'external_downloader': 'aria2c',
|
| 94 |
'cookiefile': 'www.youtube.com_cookies.txt',
|
| 95 |
'postprocessors': [{
|
| 96 |
'key': 'FFmpegExtractAudio',
|
|
@@ -98,18 +84,23 @@ def download_media():
|
|
| 98 |
'preferredquality': '64',
|
| 99 |
}],
|
| 100 |
}
|
| 101 |
-
|
| 102 |
with YoutubeDL(ydl_opts) as ydl:
|
| 103 |
-
ydl.extract_info(url, download=True)
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
return send_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
return jsonify({'error': str(e)}), 500
|
| 109 |
|
| 110 |
finally:
|
| 111 |
-
if os.path.exists(file_name):
|
| 112 |
os.remove(file_name)
|
| 113 |
|
| 114 |
if __name__ == '__main__':
|
| 115 |
-
app.run(host='0.0.0.0', port=7860, debug=True,
|
|
|
|
| 10 |
------------------------------------------------------------
|
| 11 |
"""
|
| 12 |
|
| 13 |
+
from flask import Flask, request, jsonify, send_file, render_template
|
|
|
|
|
|
|
|
|
|
| 14 |
from yt_dlp import YoutubeDL
|
| 15 |
+
import os
|
| 16 |
|
| 17 |
app = Flask(__name__)
|
|
|
|
| 18 |
|
| 19 |
@app.route('/')
|
| 20 |
def home():
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return jsonify({'error': str(e)}), 500
|
| 47 |
|
|
|
|
|
|
|
| 48 |
@app.route('/download', methods=['POST'])
|
| 49 |
def download_media():
|
| 50 |
data = request.json
|
| 51 |
url = data.get('url')
|
| 52 |
+
format_type = data.get('format_type', 'mp3')
|
| 53 |
+
quality = str(data.get('quality', 'best'))
|
| 54 |
|
| 55 |
if not url:
|
| 56 |
return jsonify({'error': 'URL is required'}), 400
|
| 57 |
|
| 58 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if format_type == 'mp4':
|
| 60 |
quality_map = {
|
| 61 |
+
'144': 'bv*[height=144][ext=mp4]+ba[ext=m4a]/b[height=144][ext=mp4]',
|
| 62 |
+
'240': 'bv*[height=240][ext=mp4]+ba[ext=m4a]/b[height=240][ext=mp4]',
|
| 63 |
+
'360': 'bv*[height=360][ext=mp4]+ba[ext=m4a]/b[height=360][ext=mp4]',
|
| 64 |
+
'480': 'bv*[height=480][ext=mp4]+ba[ext=m4a]/b[height=480][ext=mp4]',
|
| 65 |
+
'720': 'bv*[height=720][ext=mp4]+ba[ext=m4a]/b[height=720][ext=mp4]',
|
| 66 |
+
'1080': 'bv*[height=1080][ext=mp4]+ba[ext=m4a]/b[height=1080][ext=mp4]',
|
| 67 |
+
'best': 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4]'
|
| 68 |
}
|
| 69 |
+
|
| 70 |
ydl_opts = {
|
| 71 |
+
'format': quality_map.get(quality, quality_map['best']),
|
|
|
|
|
|
|
| 72 |
'merge_output_format': 'mp4',
|
| 73 |
+
'outtmpl': '%(title)s.%(ext)s',
|
|
|
|
| 74 |
'cookiefile': 'www.youtube.com_cookies.txt'
|
| 75 |
}
|
| 76 |
+
else:
|
| 77 |
ydl_opts = {
|
| 78 |
+
'format': 'bestaudio',
|
| 79 |
+
'outtmpl': '%(title)s.%(ext)s',
|
|
|
|
|
|
|
|
|
|
| 80 |
'cookiefile': 'www.youtube.com_cookies.txt',
|
| 81 |
'postprocessors': [{
|
| 82 |
'key': 'FFmpegExtractAudio',
|
|
|
|
| 84 |
'preferredquality': '64',
|
| 85 |
}],
|
| 86 |
}
|
|
|
|
| 87 |
with YoutubeDL(ydl_opts) as ydl:
|
| 88 |
+
info = ydl.extract_info(url, download=True)
|
| 89 |
+
file_ext = "mp4" if format_type == "mp4" else "mp3"
|
| 90 |
+
file_name = ydl.prepare_filename(info).rsplit(".", 1)[0] + f".{file_ext}"
|
| 91 |
|
| 92 |
+
return send_file(
|
| 93 |
+
file_name,
|
| 94 |
+
as_attachment=True,
|
| 95 |
+
download_name=os.path.basename(file_name)
|
| 96 |
+
)
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
return jsonify({'error': str(e)}), 500
|
| 100 |
|
| 101 |
finally:
|
| 102 |
+
if 'file_name' in locals() and os.path.exists(file_name):
|
| 103 |
os.remove(file_name)
|
| 104 |
|
| 105 |
if __name__ == '__main__':
|
| 106 |
+
app.run(host='0.0.0.0', port=7860, debug=True ,threaded=True)
|