def download_audio(text, lang='th'):
try:
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()
sounds_folder = 'sounds'
if not os.path.exists(sounds_folder):
os.mkdir(sounds_folder)
audio_filename = f'{text}.mp3'
audio_path = os.path.join(sounds_folder, audio_filename)
with open(audio_path, 'wb') as audio_file:
audio_file.write(response.content)
print(f"Audio saved to {audio_path}")
return audio_path
except requests.RequestException as e:
print(f"Failed to download audio: {e}")
return None
make small program to convert text to audio by use abouve function
Assistant
Certainly! Below is a small Python program that uses the download_audio function to convert text to audio. The program prompts the user to enter some text and then uses the specified language (Thai by default) to generate and save the audio. To ensure this program runs successfully, make sure you have the appropriate packages installed, and that you have implemented the get_google_translate_tts_url function which is required by download_audio.
Here's a minimal working example:
python
import requests
import os
def get_google_translate_tts_url(text, lang):
# Placeholder implementation. Replace it with the actual function logic.
# This function needs to return a URL and parameters for the Google TTS service
return "https://translate.google.com/translate_tts", {'ie': 'UTF-8', 'q': text, 'tl': lang, 'client': 'tw-ob'}
def download_audio(text, lang='th'):
try:
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()
sounds_folder = 'sounds'
if not os.path.exists(sounds_folder):
os.mkdir(sounds_folder)
audio_filename = f'{text}.mp3'
audio_path = os.path.join(sounds_folder, audio_filename)
with open(audio_path, 'wb') as audio_file:
audio_file.write(response.content)
print(f"Audio saved to {audio_path}")
return audio_path
except requests.RequestException as e:
print(f"Failed to download audio: {e}")
return None
def main():
text = input("Enter the text to convert to audio: ")
lang = input("Enter the language code (default 'th' for Thai): ") or 'th'
# Call the audio download function
download_audio(text, lang)
if __name__ == "__main__":
main()
Instructions to Run the Program:
Ensure that you have Python installed on your system.
Replace the get_google_translate_tts_url function with a proper implementation that constructs a valid request URL and parameters for Google Translate's TTS service.
Install the requests library if it is not already installed by running:
bash
pip install requests
Save the code into a Python file, for example, text_to_audio.py.
Run the script using:
bash
python text_to_audio.py
Follow the prompts to input your text and the language code.
Note:
The provided URL in the placeholder get_google_translate_tts_url function may not directly work as Google may have policies against automated usage of their TTS service without an official API or permission. Make sure to comply with their terms of service.