import requests import playsound import tempfile import os def…
- User
import requests import playsound import tempfile import os
def get_google_translate_tts_url(text, lang='th'): """Get the URL for Google Translate TTS.""" url = 'https://translate.google.com/translate_tts' params = { 'ie': 'UTF-8', 'q': text, 'tl': lang, 'client': 'gtx' } return url, params
def download_audio(text, lang='th'): """Download audio file from Google Translate.""" url, params = get_google_translate_tts_url(text, lang) response = requests.get(url, params=params, headers={'User-Agent': 'Mozilla/5.0'}) response.raise_for_status() # Raise an exception for HTTP errors
code# Save the audio to a temporary file with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp_file: tmp_file.write(response.content) audio_path = tmp_file.name print(f"Audio saved to {audio_path}") return audio_pathdef play_audio(audio_path): """Play the audio file.""" playsound.playsound(audio_path)
def main(): user_input = input("Enter Thai text or filename (e.g., ep001.txt): ") lang = 'th' # Language code for Thai
code# Check if the user input is a file if user_input.endswith('.txt') and os.path.isfile(user_input): with open(user_input, 'r', encoding='utf-8') as file: text = file.read().strip() # Read the content of the file print(f"Read from file: {text}") else: text = user_input # Use the input directly as text print(f"Using direct input: {text}") audio_path = download_audio(text, lang) play_audio(audio_path)if name == 'main': main()
user input with file type read from file
user input with text type using direct input
look like input text is too long make separate it to sentence then next sentence but i need last mp3 all
- Assistant
- User
merge mp3 to one file
- Assistant
- User
and fixed speed mp3 to 1.25x
- Assistant