Skip to content

Instantly share code, notes, and snippets.

@partrita
Created May 14, 2024 01:58
Show Gist options
  • Save partrita/4a0e6e4f486ebbd127279aaa0519efec to your computer and use it in GitHub Desktop.
Save partrita/4a0e6e4f486ebbd127279aaa0519efec to your computer and use it in GitHub Desktop.
Text to audio with StyleTTS2.

How to use

  1. Make directory of txt file in it.
input/
├── 1.txt
└── 2.txt

0 directories, 2 files
  1. Install dependency. Use virtual env.
> python -m venv venv
> source venv/bin/activate
> pip install styletts2 tqdm
  1. Run script.
python text2wav.py -i input -o output
  1. If if works
output
├── 1.wav
└── 2.wav

0 directories, 2 files
import os
import re
import argparse
from styletts2 import tts
from tqdm import tqdm
def text_to_audio(chapters_dir, output_audio_dir="output", target_voice_path=None):
"""
주어진 폴더의 텍스트 파일들을 오디오로 변환합니다.
Args:
chapters_dir (str): 텍스트 파일들이 있는 폴더의 경로.
output_audio_dir (str): 오디오 파일을 저장할 폴더의 경로. 기본값은 "output".
target_voice_path (str): 사용할 음성 파일의 경로. 기본값은 None.
"""
my_tts = tts.StyleTTS2()
if not os.path.exists(output_audio_dir):
os.makedirs(output_audio_dir)
for root, _, files in os.walk(chapters_dir):
for file_name in tqdm(files, desc="Converting chapters to audio"):
if file_name.endswith(".txt"):
chapter_path = os.path.join(root, file_name)
output_file_path = os.path.join(
output_audio_dir, file_name.replace(".txt", ".wav")
)
with open(chapter_path, "r", encoding="utf-8") as file:
chapter_text = file.read()
if target_voice_path:
my_tts.inference(
chapter_text,
target_voice_path=target_voice_path,
output_wav_file=output_file_path,
)
else:
my_tts.inference(chapter_text, output_wav_file=output_file_path)
def split_text_lines(input_file):
"""
주어진 텍스트 파일을 각 라인의 단어 수가 100을 넘어가면 다음 줄로 나눕니다.
Args:
input_file (str): 입력 파일의 경로.
"""
output_file = input_file
temp_file = input_file + ".temp"
with open(input_file, "r") as f_in, open(temp_file, "w") as f_out:
for line in f_in:
words = re.findall(r"\b\w+\b", line)
new_line = ""
word_count = 0
for word in words:
word_count += 1
if word_count <= 100:
new_line += word + " "
else:
f_out.write(new_line.strip() + "\n")
new_line = word + " "
word_count = 1
f_out.write(new_line.strip() + "\n")
os.remove(input_file)
os.rename(temp_file, output_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert text files to audio")
parser.add_argument("-i", "--input", type=str, help="Path to the folder containing input text files")
parser.add_argument("-o", "--output", type=str, default="output", help="Path to the output audio folder")
args = parser.parse_args()
if args.input:
text_to_audio(args.input, args.output)
print("Text to audio conversion is done!")
else:
print("Please provide the input folder path using -i or --input option.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment