ProyectoFinalPython/solapas/MusicDownloader.py

69 lines
2.6 KiB
Python

import tkinter as tk
from tkinter import ttk
import threading
from pytube import YouTube
class MusicDownloader:
def __init__(self, parent):
"""
Inicializa la interfaz para descargar música de YouTube en MP3.
Args:
parent (tk.Frame): Frame donde se colocará el downloader.
"""
self.parent = parent
# Etiqueta de título
title = tk.Label(self.parent, text="Descargar Música MP3", font=("Helvetica", 14, "bold"))
title.pack(pady=10)
# Entrada para la URL
self.url_label = tk.Label(self.parent, text="URL de YouTube:")
self.url_label.pack(pady=5)
self.url_entry = tk.Entry(self.parent, width=50)
self.url_entry.pack(pady=5)
# Botón para iniciar la descarga
self.download_button = tk.Button(self.parent, text="Descargar MP3", command=self.start_download, bg="lightblue")
self.download_button.pack(pady=10)
# Barra de progreso
self.progress = ttk.Progressbar(self.parent, orient="horizontal", length=300, mode="determinate")
self.progress.pack(pady=10)
# Etiqueta de estado
self.status_label = tk.Label(self.parent, text="", font=("Helvetica", 10))
self.status_label.pack(pady=5)
def start_download(self):
"""Inicia la descarga en un hilo separado."""
url = self.url_entry.get()
if not url:
self.status_label.config(text="Por favor, ingrese una URL válida.", fg="red")
return
self.status_label.config(text="Iniciando descarga...", fg="blue")
threading.Thread(target=self.download_music, args=(url,), daemon=True).start()
def download_music(self, url):
"""Descarga el audio de YouTube como MP3."""
try:
# Inicializa la descarga
yt = YouTube(url, on_progress_callback=self.update_progress)
stream = yt.streams.filter(only_audio=True).first()
# Descargar archivo
self.status_label.config(text="Descargando...")
self.progress["value"] = 0
stream.download(filename=f"{yt.title}.mp3")
self.status_label.config(text="¡Descarga completada!", fg="green")
except Exception as e:
self.status_label.config(text=f"Error: {str(e)}", fg="red")
def update_progress(self, stream, chunk, bytes_remaining):
"""Actualiza la barra de progreso durante la descarga."""
total_size = stream.filesize
bytes_downloaded = total_size - bytes_remaining
percentage = (bytes_downloaded / total_size) * 100
self.progress["value"] = percentage