Add silence inbetween tts
This commit is contained in:
1 parent
9c182abc69
commit
9609025bba
1 file changed
+34
-47
+34
-47
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
import re
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
|
||||
@@ -11,7 +11,7 @@ import numpy as np
|
||||
import translators as ts
|
||||
from moviepy.audio.AudioClip import AudioClip
|
||||
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 utils import settings
|
||||
@@ -26,7 +26,7 @@ class TTSEngine:
|
||||
"""Calls the given TTS engine to reduce code duplication and allow multiple TTS engines.
|
||||
|
||||
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.
|
||||
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.
|
||||
@@ -88,10 +88,7 @@ class TTSEngine:
|
||||
split_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"]
|
||||
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)
|
||||
self.create_silence_mp3()
|
||||
|
||||
idy = None
|
||||
for idy, text_cut in enumerate(split_text):
|
||||
@@ -115,53 +112,43 @@ class TTSEngine:
|
||||
try:
|
||||
for i in range(0, len(split_files)):
|
||||
os.unlink(split_files[i])
|
||||
except FileNotFoundError:
|
||||
print("file not found error")
|
||||
except FileNotFoundError as e:
|
||||
print("File not found: " + e.filename)
|
||||
except OSError:
|
||||
print("OSError")
|
||||
|
||||
def call_tts(self, filename: str, text: str):
|
||||
|
||||
if filename == "title":
|
||||
try:
|
||||
self.tts_module.run(text, filepath=f"{self.path}/title_no_silence.mp3")
|
||||
try:
|
||||
silence_duration = settings.config["settings"]["tts"]["silence_duration"]
|
||||
except AttributeError:
|
||||
silence_duration = 0.3
|
||||
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)
|
||||
try:
|
||||
self.tts_module.run(text, filepath=f"{self.path}/{filename}_no_silence.mp3")
|
||||
self.create_silence_mp3()
|
||||
|
||||
with open(f"{self.path}/title.txt", 'w') as f:
|
||||
f.write("file " + f"'title_no_silence.mp3'" + "\n")
|
||||
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:
|
||||
with open(f"{self.path}/{filename}.txt", 'w') as f:
|
||||
f.write("file " + f"'{filename}_no_silence.mp3'" + "\n")
|
||||
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}/{filename}.txt " +
|
||||
"-c copy " + f"{self.path}/{filename}.mp3")
|
||||
clip = AudioFileClip(f"{self.path}/{filename}.mp3")
|
||||
self.length += clip.duration
|
||||
clip.close()
|
||||
try:
|
||||
self.tts_module.run(text=text, filepath=f"{self.path}/{filename}.mp3")
|
||||
clip = AudioFileClip(f"{self.path}/{filename}.mp3")
|
||||
self.length += clip.duration
|
||||
clip.close()
|
||||
except:
|
||||
self.length = 0
|
||||
name = [f"{filename}_no_silence.mp3", "silence.mp3", f"{filename}.txt"]
|
||||
for i in range(0, len(name)):
|
||||
os.unlink(str(rf"{self.path}/" + name[i]))
|
||||
except FileNotFoundError as e:
|
||||
print("File not found: " + e.filename)
|
||||
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):
|
||||
|
||||
Reference in new issue
Block a user