Skip to content

Instantly share code, notes, and snippets.

@danieloneill
Created February 12, 2023 14:22
Show Gist options
  • Save danieloneill/3c3880fd0885b75dba94667358bfaf44 to your computer and use it in GitHub Desktop.
Save danieloneill/3c3880fd0885b75dba94667358bfaf44 to your computer and use it in GitHub Desktop.
from TTS.api import TTS
import pysbd
import os
import time
from datetime import timedelta
print(TTS.list_models())
model_name = "tts_models/multilingual/multi-dataset/your_tts"
sample_wav = "samples_of_voice_to_copy.wav"
# Init TTS
tts = TTS(model_name=model_name, gpu=True, progress_bar=True)
f = open('Part1.txt')
# "The Mysterious Island" from Gutenberg uses these weird characters that confuse it. There are better ways to do this.
fulltext = f.read(-1).replace("“", "\"").replace("”", "\"").replace("—", "-").replace("’", "'").replace("\" \"", "\". \"")
f.close()
# It crashes... I'll split it myself.
seg = pysbd.Segmenter(language="en", clean=True)
lines = seg.segment(fulltext)
start = time.time()
idx = 1
for line in lines:
if len(line) < 5:
continue
# Text to speech to a file
tts.tts_to_file(text=line, file_path="/tmp/output.wav", language="en", speaker_wav=sample_wav)
if 1 == idx:
os.system("ffmpeg -i /tmp/output.wav -y /tmp/output.aac")
else:
os.system("ffmpeg -i /tmp/output.wav -y /tmp/piece.aac")
os.system("cat /tmp/piece.aac >> /tmp/output.aac; rm /tmp/piece.aac")
elapsed = time.time() - start
print("Processed line %i of %d [%s]" % (idx, len(lines), str(timedelta(seconds=elapsed))))
idx = idx + 1
print("Done, I guess.")
os.system("mv /tmp/output.aac finished.aac")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment