Double-click on your new file
myfile.mp3
to play it with iTunes.
pip3 install gtts pip3 show gtts gtts-cli --help gtts-cli --all | cat -n cd ~/Desktop gtts-cli --lang en-us --slow --output myfile.mp3 "Hello, how are you?" ls -l myfile.mp3 -rw-r--r-- 1 myname mygroup 9024 Sep 20 08:12 myfile.mp3 file myfile.mp3 myfile.mp3: MPEG ADTS, layer III, v2, 32 kbps, 24 kHz, Monaural
""" Convert text to speech in different languages. """ import sys import os import tempfile import playsound import gtts #Google Text-To-Speech print("We're going to say what you type.") print("Here are the available languages:") print() for key, value in gtts.lang.tts_langs().items(): #a dict print(key, value) print() lang = input("Please enter the code for your language (e.g., en-us): ") text = input("Please enter some text: ") print() try: textToSpeech = gtts.gTTS(text = text, lang = lang, slow = True) except BaseException as error: print(error, file = sys.stderr) sys.exit(1) # Save the audio in a temporary file with a name. temporaryFile = tempfile.NamedTemporaryFile() textToSpeech.save(temporaryFile.name) print("Here is the temporary file:") print(os.popen(f"ls -l {temporaryFile.name}").read(), end = "") print(os.popen(f"file {temporaryFile.name}").read(), end = "") # Play and erase the temporary file. try: playsound.playsound(temporaryFile.name, True) #Requires a filename or URL. except OSError as error: print(error, file = sys.stderr) sys.exit(1) finally: temporaryFile.close() #Erases the temporary file. sys.exit(0)
We're going to say what you type. Here are the available languages: af Afrikaans sq Albanian ar Arabic hy Armenian bn Bengali bs Bosnian ca Catalan hr Croatian cs Czech da Danish nl Dutch en English eo Esperanto et Estonian tl Filipino fi Finnish fr French de German el Greek gu Gujarati hi Hindi hu Hungarian is Icelandic id Indonesian it Italian ja Japanese jw Javanese kn Kannada km Khmer ko Korean la Latin lv Latvian mk Macedonian ml Malayalam mr Marathi my Myanmar (Burmese) ne Nepali no Norwegian pl Polish pt Portuguese ro Romanian ru Russian sr Serbian si Sinhala sk Slovak es Spanish su Sundanese sw Swahili sv Swedish ta Tamil te Telugu th Thai tr Turkish uk Ukrainian ur Urdu vi Vietnamese cy Welsh zh-cn Chinese (Mandarin/China) zh-tw Chinese (Mandarin/Taiwan) en-us English (US) en-ca English (Canada) en-uk English (UK) en-gb English (UK) en-au English (Australia) en-gh English (Ghana) en-in English (India) en-ie English (Ireland) en-nz English (New Zealand) en-ng English (Nigeria) en-ph English (Philippines) en-za English (South Africa) en-tz English (Tanzania) fr-ca French (Canada) fr-fr French (France) pt-br Portuguese (Brazil) pt-pt Portuguese (Portugal) es-es Spanish (Spain) es-us Spanish (United States) Please enter the code for your language (e.g., en-us): es Please enter some text: Yo no soy marinero. Soy capitan. Here is the temporary file: -rw------- 1 myname mygroup 13632 Sep 12 11:25 /var/folders/pb/rx_csw656h95vzh_nk8ndt180000gn/T/tmp5cadx4zm /var/folders/pb/rx_csw656h95vzh_nk8ndt180000gn/T/tmp5cadx4zm: MPEG ADTS, layer III, v2, 32 kbps, 24 kHz, Monaural