MizunoBrasil

Video Download Youtube

Jul 26th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.36 KB | None | 0 0
  1. import PySimpleGUI as sg
  2. import subprocess
  3. import ctypes
  4. import json
  5. import webbrowser
  6. import os
  7. import sys
  8.  
  9. # Obtém o identificador da janela do console
  10. hwnd = ctypes.windll.kernel32.GetConsoleWindow()
  11.  
  12. # Oculta a janela do console
  13. ctypes.windll.user32.ShowWindow(hwnd, 0)  # 0 representa o comando para ocultar a janela
  14.  
  15. # Caminho padrão da pasta de destino
  16. DEFAULT_DESTINATION_PATH = ""
  17.  
  18. def save_destination_path(destination_path):
  19.     # Salva o caminho da pasta de destino em um arquivo JSON
  20.     with open("destination_path.json", "w") as file:
  21.         json.dump({"destination_path": destination_path}, file)
  22.  
  23. def load_destination_path():
  24.     # Carrega o caminho da pasta de destino do arquivo JSON
  25.     try:
  26.         with open("destination_path.json", "r") as file:
  27.             data = json.load(file)
  28.             return data["destination_path"]
  29.     except FileNotFoundError:
  30.         return DEFAULT_DESTINATION_PATH
  31.  
  32. def configure_destination_folder():
  33.     # Abre uma janela de diálogo para escolher a pasta de destino
  34.     folder_selected = sg.popup_get_folder("Escolha a pasta de destino")
  35.     if folder_selected:
  36.         save_destination_path(folder_selected)
  37.         destination_path = load_destination_path()
  38.         window["-DESTINATION-"].update("" + destination_path)
  39.         sg.popup("Configuração de Pasta", "A pasta de destino foi configurada com sucesso.")
  40.  
  41. def open_destination_folder():
  42.     # Abre a pasta de destino configurada usando o Windows Explorer
  43.     destination_path = load_destination_path()
  44.     if destination_path:
  45.         webbrowser.open(destination_path)
  46.  
  47. def convert_to_mp3(file_path):
  48.     try:
  49.         # Obtém o caminho da pasta de destino configurada
  50.         destination_path = load_destination_path()
  51.  
  52.         # Obtém o nome do arquivo sem a extensão
  53.         file_name = os.path.splitext(os.path.basename(file_path))[0]
  54.  
  55.         # Define o caminho de saída para o arquivo MP3
  56.         mp3_output_path = os.path.join(destination_path, f"{file_name}.mp3")
  57.  
  58.         # Executa o comando ffmpeg para extrair a faixa de áudio e convertê-la para MP3
  59.         comando = ['ffmpeg', '-i', file_path, mp3_output_path]
  60.         subprocess.call(comando)
  61.  
  62.         # Exibe uma mensagem de sucesso
  63.         sg.popup("Conversão Concluída", "Arquivo convertido para MP3 com sucesso.")
  64.     except Exception as e:
  65.         sg.popup_error("Erro", str(e))
  66.  
  67. def download_video(video_url):
  68.     if not video_url:
  69.         sg.popup("Aviso", "Por favor, informe a URL do vídeo do YouTube.")
  70.         return
  71.  
  72.     try:
  73.         # Carrega o caminho da pasta de destino
  74.         destination_path = load_destination_path()
  75.  
  76.         if destination_path:
  77.             # Executa o comando do yt-dlp para baixar o vídeo com o caminho da pasta de destino configurado
  78.             comando = ['yt-dlp', '--output', os.path.join(destination_path, '%(title)s.%(ext)s'), video_url]
  79.             subprocess.call(comando)
  80.  
  81.             # Exibe a mensagem de sucesso
  82.             sg.popup("Download Concluído", "O vídeo foi baixado com sucesso.")
  83.         else:
  84.             sg.popup_warning("Aviso", "Nenhuma pasta de destino configurada.")
  85.     except Exception as e:
  86.         sg.popup_error("Erro", str(e))
  87.  
  88. def rename_mp4(file_path):
  89.     try:
  90.         file_name = os.path.splitext(file_path)[0]
  91.         new_file_name = file_name + ".mp4"
  92.         os.rename(file_path, new_file_name)
  93.         sg.popup("Sucesso", "Arquivo renomeado para MP4 com sucesso.")
  94.     except Exception as e:
  95.         sg.popup_error("Erro", str(e))
  96.  
  97. # Cria a interface gráfica
  98. sg.theme("Default1")
  99.  
  100. # Obtem o caminho da pasta de destino atual
  101. destination_path = load_destination_path()
  102.  
  103. layout = [
  104.     [sg.Text("Informe a URL do vídeo: (Youtube, PornHub, Xvideos)")],
  105.     [sg.InputText(size=(80, 1), key="-URL-"), sg.Button("Baixar vídeo")],
  106.     [sg.Button("Configurar Pasta de Destino"), sg.Text("", key="-LABEL-", text_color="black"),
  107.      sg.Text(destination_path, key="-DESTINATION-", enable_events=True, tooltip="Clique para abrir a pasta", text_color="blue")],
  108.     [sg.Button("Converter para MP3"), sg.Button("Renomear para MP4")]
  109. ]
  110.  
  111. window = sg.Window("Vídeo Downloader", layout, finalize=True)
  112.  
  113. # Verifica se o arquivo JSON existe e carrega o caminho da pasta de destino se existir
  114. if os.path.isfile("destination_path.json"):
  115.     destination_path = load_destination_path()
  116.     window["-DESTINATION-"].update("" + destination_path)
  117.  
  118. # Loop principal do programa
  119. while True:
  120.     event, values = window.read()
  121.     if event == sg.WINDOW_CLOSED:
  122.         break
  123.     elif event == "Baixar vídeo":
  124.         video_url = values["-URL-"]
  125.         download_video(video_url)
  126.     elif event == "Configurar Pasta de Destino":
  127.         configure_destination_folder()
  128.     elif event == "Converter para MP3":
  129.         file_path = sg.popup_get_file("Selecione um arquivo de vídeo para converter para MP3", file_types=(("Arquivos de Vídeo", "*.mkv *.webm"),))
  130.         if file_path:
  131.             convert_to_mp3(file_path)
  132.     elif event == "Renomear para MP4":
  133.         file_path = sg.popup_get_file("Selecione um arquivo de vídeo para renomear para MP4", file_types=(("Arquivos de Vídeo", "*.mkv *.webm"),))
  134.         if file_path:
  135.             rename_mp4(file_path)
  136.     elif event == "-DESTINATION-":  # Callback para o evento de clique na label
  137.         open_destination_folder()
  138.  
  139. window.close()
  140.  
Add Comment
Please, Sign In to add comment