-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (42 loc) · 1.64 KB
/
Copy pathapp.py
File metadata and controls
51 lines (42 loc) · 1.64 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
48
49
50
51
from flask import Flask, render_template, request, send_file
import os
from yt_dlp import YoutubeDL
app = Flask(__name__)
DOWNLOAD_FOLDER = "downloads"
if not os.path.exists(DOWNLOAD_FOLDER):
os.mkdir(DOWNLOAD_FOLDER)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/download", methods=["POST"])
def download():
url = request.form.get("url")
format_choice = request.form.get("format")
quality_choice = request.form.get("quality")
audio_bitrate = request.form.get("audio_bitrate") # New field for audio bitrate
if not url:
return "Error: URL is required!", 400
options = {
'outtmpl': os.path.join(DOWNLOAD_FOLDER, '%(title)s.%(ext)s'),
'ffmpeg_location': r'C:\ffmpeg\bin', # Adjust this path as necessary
}
if format_choice == "mp3":
options['format'] = f'bestaudio[abr<={audio_bitrate}]' # Use selected audio bitrate
options['postprocessors'] = [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': audio_bitrate,
}]
else:
options['format'] = f'bestvideo[height<={quality_choice}]+bestaudio/best'
try:
with YoutubeDL(options) as ydl:
info = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info)
if format_choice == "mp3":
filename = filename.replace(".webm", ".mp3")
return send_file(filename, as_attachment=True)
except Exception as e:
return f"Error: {e}", 500
if __name__ == "__main__":
app.run(debug=True)