|
| 1 | +import os, sys |
| 2 | +import tkinter as tk |
| 3 | +from tkinter import messagebox, simpledialog |
| 4 | +from pydub import AudioSegment |
| 5 | +from scipy.io.wavfile import write |
| 6 | +import customtkinter |
| 7 | +import sounddevice as spd |
| 8 | + |
| 9 | +fs = 44100 # Sample rate |
| 10 | +seconds = 5 # Duration of recording |
| 11 | + |
| 12 | +class AudioRecorderGUI: |
| 13 | + def __init__(self): |
| 14 | + self.setup_gui() |
| 15 | + |
| 16 | + def setup_gui(self): |
| 17 | + self.Recorder_Gui = customtkinter.CTk() |
| 18 | + self.Recorder_Gui.title("Mic Recorder") |
| 19 | + self.Recorder_Gui.geometry("300x150") |
| 20 | + self.Recorder_Gui.resizable(False, False) |
| 21 | + self.Recorder_Gui.attributes("-topmost", True) |
| 22 | + |
| 23 | + self.record_button = customtkinter.CTkButton(self.Recorder_Gui, text="Record", command=self.record) |
| 24 | + self.record_button.pack(pady=5) |
| 25 | + |
| 26 | + self.stop_button = customtkinter.CTkButton(self.Recorder_Gui, text="Stop Recording", command=self.stop) |
| 27 | + self.stop_button.pack(pady=20) |
| 28 | + self.stop_button.configure(state="disabled") |
| 29 | + |
| 30 | + self.label_input = customtkinter.CTkLabel(self.Recorder_Gui, text="", font=("Arial", 12, "bold"),text_color="white") |
| 31 | + self.label_input.pack(pady=5) |
| 32 | + |
| 33 | + self.Recorder_Gui.mainloop() |
| 34 | + def record(self): |
| 35 | + self.stop_button.configure(state="normal") |
| 36 | + self.record_button.configure(state="disabled") |
| 37 | + self.output_file = "output.wav" |
| 38 | + |
| 39 | + print("Recording...") |
| 40 | + self.label_input.configure(self.Recorder_Gui, text=f"Recording...",font=("Arial", 16, "bold"), text_color="red") |
| 41 | + |
| 42 | + self.myrecording = spd.rec(int(seconds * fs), samplerate=fs, channels=2) |
| 43 | + |
| 44 | + def stop(self): |
| 45 | + self.stop_button.configure(state="disabled") |
| 46 | + self.record_button.configure(state="normal") |
| 47 | + write('output.wav', fs, self.myrecording) # Save as WAV file |
| 48 | + |
| 49 | + spd.stop() |
| 50 | + |
| 51 | + mp3_output_file = simpledialog.askstring("Output File Name", "Enter the name of the output MP3 file:") |
| 52 | + mp3_output_file = str(mp3_output_file)+'.mp3' |
| 53 | + if mp3_output_file: |
| 54 | + convert_to_mp3(self.output_file, mp3_output_file) |
| 55 | + |
| 56 | + messagebox.showinfo("Success", f"Audio saved as {mp3_output_file}") |
| 57 | + self.label_input.configure(self.Recorder_Gui, text=f"",font=("Arial", 16, "bold"), text_color="red") |
| 58 | + |
| 59 | +def convert_to_mp3(input_file, output_file): |
| 60 | + sound = AudioSegment.from_wav(input_file) |
| 61 | + sound.export(output_file, format="mp3") |
| 62 | + os.remove(input_file) |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + gui = AudioRecorderGUI() |
0 commit comments