From 174df19be7cfae46301b06068d199aacf8166be6 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 7 Jun 2022 18:38:14 -0400 Subject: [PATCH] fix: improved TTS requests fixes #64 --- .env.template | 2 +- main.py | 2 +- video_creation/TTSwrapper.py | 30 ++++++++++++++++++++---------- video_creation/final_video.py | 4 ++-- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/.env.template b/.env.template index a99bcc9..809c6fc 100644 --- a/.env.template +++ b/.env.template @@ -15,7 +15,7 @@ THEME="LIGHT" TIMES_TO_RUN="" MAX_COMMENT_LENGTH="500" # Range is 0 -> 1 recommended around 0.8-0.9 -OPACITY="" +OPACITY="1" # see TTSwrapper.py for all valid options VOICE="en_us_001" # e.g. en_us_002 diff --git a/main.py b/main.py index 67cb645..d86b422 100755 --- a/main.py +++ b/main.py @@ -56,7 +56,7 @@ def run_many(times): if __name__ == "__main__": try: - if getenv("TIMES_TO_RUN"): + if getenv("TIMES_TO_RUN") and isinstance(int(getenv("TIMES_TO_RUN")), int): run_many(int(getenv("TIMES_TO_RUN"))) else: main() diff --git a/video_creation/TTSwrapper.py b/video_creation/TTSwrapper.py index 7015d44..42158ba 100644 --- a/video_creation/TTSwrapper.py +++ b/video_creation/TTSwrapper.py @@ -1,10 +1,11 @@ -import re - import base64 import os import random +import re + import requests from moviepy.editor import AudioFileClip, concatenate_audioclips, CompositeAudioClip +from requests.adapters import HTTPAdapter, Retry # from profanity_filter import ProfanityFilter # pf = ProfanityFilter() @@ -66,11 +67,11 @@ class TTTTSWrapper: # TikTok Text-to-Speech Wrapper self.URI_BASE = "https://api16-normal-useast5.us.tiktokv.com/media/api/text/speech/invoke/?text_speaker=" def tts( - self, - req_text: str = "TikTok Text To Speech", - filename: str = "title.mp3", - random_speaker: bool = False, - censer=False, + self, + req_text: str = "TikTok Text To Speech", + filename: str = "title.mp3", + random_speaker: bool = False, + censer=False, ): req_text = req_text.replace("+", "plus").replace(" ", "+").replace("&", "and") if censer: @@ -90,9 +91,18 @@ class TTTTSWrapper: # TikTok Text-to-Speech Wrapper chunkId = 0 for chunk in chunks: - r = requests.post( - f"{self.URI_BASE}{voice}&req_text={chunk}&speaker_map_type=0" - ) + try: + r = requests.post( + f"{self.URI_BASE}{voice}&req_text={chunk}&speaker_map_type=0" + ) + except requests.exceptions.SSLError: + # https://stackoverflow.com/a/47475019/18516611 + session = requests.Session() + retry = Retry(connect=3, backoff_factor=0.5) + adapter = HTTPAdapter(max_retries=retry) + session.mount('http://', adapter) + session.mount('https://', adapter) + r = session.post(f"{self.URI_BASE}{voice}&req_text={chunk}&speaker_map_type=0") vstr = [r.json()["data"]["v_str"]][0] b64d = base64.b64decode(vstr) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 2d0afcd..42168f7 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -43,7 +43,7 @@ def make_final_video(number_of_clips, length): # Gather all images image_clips = [] for i in range(0, number_of_clips): - if opacity is None or int(opacity) >= 1: # opacity not set or is set to one OR MORE + if opacity is None or float(opacity) >= 1: # opacity not set or is set to one OR MORE image_clips.append( ImageClip(f"assets/temp/png/comment_{i}.png") .set_duration(audio_clips[i + 1].duration) @@ -58,7 +58,7 @@ def make_final_video(number_of_clips, length): .resize(width=W - 100) .set_opacity(float(opacity)), ) - if opacity is None or int(opacity) >= 1: # opacity not set or is set to one OR MORE + if opacity is None or float(opacity) >= 1: # opacity not set or is set to one OR MORE image_clips.insert( 0, ImageClip(f"assets/temp/png/title.png")