Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import customtkinter as ctk
- import os
- from gtts import gTTS
- from pydub import AudioSegment
- import tempfile
- # Set appearance
- ctk.set_appearance_mode("dark")
- ctk.set_default_color_theme("blue")
- # Create main window
- root = ctk.CTk()
- root.title("Text to Speech")
- root.geometry("400x300")
- # Get Windows Downloads folder path
- downloads_folder = os.path.join(os.path.expanduser("~"), "Downloads")
- def convert_text_to_speech():
- text = entry.get()
- if not text:
- status_label.configure(text="Please enter some text!", text_color="red")
- return
- try:
- status_label.configure(text="Processing...", text_color="yellow")
- root.update()
- # Create temporary file for MP3
- with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_mp3:
- # Generate speech with gTTS
- tts = gTTS(text=text, lang='en')
- tts.save(temp_mp3.name)
- # Convert MP3 to WAV using pydub
- audio = AudioSegment.from_mp3(temp_mp3.name)
- output_file = os.path.join(downloads_folder, "output.wav")
- audio.export(output_file, format="wav")
- # Clean up temporary file
- os.unlink(temp_mp3.name)
- status_label.configure(text="Saved to Downloads/output.wav", text_color="green")
- except Exception as e:
- status_label.configure(text=f"Error: {str(e)}", text_color="red")
- # Create GUI elements
- label = ctk.CTkLabel(root, text="Enter text to convert to speech:", font=("Arial", 14))
- label.pack(pady=20)
- entry = ctk.CTkEntry(root, width=300, placeholder_text="Type your text here")
- entry.pack(pady=10)
- convert_button = ctk.CTkButton(root, text="Convert to WAV", command=convert_text_to_speech)
- convert_button.pack(pady=20)
- status_label = ctk.CTkLabel(root, text="", font=("Arial", 12))
- status_label.pack(pady=10)
- # Start main loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment