2024-11-21 22:44:27 -05:00

59 lines
1.6 KiB
Python

from flask import Flask, request, send_file
import yt_dlp
import os
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
return send_file('index.html')
@app.route('/download', methods=['GET'])
def download_video():
try:
video_url = request.args.get('url')
download_type = request.args.get('type', 'video') # Default to video if not specified
if not video_url:
return "Please provide a YouTube URL", 400
# Configure yt-dlp options based on download type
if download_type == 'audio':
try:
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': '%(title)s.%(ext)s',
}
except Exception as e:
print(f"Error: {str(e)}")
return str(e), 500
else: # video
ydl_opts = {
'format': 'best',
'outtmpl': '%(title)s.%(ext)s',
}
# Download the video/audio
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(video_url, download=True)
return "Download completed", 200
except Exception as e:
print(f"Error: {str(e)}")
return str(e), 500
@app.route('/ping')
def ping():
return 'pong'
if __name__ == '__main__':
app.run(debug=True, port=5000)