Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
-
import requests
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Limpiamos archivos viejos
|
| 11 |
-
for f in os.listdir(output_folder):
|
| 12 |
-
try: os.remove(os.path.join(output_folder, f))
|
| 13 |
-
except: pass
|
| 14 |
-
|
| 15 |
-
# OPCIÓN 1: Intento por Cobalt (API Externa - Más probable que funcione)
|
| 16 |
-
try:
|
| 17 |
-
api_url = "https://api.cobalt.tools/api/json"
|
| 18 |
-
payload = {
|
| 19 |
-
"url": url,
|
| 20 |
-
"isAudioOnly": True if mode == "Audio (MP3)" else False,
|
| 21 |
-
"aFormat": "mp3"
|
| 22 |
-
}
|
| 23 |
-
res = requests.post(api_url, json=payload, headers={"Accept": "application/json"})
|
| 24 |
-
data = res.json()
|
| 25 |
-
if data.get("url"):
|
| 26 |
-
# Devolvemos el link para que lo descargues tú directamente
|
| 27 |
-
return None, f"### ✅ ¡Enlace listo!\n[Haz clic aquí para descargar tu {mode}]({data.get('url')})"
|
| 28 |
-
except:
|
| 29 |
-
pass
|
| 30 |
-
|
| 31 |
-
# OPCIÓN 2: Intento directo (yt-dlp)
|
| 32 |
-
ydl_opts = {
|
| 33 |
-
'format': 'bestaudio/best' if mode == "Audio (MP3)" else 'best',
|
| 34 |
-
'outtmpl': f'{output_folder}/archivo_%(title)s.%(ext)s',
|
| 35 |
-
'postprocessors': [{
|
| 36 |
-
'key': 'FFmpegExtractAudio',
|
| 37 |
-
'preferredcodec': 'mp3',
|
| 38 |
-
'preferredquality': '320',
|
| 39 |
-
}] if mode == "Audio (MP3)" else [],
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
-
return
|
| 51 |
|
| 52 |
-
# Interfaz
|
| 53 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 54 |
-
gr.Markdown("#
|
|
|
|
|
|
|
| 55 |
with gr.Row():
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
btn = gr.Button("🚀
|
| 60 |
|
| 61 |
-
|
| 62 |
-
file_out = gr.File(label="Archivo para descargar (si el servidor lo logra)")
|
| 63 |
-
text_out = gr.Markdown() # Aquí aparecerá el link si el servidor falla
|
| 64 |
-
|
| 65 |
-
btn.click(fn=download_media, inputs=[url_in, mode_in], outputs=[file_out, text_out])
|
| 66 |
|
| 67 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from moviepy.editor import VideoFileClip
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
+
def convert_to_mp3(video_file):
|
| 6 |
+
if video_file is None:
|
| 7 |
+
return None
|
| 8 |
+
|
| 9 |
+
# Creamos carpeta temporal
|
| 10 |
+
output_path = "audio_hd.mp3"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
+
# Cargamos el video que subiste
|
| 14 |
+
video = VideoFileClip(video_file.name)
|
| 15 |
+
|
| 16 |
+
# Extraemos el audio con calidad 320kbps
|
| 17 |
+
# 'bitrate' asegura que el sonido sea nítido
|
| 18 |
+
video.audio.write_audiofile(output_path, bitrate="320k")
|
| 19 |
+
|
| 20 |
+
# Cerramos el archivo para liberar memoria
|
| 21 |
+
video.close()
|
| 22 |
+
|
| 23 |
+
return output_path
|
| 24 |
except Exception as e:
|
| 25 |
+
return f"Error en la conversión: {str(e)}"
|
| 26 |
|
| 27 |
+
# Interfaz limpia y rápida
|
| 28 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 29 |
+
gr.Markdown("# 🎵 Convertidor MP4 a MP3 HD (320kbps)")
|
| 30 |
+
gr.Markdown("Sube tu video descargado y obtén un audio de máxima calidad sin bloqueos de red.")
|
| 31 |
+
|
| 32 |
with gr.Row():
|
| 33 |
+
video_input = gr.File(label="Sube tu video (MP4, MKV, AVI)", file_types=["video"])
|
| 34 |
+
audio_output = gr.File(label="Descargar MP3 de Alta Calidad")
|
| 35 |
|
| 36 |
+
btn = gr.Button("🚀 Convertir a MP3 HD", variant="primary")
|
| 37 |
|
| 38 |
+
btn.click(fn=convert_to_mp3, inputs=video_input, outputs=audio_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
demo.launch()
|