-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt_2_mp3.py
116 lines (103 loc) · 3.73 KB
/
txt_2_mp3.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
import sys
import os
import rich_click as click
from gtts import gTTS
import ntpath
import time
# without this, pygame prints a header in the console
import contextlib
with contextlib.redirect_stdout(None):
import pygame
# main class
class GetAudio:
def __init__(
self,
path,
accent,
):
self.path = path
# function to assign accent code based on numerical choice
def match_accent(accent):
if accent == "1":
return "com.au"
elif accent == "2":
return "co.uk"
elif accent == "3":
return "com"
elif accent == "4":
return "ca"
elif accent == "5":
return "co.in"
elif accent == "6":
return "ie"
elif accent == "7":
return "co.za"
else:
return "com"
self.accent = match_accent(accent)
# main function
def create_mp3(self):
language = "en"
# load text, convert to mp3, save file and play sample for user
try:
with open(self.path) as f:
the_text = f.read()
# conversion magic
mp3 = gTTS(the_text, lang=language, tld=self.accent)
# strip filename from filepath
file_name = ntpath.basename(self.path)
# strip file type extension from name
file_name = file_name.replace(".txt", "")
if not os.path.exists(f"{ sys.path[0] }/mp3s/"):
os.makedirs(f"{ sys.path[0] }/mp3s/")
time.sleep(3)
# save mp3, with progress bar for large files
mp3.save(f"{ sys.path[0] }/mp3s/{file_name}.mp3")
# Alert use of success and location of mp3
click.secho(
f"\n\nMP3 file created at { sys.path[0] }/mp3s/{file_name}.mp3\n\n",
fg="green",
)
# Uncomment to automatically play audio sample and alert user (overuse could damage speakers)
# pygame.mixer.init() # initialize mixer module
# pygame.mixer.music.load(f"{ sys.path[0] }/mp3s/{file_name}.mp3")
# pygame.mixer.music.play()
# print("Audio sample will play for 3 seconds\n\n")
# time.sleep(3)
# handle exception (exits program)
except FileNotFoundError:
print(
"\n\nERROR\n\nA file named '{}' does not exist. Please try again.\n\n".format(
self.path
)
)
# Prompt user for file input, store as 'path'
@click.command()
@click.option(
"--path",
prompt="\n\nWELCOME to TXT-2-MP3\n\nEnter the path and filename of the .txt file, to convert from text to speech (example: 'readthis.txt').\n\n",
help="Enter the path and filename of the .txt file, to convert from text to speech (example: 'readthis.txt')",
required=True,
)
# Prompt user for accent input, store as 'accent'
@click.option(
"--accent",
prompt="\nPlease choose an English accent (type a number and hit ENTER)\n\n\
1. English (Australia)\n\
2. English (United Kingdom)\n\
3. English (United States)\n\
4. English (Canada)\n\
5. English (India)\n\
6. English (Ireland)\n\
7. English (South Africa)\n\n",
help="Please choose an English accent",
required=True,
)
# Call main class and function
def cli(path, accent):
invoke_class = GetAudio(path, accent)
invoke_class.create_mp3()
# if you're main, and not remote, this will be invoked ; ) and start the program
if __name__ == "__main__":
cli()