vaniconnect-api / yt_down.py
VaniConnect Pipeline Bot
Auto-deployed fresh snapshot from GitHub
ba4ad1b
Raw
History Blame Contribute Delete
2.09 kB
import yt_dlp
import os
import uuid
def download_youtube_video(video_url, output_folder="downloads", quality="720p"):
try:
os.makedirs(output_folder, exist_ok=True)
random_id = uuid.uuid4().hex[:8]
is_audio = (quality == "audio")
ext = "mp3" if is_audio else "mp4"
safe_filename = f"yt_{random_id}.{ext}"
final_path = os.path.join(output_folder, safe_filename)
# Quality Mapping
if quality == "best":
format_str = 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best'
elif quality == "720p":
format_str = 'bestvideo[height<=720][ext=mp4]+bestaudio[ext=m4a]/best[height<=720][ext=mp4]/best'
elif quality == "480p":
format_str = 'bestvideo[height<=480][ext=mp4]+bestaudio[ext=m4a]/best[height<=480][ext=mp4]/best'
elif quality == "audio":
format_str = 'bestaudio/best'
else:
format_str = 'best[ext=mp4]/best'
ydl_opts = {
'format': format_str,
'outtmpl': final_path,
'quiet': False,
'no_warnings': True,
'source_address': '0.0.0.0'
}
if is_audio:
ydl_opts['postprocessors'] = [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}]
else:
ydl_opts['merge_output_format'] = 'mp4'
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
# 🌟 THE UPGRADE: We extract info AND download at the same time!
info_dict = ydl.extract_info(video_url, download=True)
# Grab the metadata to send to React
title = info_dict.get('title', 'Unknown Media')
thumbnail = info_dict.get('thumbnail', '')
# 🌟 Return all 3 pieces of data!
return safe_filename, title, thumbnail
except Exception as e:
print(f"🚨 Download Error: {e}")
return None, None, None