You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RedditVideoMakerBot/video_creation/voices.py

94 lines
3.1 KiB

#!/usr/bin/env python3
from os import getenv
3 years ago
from pathlib import Path
3 years ago
import sox
from mutagen import MutagenError
from mutagen.mp3 import MP3, HeaderNotFoundError
3 years ago
from rich.console import Console
from rich.progress import track
from TTS.swapper import TTS
2 years ago
console = Console()
3 years ago
from utils.console import print_step, print_substep
from utils.voice import sanitize_text
2 years ago
import translators as ts
import os
VIDEO_LENGTH: int = 40 # secs
3 years ago
def save_text_to_mp3(reddit_obj):
"""Saves Text to MP3 files.
Args:
2 years ago
reddit_obj : The reddit object you received from the reddit API in the askreddit.py file.
"""
print_step("Saving Text to MP3 files...")
length = 0
3 years ago
# Create a folder for the mp3 files.
Path("assets/temp/mp3").mkdir(parents=True, exist_ok=True)
2 years ago
if os.getenv("POSTLANG"):
print_substep("Translating Texts...")
tl_title = ts.google(reddit_obj["thread_title"], to_language=os.getenv("POSTLANG"))
else:
print_substep("Skipping Translation...")
tl_title = reddit_obj["thread_title"]
TextToSpeech = TTS()
TextToSpeech.tts(
2 years ago
sanitize_text(tl_title),
filename=f"assets/temp/mp3/title.mp3",
random_speaker=False,
)
try:
2 years ago
length += MP3(f"assets/temp/mp3/title.mp3").info.length
except HeaderNotFoundError: # note to self AudioFileClip
2 years ago
length += sox.file_info.duration(f"assets/temp/mp3/title.mp3")
if getenv("STORYMODE").casefold() == "true":
TextToSpeech.tts(
sanitize_text(reddit_obj["thread_content"]),
2 years ago
filename=f"assets/temp/mp3/story_content.mp3",
random_speaker=False,
)
# 'story_content'
com = 0
for comment in track((reddit_obj["comments"]), "Saving..."):
2 years ago
# ! Stop creating mp3 files if the length is greater than VIDEO_LENGTH seconds. This can be longer, but this is just a good_voices starting point
if length > VIDEO_LENGTH:
break
2 years ago
if os.getenv("POSTLANG"):
tl_comment = ts.google(comment["comment_body"], to_language=os.getenv("POSTLANG"))
else:
tl_comment = comment["comment_body"]
TextToSpeech.tts(
2 years ago
sanitize_text(tl_comment),
filename=f"assets/temp/mp3/{com}.mp3",
random_speaker=False,
)
try:
length += MP3(f"assets/temp/mp3/{com}.mp3").info.length
com += 1
except (HeaderNotFoundError, MutagenError, Exception):
try:
length += sox.file_info.duration(f"assets/temp/mp3/{com}.mp3")
com += 1
except (OSError, IOError):
print(
"would have removed"
f"assets/temp/mp3/{com}.mp3"
f"assets/temp/png/comment_{com}.png"
)
# remove(f"assets/temp/mp3/{com}.mp3")
# remove(f"assets/temp/png/comment_{com}.png")# todo might cause odd un-syncing
3 years ago
print_substep("Saved Text to MP3 files Successfully.", style="bold green")
# ! Return the index, so we know how many screenshots of comments we need to make.
return length, com