Youtube_Downloader_Shdze / youtube_extractor.py
SheilaDesanze's picture
Update youtube_extractor.py
7058cae verified
import yt_dlp
class YouTubeExtractor:
def __init__(self, ydl_opts=None):
self.ydl_opts = ydl_opts or {
'quiet': True,
'no_warnings': True,
'no_color': True,
'youtube_include_dash_manifest': False,
'youtube_include_hls_manifest': False,
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
def extract_info(self, youtube_url):
try:
with yt_dlp.YoutubeDL(self.ydl_opts) as ydl:
info = ydl.extract_info(youtube_url, download=False)
metadata = {
"Titulo del video": info.get('title', 'N/A'),
"Canal": info.get('channel', 'N/A'),
"Fecha de carga": info.get('upload_date', 'N/A'),
"Cantidad de visitas": info.get('view_count', 'N/A'),
"Duracion (segundos)": info.get('duration', 'N/A'),
}
formats = info.get('formats', [])
# Solo vídeo (resolución más alta)
best_video_only = max(
(f for f in formats if f.get('vcodec') != 'none' and f.get('acodec') == 'none'),
key=lambda x: x.get('height', 0),
default=None
)
# Solo audio (tasa de bits más alta)
best_audio_only = max(
(f for f in formats if f.get('acodec') != 'none' and f.get('vcodec') == 'none'),
key=lambda x: x.get('abr', 0),
default=None
)
# Vídeo + audio combinados (máxima resolución)
best_combined = max(
(f for f in formats if f.get('vcodec') != 'none' and f.get('acodec') != 'none'),
key=lambda x: (x.get('ext') == 'mp4', x.get('height', 0)),
default=None
)
return metadata, best_video_only, best_audio_only, best_combined
except Exception as e:
return None, None, None, None
def format_output(self, metadata, best_video_only, best_audio_only, best_combined):
if not metadata:
return "Se produjo un error: no se pueden extraer metadatos.", "Enlace de descarga no encontrado."
metadata_str = "\n".join([f"{k}: {v}" for k, v in metadata.items()])
video_only_link = f'<a href="{best_video_only["url"]}" target="_blank">Descargar_solo_videos</a>' if best_video_only else ""
audio_only_link = f'<a href="{best_audio_only["url"]}" target="_blank">Descargar_solo_audio</a>' if best_audio_only else ""
combined_link = f'<a href="{best_combined["url"]}" target="_blank">Descargar_audioyvideo</a>' if best_combined else ""
download_links = f"{combined_link}<br>{audio_only_link}<br>{video_only_link}<br>" if any([combined_link, audio_only_link, video_only_link]) else "Enlace de descarga no encontrado."
return metadata_str, download_links