Customize voice

pull/90/head
lilkitkat1 3 years ago
parent 5b3989611f
commit 477f69261d

1
.gitignore vendored

@ -1,3 +1,4 @@
assets/ assets/
.env .env
reddit-bot-351418-5560ebc49cac.json reddit-bot-351418-5560ebc49cac.json
__pycache__/

@ -3,7 +3,27 @@ from pathlib import Path
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
from utils.console import print_step, print_substep from utils.console import print_step, print_substep
from rich.progress import track from rich.progress import track
from sys import argv
def get_accent(_in: str) -> str:
"""Returns the short version of an accent.
For example: _in = "English (Australia)" -> "com.au".
Args:
_in: the input text. For example "Australia"
"""
accents = {
"australia": "com.au",
"united kingdom": "co.uk",
"united states": "com",
"canada": "ca",
"india" : "co.in",
"ireland": "ie",
"south africa": "co.za"
}
return accents[_in.lower()]
def save_text_to_mp3(reddit_obj): def save_text_to_mp3(reddit_obj):
"""Saves Text to MP3 files. """Saves Text to MP3 files.
@ -11,13 +31,19 @@ def save_text_to_mp3(reddit_obj):
Args: Args:
reddit_obj : The reddit object you received from the reddit API in the askreddit.py file. reddit_obj : The reddit object you received from the reddit API in the askreddit.py file.
""" """
try:
accent = get_accent(argv[1])
# If no argument; set to default (co.uk)
except IndexError:
accent = "co.uk"
print_step("Saving Text to MP3 files 🎶") print_step("Saving Text to MP3 files 🎶")
length = 0 length = 0
# Create a folder for the mp3 files. # Create a folder for the mp3 files.
Path("assets/mp3").mkdir(parents=True, exist_ok=True) Path("assets/mp3").mkdir(parents=True, exist_ok=True)
tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False, tld="co.uk") tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False, tld = accent)
tts.save(f"assets/mp3/title.mp3") tts.save(f"assets/mp3/title.mp3")
length += MP3(f"assets/mp3/title.mp3").info.length length += MP3(f"assets/mp3/title.mp3").info.length
@ -25,7 +51,7 @@ def save_text_to_mp3(reddit_obj):
# ! Stop creating mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good starting point # ! Stop creating mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good starting point
if length > 50: if length > 50:
break break
tts = gTTS(text=comment["comment_body"], lang="en") tts = gTTS(text=comment["comment_body"], lang="en", tld = accent)
tts.save(f"assets/mp3/{idx}.mp3") tts.save(f"assets/mp3/{idx}.mp3")
length += MP3(f"assets/mp3/{idx}.mp3").info.length length += MP3(f"assets/mp3/{idx}.mp3").info.length

Loading…
Cancel
Save