Merge pull request #138 from lordvader501/text-to-speech

fixed some bugs in issue #135
This commit is contained in:
Advaita Saha 2022-10-05 13:07:21 +05:30 committed by GitHub
commit 46c3c6b88a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 25 deletions

View File

@ -4,6 +4,5 @@ This is a simple script that lets you convert text to speech and store it in .mp
#### Usage #### Usage
* Clone the repo * Clone the repo
* download the requirements * download the requirements using `pip install -i requirements.txt`
* change the input in script.py * run `python script.py`
* run python script.py

View File

@ -1 +1,2 @@
gtts gtts
playsound==1.2.2

View File

@ -1,26 +1,61 @@
# Import the required module for text
# to speech conversion
from gtts import gTTS from gtts import gTTS
from playsound import playsound
# This module is imported so that we can
# play the converted audio
import os import os
# The text that you want to convert to audio mytext = input("Enter text: ")
mytext = 'Welcome to geeksforgeeks!'
# Language in which you want to convert print("MENU\n1. English (Australia)\n2. English (United Kingdom)\n3. English (United States)\n4. English (Canada)\n5. English (India)\n6. English (Ireland)\n7. English (South Africa)\n8. French (Canada)\n9. French (France)\n10. Mandarin (China Mainland)\n11. Mandarin (Taiwan)\n12. Portuguese (Brazil)\n13. Portuguese (Portugal)\n14. Spanish (Mexico)\n15. Spanish (Spain)\n16. Spanish (United States)")
language = 'en' option = int(input("Select option: "))
if option == 1:
language = 'en'
tld1 = 'com.au'
if option == 2:
language = 'en'
tld1 = 'co.uk'
if option == 3:
language = 'en'
tld1 = 'com'
if option == 4:
language = 'en'
tld1 = 'ca'
if option == 5:
language = 'en'
tld1 = 'co.in'
if option == 6:
language = 'en'
tld1 = 'ie'
if option == 7:
language = 'en'
tld1 = 'co.za'
if option == 8:
language = 'fr'
tld1 = 'ca'
if option == 9:
language = 'fr'
tld1 = 'fr'
if option == 10:
language = 'zh-CN'
tld1 = 'com'
if option == 11:
language = 'zh-TW'
tld1 = 'com'
if option == 12:
language = 'pt'
tld1 = 'com.br'
if option == 13:
language = 'pt'
tld1 = 'pt'
if option == 14:
language = 'es'
tld1 = 'com.mx'
if option == 15:
language = 'es'
tld1 = 'es'
if option == 16:
language = 'es'
tld1 = 'com'
# Passing the text and language to the engine, tts = gTTS(text=mytext, tld=tld1, lang=language, slow=False)
# here we have marked slow=False. Which tells audio_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tts.mp3")
# the module that the converted audio should tts.save(audio_file)
# have a high speed playsound(audio_file)
myobj = gTTS(text=mytext, lang=language, slow=False)
# Saving the converted audio in a mp3 file named
# welcome
myobj.save("welcome.mp3")
# Playing the converted file
os.system("mpg321 welcome.mp3")

Binary file not shown.