|
|
|
|
|
import gradio as gr |
|
|
from pytube import YouTube |
|
|
import moviepy.editor as mp |
|
|
import os |
|
|
import re |
|
|
import time |
|
|
|
|
|
|
|
|
def sanitize_filename(filename): |
|
|
|
|
|
sanitized = re.sub(r'[\\/*?:"<>|]', "", filename) |
|
|
|
|
|
return sanitized[:100] |
|
|
|
|
|
|
|
|
def baixar_audio_youtube(youtube_url): |
|
|
try: |
|
|
print(f"Processando URL: {youtube_url}") |
|
|
yt = YouTube(youtube_url) |
|
|
|
|
|
|
|
|
|
|
|
audio_stream = yt.streams.filter(only_audio=True).order_by('abr').desc().first() |
|
|
|
|
|
if not audio_stream: |
|
|
print("Erro: Nenhum stream de áudio encontrado.") |
|
|
return "Erro: Nenhuma stream de áudio encontrada para este vídeo.", None |
|
|
|
|
|
print(f"Título: {yt.title}") |
|
|
print(f"Stream selecionado: {audio_stream}") |
|
|
|
|
|
|
|
|
print("Baixando áudio...") |
|
|
|
|
|
temp_filename_base = sanitize_filename(yt.title) |
|
|
timestamp = int(time.time()) |
|
|
|
|
|
ext = audio_stream.mime_type.split('/')[-1] |
|
|
temp_download_path = f"{temp_filename_base}_{timestamp}.{ext}" |
|
|
|
|
|
audio_stream.download(filename=temp_download_path) |
|
|
print(f"Áudio baixado para: {temp_download_path}") |
|
|
|
|
|
|
|
|
print("Convertendo para MP3...") |
|
|
mp3_filename = f"{temp_filename_base}_{timestamp}.mp3" |
|
|
|
|
|
|
|
|
if os.path.exists(mp3_filename): |
|
|
os.remove(mp3_filename) |
|
|
|
|
|
audio_clip = mp.AudioFileClip(temp_download_path) |
|
|
|
|
|
audio_clip.write_audiofile(mp3_filename, codec='libmp3lame') |
|
|
audio_clip.close() |
|
|
|
|
|
print(f"Convertido com sucesso para: {mp3_filename}") |
|
|
|
|
|
|
|
|
try: |
|
|
os.remove(temp_download_path) |
|
|
print(f"Arquivo temporário removido: {temp_download_path}") |
|
|
except OSError as e: |
|
|
print(f"Aviso: Não foi possível remover o arquivo temporário {temp_download_path}. Erro: {e}") |
|
|
|
|
|
|
|
|
|
|
|
return f"Sucesso! Áudio '{yt.title}' pronto.", mp3_filename |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Ocorreu um erro: {e}") |
|
|
|
|
|
if 'temp_download_path' in locals() and os.path.exists(temp_download_path): |
|
|
try: |
|
|
os.remove(temp_download_path) |
|
|
except OSError: pass |
|
|
if 'mp3_filename' in locals() and os.path.exists(mp3_filename): |
|
|
try: |
|
|
os.remove(mp3_filename) |
|
|
except OSError: pass |
|
|
return f"Ocorreu um erro: {str(e)}. Verifique o link ou tente novamente.", None |
|
|
|
|
|
|
|
|
with gr.Blocks() as iface: |
|
|
gr.Markdown( |
|
|
""" |
|
|
# Baixar Áudio do YouTube em MP3 |
|
|
Cole o link (URL) de um vídeo do YouTube abaixo para extrair e baixar o áudio em formato MP3. |
|
|
""" |
|
|
) |
|
|
with gr.Row(): |
|
|
youtube_link_input = gr.Textbox(label="Link do Vídeo do YouTube", placeholder="Ex: https://www.youtube.com/watch?v=...") |
|
|
with gr.Row(): |
|
|
submit_button = gr.Button("Baixar e Converter para MP3") |
|
|
with gr.Row(): |
|
|
status_output = gr.Textbox(label="Status", interactive=False) |
|
|
with gr.Row(): |
|
|
mp3_output_file = gr.File(label="Download do Arquivo MP3") |
|
|
|
|
|
submit_button.click( |
|
|
fn=baixar_audio_youtube, |
|
|
inputs=youtube_link_input, |
|
|
outputs=[status_output, mp3_output_file] |
|
|
) |
|
|
gr.Examples( |
|
|
[["https://www.youtube.com/watch?v=dQw4w9WgXcQ"], |
|
|
["https://www.youtube.com/watch?v=kJQP7kiw5Fk"]], |
|
|
inputs=youtube_link_input |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
iface.launch() |
|
|
|
|
|
|