-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-to-speech.py
More file actions
35 lines (27 loc) · 1.28 KB
/
text-to-speech.py
File metadata and controls
35 lines (27 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pyttsx3
engine = pyttsx3.init()
#config
speakingRate = 125 #speaking rate
volume = 1 #volume 0-1
engine.setProperty('rate', speakingRate) #set speaking rate to the one in config
rate = engine.getProperty('rate') #get the current speaking rate
print ("Speaking rate set to " + str(speakingRate))
engine.setProperty('volume', volume) #set speaking rate to the one in config
volume = engine.getProperty('volume') #get the current speaking rate
print ("Volume set to: " + str(volume))
def log(message): # print a message with [System] at the start
print(f"[System] {message}")
#check if there is a input.txt file. If not, ask user for input. If there is, set the "text" property to the files contents
try:
with open('input.txt', 'r') as file:
text = file.read()
except FileNotFoundError:
text = input('No input.txt file not found. What do you want to convert to audio? ')
save = input('Would you like to save this as an mp3? (y/n)')
if save == "y":
path = input("Where should the file be saved to? ")
log("Saving...")
engine.save_to_file(text, f'{path}output.mp3') # save the file with the text to the path the user requested
log(f"Saved! Path: {path}output.mp3!")
engine.say(text) # say the text outloud
engine.runAndWait()