Add silence inbetween tts

pull/1177/head
Thomas Meaney 3 years ago
parent 9c182abc69
commit 9609025bba

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import re
import os import os
import re
from pathlib import Path from pathlib import Path
from typing import Tuple from typing import Tuple
@ -11,7 +11,7 @@ import numpy as np
import translators as ts import translators as ts
from moviepy.audio.AudioClip import AudioClip from moviepy.audio.AudioClip import AudioClip
from moviepy.audio.fx.volumex import volumex from moviepy.audio.fx.volumex import volumex
from moviepy.editor import AudioFileClip, CompositeAudioClip, concatenate_audioclips from moviepy.editor import AudioFileClip
from rich.progress import track from rich.progress import track
from utils import settings from utils import settings
@ -26,7 +26,7 @@ class TTSEngine:
"""Calls the given TTS engine to reduce code duplication and allow multiple TTS engines. """Calls the given TTS engine to reduce code duplication and allow multiple TTS engines.
Args: Args:
tts_module : The TTS module. Your module should handle the TTS itself and saving to the given path under the run method. tts_module : The TTS module. Your module should handle the TTS itself and saving to the given path under the run method.
reddit_object : The reddit object that contains the posts to read. reddit_object : The reddit object that contains the posts to read.
path (Optional) : The unix style path to save the mp3 files to. This must not have leading or trailing slashes. path (Optional) : The unix style path to save the mp3 files to. This must not have leading or trailing slashes.
max_length (Optional) : The maximum length of the mp3 files in total. max_length (Optional) : The maximum length of the mp3 files in total.
@ -88,10 +88,7 @@ class TTSEngine:
split_text = [ split_text = [
x.group().strip() for x in re.finditer(r" *(((.|\n){0," + str(self.tts_module.max_chars) + "})(\.|.$))", text) x.group().strip() for x in re.finditer(r" *(((.|\n){0," + str(self.tts_module.max_chars) + "})(\.|.$))", text)
] ]
silence_duration = settings.config["settings"]["tts"]["silence_duration"] self.create_silence_mp3()
silence = AudioClip(make_frame=lambda t: np.sin(440 * 2 * np.pi * t), duration=silence_duration, fps=44100)
silence = volumex(silence, 0)
silence.write_audiofile(f"{self.path}/silence.mp3", fps=44100, verbose=False, logger=None)
idy = None idy = None
for idy, text_cut in enumerate(split_text): for idy, text_cut in enumerate(split_text):
@ -115,53 +112,43 @@ class TTSEngine:
try: try:
for i in range(0, len(split_files)): for i in range(0, len(split_files)):
os.unlink(split_files[i]) os.unlink(split_files[i])
except FileNotFoundError: except FileNotFoundError as e:
print("file not found error") print("File not found: " + e.filename)
except OSError: except OSError:
print("OSError") print("OSError")
def call_tts(self, filename: str, text: str): def call_tts(self, filename: str, text: str):
if filename == "title": try:
try: self.tts_module.run(text, filepath=f"{self.path}/{filename}_no_silence.mp3")
self.tts_module.run(text, filepath=f"{self.path}/title_no_silence.mp3") self.create_silence_mp3()
try:
silence_duration = settings.config["settings"]["tts"]["silence_duration"] with open(f"{self.path}/{filename}.txt", 'w') as f:
except AttributeError: f.write("file " + f"'{filename}_no_silence.mp3'" + "\n")
silence_duration = 0.3 f.write("file " + f"'silence.mp3'" + "\n")
silence = AudioClip(make_frame=lambda t: np.sin(440 * 2 * np.pi * t), duration=silence_duration, f.close()
fps=44100) os.system("ffmpeg -f concat -y -hide_banner -loglevel panic -safe 0 " +
silence = volumex(silence, 0) "-i " + f"{self.path}/{filename}.txt " +
silence.write_audiofile(f"{self.path}/silence.mp3", fps=44100, verbose=False, logger=None) "-c copy " + f"{self.path}/{filename}.mp3")
clip = AudioFileClip(f"{self.path}/{filename}.mp3")
with open(f"{self.path}/title.txt", 'w') as f: self.length += clip.duration
f.write("file " + f"'title_no_silence.mp3'" + "\n") clip.close()
f.write("file " + f"'silence.mp3'" + "\n")
f.close()
os.system("ffmpeg -f concat -y -hide_banner -loglevel panic -safe 0 " +
"-i " + f"{self.path}/title.txt " +
"-c copy " + f"{self.path}/title.mp3")
clip = AudioFileClip(f"{self.path}/title.mp3")
self.length += clip.duration
clip.close()
try:
name = ["title_no_silence.mp3", "silence.mp3", "title.txt"]
for i in range(0, len(name)):
os.unlink(str(rf"{self.path}/" + name[i]))
except FileNotFoundError:
print("file not found error")
except OSError:
print("OSError")
except:
self.length = 0
else:
try: try:
self.tts_module.run(text=text, filepath=f"{self.path}/{filename}.mp3") name = [f"{filename}_no_silence.mp3", "silence.mp3", f"{filename}.txt"]
clip = AudioFileClip(f"{self.path}/{filename}.mp3") for i in range(0, len(name)):
self.length += clip.duration os.unlink(str(rf"{self.path}/" + name[i]))
clip.close() except FileNotFoundError as e:
except: print("File not found: " + e.filename)
self.length = 0 except OSError:
print("OSError")
except:
self.length = 0
def create_silence_mp3(self):
silence_duration = settings.config["settings"]["tts"]["silence_duration"]
silence = AudioClip(make_frame=lambda t: np.sin(440 * 2 * np.pi * t), duration=silence_duration, fps=44100)
silence = volumex(silence, 0)
silence.write_audiofile(f"{self.path}/silence.mp3", fps=44100, verbose=False, logger=None)
def process_text(text: str): def process_text(text: str):

Loading…
Cancel
Save