-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
88 lines (71 loc) · 4.56 KB
/
functions.py
File metadata and controls
88 lines (71 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
import wx
from pydub import AudioSegment
logging.basicConfig(filename='app.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
class converterApp(wx.Frame):
def __init__(self, parent, title):
super(converterApp, self).__init__(parent, title=title, size=(300, 200))
self.InitUi()
self.Centre()
self.Show()
def InitUi(self):
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
# Primero el cuadro de edición para la ruta del archivo
name_to_convert = wx.StaticText(panel, label="Escribe o pega la ruta del archivo a convertir (con formato)")
vbox.Add(name_to_convert, flag=wx.LEFT | wx.TOP, border=10)
self.fileName = wx.TextCtrl(panel, style=wx.TE_PROCESS_ENTER)
vbox.Add(self.fileName, proportion=0, flag=wx.EXPAND | wx.ALL, border=10)
# Luego el botón para añadir el archivo, con atajo de teclado
self.select_file = wx.Button(panel, label="&Abrir el explorador")
self.select_file.Bind(wx.EVT_BUTTON, self.file_select)
vbox.Add(self.select_file, flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=10)
# Después el cuadro para el nombre del archivo que se creará
nameFile = wx.StaticText(panel, label="Escribe el nombre del archivo convertido:")
vbox.Add(nameFile, flag=wx.LEFT | wx.TOP, border=10)
self.name_from_file = wx.TextCtrl(panel, style=wx.TE_PROCESS_ENTER)
vbox.Add(self.name_from_file, proportion=0, flag=wx.EXPAND | wx.ALL, border=10)
# Finalmente, los botones restantes con sus atajos
self.ready = wx.Button(panel, label="&Convertir")
self.ready.Bind(wx.EVT_BUTTON, self.audio_converter)
vbox.Add(self.ready, flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=10)
self.exit_button = wx.Button(panel, label="&Salir del programa")
self.exit_button.Bind(wx.EVT_BUTTON, self.exit_program)
vbox.Add(self.exit_button, flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=10)
panel.SetSizer(vbox)
def file_select(self, event):
with wx.FileDialog(self, "Seleccionar archivo de audio", wildcard="Archivos de audio (*.mp3;*.wav;*.opus;*.flac;*.m4a;*.ogg)|*.mp3;*.wav;*.opus;*.flac;*.m4a;*.ogg", style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return # El usuario canceló la selección
path = fileDialog.GetPath()
self.fileName.SetValue(path)
self.name_from_file.SetFocus()
def audio_converter(self, event):
source_path = self.fileName.GetValue().strip()
output_path = self.name_from_file.GetValue().strip()
if not source_path or not output_path:
wx.MessageBox("Llena correctamente todos los campos.", "Error", wx.OK | wx.ICON_ERROR)
return
dialog = wx.SingleChoiceDialog(self, "Elige el formato a convertir:", "Selecciona formato", ["mp3", "wav", "m4a", "opus", "flac", "ogg"])
if dialog.ShowModal() == wx.ID_OK:
selected_format = dialog.GetStringSelection()
try:
audio = AudioSegment.from_file(source_path)
output_file = f"{output_path}.{selected_format}"
audio.export(output_file, format=selected_format)
wx.MessageBox(f'Archivo convertido y guardado en: {output_file}', "Conversión completada", wx.OK | wx.ICON_INFORMATION)
self.fileName.SetValue("")
self.name_from_file.SetValue("")
self.fileName.SetFocus()
except FileNotFoundError:
logging.exception("El archivo no fue encontrado")
wx.MessageBox("El archivo especificado no fue encontrado. Verifica la ruta y vuelve a intentarlo.", "Archivo no encontrado", wx.OK | wx.ICON_ERROR)
except PermissionError:
logging.exception("no se tienen los permisos suficientes.")
wx.MessageBox("No se tienen los permisos necesarios para leer el archivo o escribir el archivo de salida. Verifica los permisos y vuelve a intentarlo.", "Error de permisos", wx.OK | wx.ICON_ERROR)
except Exception as e:
logging.exception(f"Error desconocido durante la conversión: {str(e)}")
wx.MessageBox(f"Error durante la conversión: {str(e)}", "Error Desconocido", wx.OK | wx.ICON_ERROR)
dialog.Destroy()
def exit_program(self, event):
self.Close(True)