Skip to content

Instantly share code, notes, and snippets.

@Nusab19
Created December 23, 2024 16:06
Show Gist options
  • Save Nusab19/a6fa8266f06fd2b599ea13f2d07fb6c7 to your computer and use it in GitHub Desktop.
Save Nusab19/a6fa8266f06fd2b599ea13f2d07fb6c7 to your computer and use it in GitHub Desktop.
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
import os
import yt_dlp
import glob
app = Flask(__name__)
CORS(app)
DOWNLOAD_DIR = os.path.join(os.getcwd(), "downloads")
COOKIES_FILE = os.path.join(os.getcwd(), "cookies.txt") # Path to cookies file
if not os.path.exists(DOWNLOAD_DIR):
os.makedirs(DOWNLOAD_DIR)
@app.route('/')
def home():
return jsonify({'message': 'Welcome to the YouTube Downloader API'})
@app.route('/download', methods=['POST'])
def download_video():
if not os.path.exists(COOKIES_FILE):
return jsonify({'error': 'Cookies file not found'}), 400
data = request.json
video_url = data.get('url')
resolution = data.get('resolution', 'best')
format_type = data.get('format', 'mp4')
if not video_url:
return jsonify({'error': 'YouTube URL is required'}), 400
print(f"Downloading {video_url} with cookies from {COOKIES_FILE}")
if format_type == 'mp3':
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': f"{DOWNLOAD_DIR}/%(title)s.%(ext)s",
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'ffmpeg_location': '/usr/bin/ffmpeg',
'cookiefile': COOKIES_FILE, # Use cookies file
}
else:
ydl_opts = {
'format': f'bestvideo[height<={resolution}]+bestaudio/best',
'outtmpl': f"{DOWNLOAD_DIR}/%(title)s.%(ext)s",
'merge_output_format': format_type,
'ffmpeg_location': '/usr/bin/ffmpeg',
'cookiefile': COOKIES_FILE, # Use cookies file
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
downloaded_files = glob.glob(os.path.join(DOWNLOAD_DIR, f"*.{format_type}"))
if format_type == 'mp3':
downloaded_files += glob.glob(os.path.join(DOWNLOAD_DIR, "*.mp3"))
if downloaded_files:
file_path = downloaded_files[0]
file_name = os.path.basename(file_path)
return jsonify({'file_name': file_name}), 200
else:
return jsonify({'error': 'File not found after download'}), 500
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/download-file/<file_name>', methods=['GET'])
def serve_file(file_name):
file_path = os.path.join(DOWNLOAD_DIR, file_name)
if os.path.exists(file_path):
return send_file(file_path, as_attachment=True, download_name=file_name)
else:
return jsonify({'error': 'File not found'}), 404
if __name__ == '__main__':
app.run(debug=False)
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by Cookie-Editor
#HttpOnly_.youtube.com TRUE / TRUE 1749994348 __Secure-3PSID g.a000rQimtOGEyXh958STJPC6DUlmWPHUIzjit-cddUpDobSBQAsptJF_HHW3eMu9Zsh9BmgjkAACgYKAeUSARYSFQHGX2MiUXlURoV3cG3xzKe4LDXrrhoVAUF8yKpGC1tWJlwBoOvA_nt1A_6z0076
#HttpOnly_.youtube.com TRUE / TRUE 1750521505 __Secure-1PSIDTS sidts-CjIB7wV3sWB44QzxX8zwxNWVbpZO6vjjS3aQVqhn_hN31QeNgE6tDcX_5XE7cl7za5SWDxAA
.youtube.com TRUE / TRUE 1749994348 SAPISID L4hGd7WD6-nY8H0A/A4r0Aa5kcRNozt0zR
#HttpOnly_.youtube.com TRUE / TRUE 1750521800 __Secure-1PSIDCC AKEyXzXzhApa05WnaWMHR88vo2XigcIkUbcRQ3d6kKMkZnhatJWfhWleL-hSI9hKKNDkS1r9D8A
#HttpOnly_.youtube.com TRUE / TRUE 1749994348 SSID AP0Zuy7zmPRUPyNXd
.youtube.com TRUE / TRUE 1735056231 wide 1
.youtube.com TRUE / TRUE 1749994348 __Secure-1PAPISID L4hGd7WD6-nY8H0A/A4r0Aa5kcRNozt0zR
#HttpOnly_.youtube.com TRUE / TRUE 1749994348 __Secure-1PSID g.a000rQimtOGEyXh958STJPC6DUlmWPHUIzjit-cddUpDobSBQAspyOybZvhw2dNKCvoGH30hsgACgYKAQUSARYSFQHGX2MiHpLKcCYf852XwJjDuuUb0RoVAUF8yKqjVAvr2UGeVeGVHfQzNORz0076
.youtube.com TRUE / TRUE 1749994348 __Secure-3PAPISID L4hGd7WD6-nY8H0A/A4r0Aa5kcRNozt0zR
#HttpOnly_.youtube.com TRUE / TRUE 1750521800 __Secure-3PSIDCC AKEyXzViTT-SCRUVWnC82DjGvDIN8SxlkE3INTF1J6gPoVzL7qc88kl8Onm2q8_HBOWv55blxI0
#HttpOnly_.youtube.com TRUE / TRUE 1750521505 __Secure-3PSIDTS sidts-CjIB7wV3sWB44QzxX8zwxNWVbpZO6vjjS3aQVqhn_hN31QeNgE6tDcX_5XE7cl7za5SWDxAA
#HttpOnly_.youtube.com TRUE / TRUE 1750521699 LOGIN_INFO AFmmF2swRAIge-tdigR9pQJMM2-ZE4R5zJUo-3GnAg0XBlcDHYL9f2gCIEzWrATr3Eb2bSbmOfFhGjyfcRUngDgS25ggRJLjV0PP:QUQ3MjNmeXR4a3Jua1c0SjlzTzNfc09LbTJGR2w3M01SV01MaTVrN213MzlCMGVEeTdtcmFaTWtocjBkcnkwbDlwbFVHSmlsTnJpZjd1dlZZTnBZeHlfcXBqSjZOcDFhTFVDWnE1RXFqVnBWMGhHQ2gwckM2eTFiQjFkRnBRSjZYUF95WkN0NW1Fc09FRmdTVUVpTTFweFZDNG5aTzVlX0Nn
.youtube.com TRUE / TRUE 1735574596 PREF tz=Asia.Dhaka&f4=4000000&f6=40000000&f5=30000&f7=100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment