Tkinter utilities: convert a wav in mp3 with a GUI

In the last post we made a CLI (command line interface) to covert wav in mp3 with Pyton and ffmpeg.

This time we are going to make a GUI (graphic user interface).

You can find the CLI version hereConvert wav to mp3 with Python + ffmpeg.

import tkinter as tk
import tkinter.ttk as ttk
from glob import glob
import os


root = tk.Tk()
root.title("UTILITIES V.1")


class Button:
    def __init__(self, text, function, row, col):
        self.button = ttk.Button(
            root,
            text=text,
            command=function)
        self.button.grid(row=row, column=col, sticky=tk.NW, pady=0)

def convert():
    if listbox.curselection() != ():
        filewav = listbox.get(listbox.curselection())[0]
        command = f"ffmpeg -i {filewav} -vn -ar 44100 -ac 2 -b:a 192k {filewav[:-3]}mp3"
        os.system(command)
        label['text'] = filewav + " convertito"
        label['bg'] = "green"
        listbox2.delete(0, tk.END)
        listbox2.insert(tk.END, glob("*.mp3"))
    else:
        label["text"] = "Select something"


def delete_mp3():
    if listbox2.curselection() != ():
        filemp3 = listbox2.get(listbox2.curselection())[0]
        os.remove(filemp3)
        label['text'] = filemp3 + " eliminato"
        label['bg'] = "green"
        listbox2.delete(0, tk.END)
        listbox2.insert(tk.END, glob("*.mp3"))
    else:
        label["text"] = "Select something"

listbox = tk.Listbox(root)
listbox.grid(row=0, column=0)
listbox.insert(tk.END, glob("*.wav"))
listbox2 = tk.Listbox(root)
listbox2.grid(row=0, column=1)
listbox2.insert(tk.END, glob("*.mp3"))
button = Button("Convert Wav", convert, 1, 0)
button = Button("Delete Mp3", delete_mp3, 1, 1)
label = tk.Label(root)
label.grid(row=3, column=0, sticky=tk.NW)
label['bg'] = "yellow"
label['text'] = "Select and Choose an action "

root.mainloop()

This is how the gui looks like.

Convert wav to mp3 with Python + ffmpeg

Ffmpeg & Python for videos

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.