File size: 6,858 Bytes
e71d833 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# author: GiorDior aka Giorgio
# date: 12.06.2023
# topic: TikTok-Voice-TTS
# version: 1.0
# credits: https://github.com/oscie57/tiktok-voice
# --- MODIFIED VERSION --- #
import base64
import requests
import threading
from typing import List
from termcolor import colored
from playsound import playsound
VOICES = [
# DISNEY VOICES
"en_us_ghostface", # Ghost Face
"en_us_chewbacca", # Chewbacca
"en_us_c3po", # C3PO
"en_us_stitch", # Stitch
"en_us_stormtrooper", # Stormtrooper
"en_us_rocket", # Rocket
# ENGLISH VOICES
"en_au_001", # English AU - Female
"en_au_002", # English AU - Male
"en_uk_001", # English UK - Male 1
"en_uk_003", # English UK - Male 2
"en_us_001", # English US - Female (Int. 1)
"en_us_002", # English US - Female (Int. 2)
"en_us_006", # English US - Male 1
"en_us_007", # English US - Male 2
"en_us_009", # English US - Male 3
"en_us_010", # English US - Male 4
# EUROPE VOICES
"fr_001", # French - Male 1
"fr_002", # French - Male 2
"de_001", # German - Female
"de_002", # German - Male
"es_002", # Spanish - Male
# AMERICA VOICES
"es_mx_002", # Spanish MX - Male
"br_001", # Portuguese BR - Female 1
"br_003", # Portuguese BR - Female 2
"br_004", # Portuguese BR - Female 3
"br_005", # Portuguese BR - Male
# ASIA VOICES
"id_001", # Indonesian - Female
"jp_001", # Japanese - Female 1
"jp_003", # Japanese - Female 2
"jp_005", # Japanese - Female 3
"jp_006", # Japanese - Male
"kr_002", # Korean - Male 1
"kr_003", # Korean - Female
"kr_004", # Korean - Male 2
# SINGING VOICES
"en_female_f08_salut_damour", # Alto
"en_male_m03_lobby", # Tenor
"en_female_f08_warmy_breeze", # Warmy Breeze
"en_male_m03_sunshine_soon", # Sunshine Soon
# OTHER
"en_male_narration", # narrator
"en_male_funny", # wacky
"en_female_emotional", # peaceful
]
ENDPOINTS = [
"https://tiktok-tts.weilnet.workers.dev/api/generation",
"https://tiktoktts.com/api/tiktok-tts",
]
current_endpoint = 0
# in one conversion, the text can have a maximum length of 300 characters
TEXT_BYTE_LIMIT = 300
# create a list by splitting a string, every element has n chars
def split_string(string: str, chunk_size: int) -> List[str]:
words = string.split()
result = []
current_chunk = ""
for word in words:
if (
len(current_chunk) + len(word) + 1 <= chunk_size
): # Check if adding the word exceeds the chunk size
current_chunk += f" {word}"
else:
if current_chunk: # Append the current chunk if not empty
result.append(current_chunk.strip())
current_chunk = word
if current_chunk: # Append the last chunk if not empty
result.append(current_chunk.strip())
return result
# checking if the website that provides the service is available
def get_api_response() -> requests.Response:
url = f'{ENDPOINTS[current_endpoint].split("/a")[0]}'
response = requests.get(url)
return response
# saving the audio file
def save_audio_file(base64_data: str, filename: str = "output.mp3") -> None:
audio_bytes = base64.b64decode(base64_data)
with open(filename, "wb") as file:
file.write(audio_bytes)
# send POST request to get the audio data
def generate_audio(text: str, voice: str) -> bytes:
url = f"{ENDPOINTS[current_endpoint]}"
headers = {"Content-Type": "application/json"}
data = {"text": text, "voice": voice}
response = requests.post(url, headers=headers, json=data)
return response.content
# creates an text to speech audio file
def tts(
text: str,
voice: str = "none",
filename: str = "output.mp3",
play_sound: bool = False,
) -> None:
# checking if the website is available
global current_endpoint
if get_api_response().status_code == 200:
print(colored("[+] TikTok TTS Service available!", "green"))
else:
current_endpoint = (current_endpoint + 1) % 2
if get_api_response().status_code == 200:
print(colored("[+] TTS Service available!", "green"))
else:
print(colored("[-] TTS Service not available and probably temporarily rate limited, try again later..." , "red"))
return
# checking if arguments are valid
if voice == "none":
print(colored("[-] Please specify a voice", "red"))
return
if voice not in VOICES:
print(colored("[-] Voice not available", "red"))
return
if not text:
print(colored("[-] Please specify a text", "red"))
return
# creating the audio file
try:
if len(text) < TEXT_BYTE_LIMIT:
audio = generate_audio((text), voice)
if current_endpoint == 0:
audio_base64_data = str(audio).split('"')[5]
else:
audio_base64_data = str(audio).split('"')[3].split(",")[1]
if audio_base64_data == "error":
print(colored("[-] This voice is unavailable right now", "red"))
return
else:
# Split longer text into smaller parts
text_parts = split_string(text, 299)
audio_base64_data = [None] * len(text_parts)
# Define a thread function to generate audio for each text part
def generate_audio_thread(text_part, index):
audio = generate_audio(text_part, voice)
if current_endpoint == 0:
base64_data = str(audio).split('"')[5]
else:
base64_data = str(audio).split('"')[3].split(",")[1]
if audio_base64_data == "error":
print(colored("[-] This voice is unavailable right now", "red"))
return "error"
audio_base64_data[index] = base64_data
threads = []
for index, text_part in enumerate(text_parts):
# Create and start a new thread for each text part
thread = threading.Thread(
target=generate_audio_thread, args=(text_part, index)
)
thread.start()
threads.append(thread)
# Wait for all threads to complete
for thread in threads:
thread.join()
# Concatenate the base64 data in the correct order
audio_base64_data = "".join(audio_base64_data)
save_audio_file(audio_base64_data, filename)
print(colored(f"[+] Audio file saved successfully as '{filename}'", "green"))
if play_sound:
playsound(filename)
except Exception as e:
print(colored(f"[-] An error occurred during TTS: {e}", "red"))
|