Advertisement
MizunoBrasil

Conversor MP4 para MP3 e Vice Versa (Necessita do FFMPEG) Versão Final (IMPORTANTE)

May 11th, 2024 (edited)
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.78 KB | None | 0 0
  1. ************************************************************
  2. ** Necessário ter instalado o FFMPEG para funcionar     ****
  3. ------------------------------------------------------------
  4. Arquivo .bat:
  5. @echo off
  6. echo Instalando o FFmpeg...
  7. winget install ffmpeg
  8. echo.
  9. echo O FFmpeg foi instalado com sucesso!
  10. pause
  11. echo Pressione uma tecla para fechar...
  12. pause > nul
  13. ---------------------------------------------------------------------
  14. ** MP3 para MP4 permite adicionar imagem de fundo (poster/thumbnail) *
  15. *********************************************************************
  16.  
  17. import tkinter as tk
  18. from tkinter import filedialog, messagebox, ttk
  19. import subprocess
  20. import threading
  21. import os
  22.  
  23. class FileConverterApp:
  24.     def __init__(self, master):
  25.         self.master = master
  26.         master.title("Conversor de Arquivos")
  27.  
  28.         # Criar o menu
  29.         menu_bar = tk.Menu(master)
  30.         file_menu = tk.Menu(menu_bar, tearoff=0)
  31.         file_menu.add_command(label="Sair", command=master.quit)
  32.         menu_bar.add_cascade(label="Arquivo", menu=file_menu)
  33.         help_menu = tk.Menu(menu_bar, tearoff=0)
  34.         help_menu.add_command(label="Sobre", command=self.show_about)
  35.         menu_bar.add_cascade(label="Ajuda", menu=help_menu)
  36.         master.config(menu=menu_bar)
  37.  
  38.         # Configurações da janela
  39.         window_size = "400x300"
  40.         master.geometry(window_size)
  41.         screen_width = master.winfo_screenwidth()
  42.         screen_height = master.winfo_screenheight()
  43.         x = int((screen_width / 2) - (400 / 2))
  44.         y = int((screen_height / 2) - (300 / 2))
  45.         master.geometry(f"{window_size}+{x}+{y}")
  46.  
  47.         # Botões para escolher tipo de conversão
  48.         self.mp4_to_mp3_button = tk.Button(master, text="Converter MP4 para MP3", command=self.convert_mp4_to_mp3)
  49.         self.mp4_to_mp3_button.pack(pady=10)
  50.  
  51.         self.mp3_to_mp4_button = tk.Button(master, text="Converter MP3 para MP4", command=self.convert_mp3_to_mp4)
  52.         self.mp3_to_mp4_button.pack(pady=10)
  53.  
  54.         # Barra de progresso
  55.         self.progress_bar = ttk.Progressbar(master, mode='indeterminate', length=200)
  56.  
  57.         # Caixa de texto para exibir o caminho do arquivo convertido
  58.         self.output_path_entry = tk.Entry(master, width=50)
  59.         self.output_path_entry.pack(pady=10)
  60.         self.output_path_entry.config(state='readonly', readonlybackground='white')
  61.  
  62.         # Adicionar menu de contexto à caixa de texto
  63.         self.context_menu = tk.Menu(master, tearoff=0)
  64.         self.context_menu.add_command(label="Copiar", command=self.copy_to_clipboard)
  65.         self.output_path_entry.bind("<Button-3>", self.show_context_menu)
  66.  
  67.         # Label para mensagem final
  68.         self.result_label = tk.Label(master, text="")
  69.         self.result_label.pack()
  70.  
  71.         # Rodapé com a versão do programa
  72.         self.footer_label = tk.Label(master, text="Conversor MP3/MP4 - v5", anchor="w")
  73.         self.footer_label.pack(side="bottom", fill="x")
  74.  
  75.     def convert_mp4_to_mp3(self):
  76.         mp4_file = filedialog.askopenfilename(filetypes=[("MP4 Files", "*.mp4")])
  77.         if not mp4_file:
  78.             messagebox.showwarning('Aviso', 'Seleção de arquivo MP4 cancelada.')
  79.             return
  80.  
  81.         # Obter apenas o nome do arquivo sem extensão
  82.         output_file_name = os.path.splitext(os.path.basename(mp4_file))[0]
  83.        
  84.         output_file = filedialog.asksaveasfilename(defaultextension=".mp3", filetypes=[("MP3 Files", "*.mp3")], initialfile=output_file_name)
  85.         if not output_file:
  86.             messagebox.showwarning('Aviso', 'Conversão cancelada.')
  87.             return
  88.  
  89.         command = [
  90.             'ffmpeg',
  91.             '-i', mp4_file,
  92.             '-vn',
  93.             '-acodec', 'libmp3lame',
  94.             '-q:a', '4',
  95.             f"{output_file}"
  96.         ]
  97.  
  98.         self.progress_bar.pack(pady=10)
  99.         self.progress_bar.start(10)
  100.        
  101.         threading.Thread(target=self.run_ffmpeg_command, args=(command, output_file)).start()
  102.  
  103.     def convert_mp3_to_mp4(self):
  104.         mp3_file = filedialog.askopenfilename(filetypes=[("MP3 Files", "*.mp3")])
  105.         if not mp3_file:
  106.             messagebox.showwarning('Aviso', 'Seleção de arquivo MP3 cancelada.')
  107.             return
  108.  
  109.         # Obter apenas o nome do arquivo sem extensão
  110.         output_file_name = os.path.splitext(os.path.basename(mp3_file))[0]
  111.        
  112.         output_file = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("MP4 Files", "*.mp4")], initialfile=output_file_name)
  113.         if not output_file:
  114.             messagebox.showwarning('Aviso', 'Conversão cancelada.')
  115.             return
  116.  
  117.         image_file = filedialog.askopenfilename(filetypes=[("Image Files", "*.jpg;*.jpeg;*.png")])
  118.         if not image_file:
  119.             messagebox.showwarning('Aviso', 'Seleção de imagem cancelada.')
  120.             return
  121.  
  122.         command = [
  123.             'ffmpeg',
  124.             '-loop', '1',
  125.             '-framerate', '2',
  126.             '-i', image_file,
  127.             '-i', mp3_file,
  128.             '-vf', 'scale=trunc(iw/2)*2:trunc(ih/2)*2',
  129.             '-c:v', 'libx264',
  130.             '-tune', 'stillimage',
  131.             '-c:a', 'aac',
  132.             '-b:a', '192k',
  133.             '-shortest',
  134.             '-pix_fmt', 'yuv420p',
  135.             f"{output_file}"
  136.         ]
  137.  
  138.         self.progress_bar.pack(pady=10)
  139.         self.progress_bar.start(10)
  140.        
  141.         threading.Thread(target=self.run_ffmpeg_command, args=(command, output_file)).start()
  142.  
  143.     def run_ffmpeg_command(self, command, output_file):
  144.         try:
  145.             subprocess.run(command, check=True)
  146.             self.output_path_entry.config(state='normal')
  147.             self.output_path_entry.delete(0, tk.END)
  148.             self.output_path_entry.insert(0, output_file)
  149.             self.output_path_entry.config(state='readonly')
  150.             self.result_label.config(text="Conversão concluída!")
  151.             messagebox.showinfo('Sucesso', 'Conversão concluída!')
  152.         except subprocess.CalledProcessError as e:
  153.             self.result_label.config(text=f"Falha na conversão: {e}")
  154.             messagebox.showerror('Erro', f'Falha na conversão: {e}')
  155.         finally:
  156.             self.progress_bar.stop()
  157.             self.progress_bar.pack_forget()  # Esconde a barra de progresso após a conclusão
  158.  
  159.     def show_context_menu(self, event):
  160.         self.context_menu.post(event.x_root, event.y_root)
  161.  
  162.     def copy_to_clipboard(self):
  163.         self.output_path_entry.select_range(0, tk.END)
  164.         self.output_path_entry.event_generate('<<Copy>>')
  165.         self.output_path_entry.selection_clear()
  166.  
  167.     def show_about(self):
  168.         messagebox.showinfo("Sobre", "Conversor de arquivos MP3 para vídeo em MP4 e vice-versa\n2024 - Mizuno")
  169.  
  170. root = tk.Tk()
  171. app = FileConverterApp(root)
  172. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement