tiktok voice

pull/2041/head
Cyteon 1 year ago committed by GitHub
parent bc3ce4e972
commit 7a58785615
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,9 +1,9 @@
# documentation for tiktok api: https://github.com/oscie57/tiktok-voice/wiki # documentation for tiktok api: https://github.com/oscie57/tiktok-voice/wiki
import base64 import base64
import random import random
import time import time
from typing import Optional, Final from typing import Optional, Final
import requests import requests
from utils import settings from utils import settings
@ -34,7 +34,6 @@ eng_voices: Final[tuple] = (
"en_us_009", # English US - Male 3 "en_us_009", # English US - Male 3
"en_us_010", # English US - Male 4 "en_us_010", # English US - Male 4
"en_male_narration", # Narrator "en_male_narration", # Narrator
"en_male_funny", # Funny
"en_female_emotional", # Peaceful "en_female_emotional", # Peaceful
"en_male_cody", # Serious "en_male_cody", # Serious
) )
@ -83,10 +82,12 @@ class TikTok:
headers = { headers = {
"User-Agent": "com.zhiliaoapp.musically/2022600030 (Linux; U; Android 7.1.2; es_ES; SM-G988N; " "User-Agent": "com.zhiliaoapp.musically/2022600030 (Linux; U; Android 7.1.2; es_ES; SM-G988N; "
"Build/NRD90M;tt-ok/3.12.13.1)", "Build/NRD90M;tt-ok/3.12.13.1)",
"Cookie": f"sessionid={settings.config['settings']['tts']['tiktok_sessionid']}", #"Cookie": f"sessionid={settings.config['settings']['tts']['tiktok_sessionid']}",
} }
self.URI_BASE = "https://api16-normal-c-useast1a.tiktokv.com/media/api/text/speech/invoke/" self.URI_BASE = (
"https://tiktok-tts.weilnet.workers.dev/api/generation"
)
self.max_chars = 200 self.max_chars = 200
self._session = requests.Session() self._session = requests.Session()
@ -102,15 +103,12 @@ class TikTok:
# get the audio from the TikTok API # get the audio from the TikTok API
data = self.get_voices(voice=voice, text=text) data = self.get_voices(voice=voice, text=text)
# check if there was an error in the request # check if there was an error in the request
status_code = data["status_code"] status_code = data["error"]
if status_code != 0:
raise TikTokTTSException(status_code, data["message"])
# decode data from base64 to binary # decode data from base64 to binary
try: try:
raw_voices = data["data"]["v_str"] raw_voices = data["data"]
except: except:
print( print(
"The TikTok TTS returned an invalid response. Please try again later, and report this bug." "The TikTok TTS returned an invalid response. Please try again later, and report this bug."
@ -128,17 +126,16 @@ class TikTok:
text = text.replace("+", "plus").replace("&", "and").replace("r/", "") text = text.replace("+", "plus").replace("&", "and").replace("r/", "")
# prepare url request # prepare url request
params = {"req_text": text, "speaker_map_type": 0, "aid": 1233} params = {"text": text,"voice": voice}
if voice is not None: if voice is not None:
params["text_speaker"] = voice params["voice"] = voice
# send request # send request
try: try:
response = self._session.post(self.URI_BASE, params=params) response = self._session.post(self.URI_BASE, json=params)
except ConnectionError: except ConnectionError:
time.sleep(random.randrange(1, 7)) time.sleep(random.randrange(1, 7))
response = self._session.post(self.URI_BASE, params=params) response = self._session.post(self.URI_BASE, json=params)
return response.json() return response.json()
@ -148,18 +145,10 @@ class TikTok:
class TikTokTTSException(Exception): class TikTokTTSException(Exception):
def __init__(self, code: int, message: str): def __init__(self, status_code, message):
self._code = code self.status_code = status_code
self._message = message self.message = message
super().__init__(self.message)
def __str__(self) -> str:
if self._code == 1:
return f"Code: {self._code}, reason: probably the aid value isn't correct, message: {self._message}"
if self._code == 2:
return f"Code: {self._code}, reason: the text is too long, message: {self._message}"
if self._code == 4:
return f"Code: {self._code}, reason: the speaker doesn't exist, message: {self._message}"
return f"Code: {self._message}, reason: unknown, message: {self._message}" def __str__(self):
return f"Code: {self.status_code}, Message: {self.message}"

@ -52,7 +52,7 @@ elevenlabs_api_key = { optional = true, example = "21f13f91f54d741e2ae27d2ab1b99
aws_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for AWS Polly" } aws_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for AWS Polly" }
streamlabs_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for Streamlabs Polly" } streamlabs_polly_voice = { optional = false, default = "Matthew", example = "Matthew", explanation = "The voice used for Streamlabs Polly" }
tiktok_voice = { optional = true, default = "en_us_001", example = "en_us_006", explanation = "The voice used for TikTok TTS" } tiktok_voice = { optional = true, default = "en_us_001", example = "en_us_006", explanation = "The voice used for TikTok TTS" }
tiktok_sessionid = { optional = true, example = "c76bcc3a7625abcc27b508c7db457ff1", explanation = "TikTok sessionid needed if you're using the TikTok TTS. Check documentation if you don't know how to obtain it." } #tiktok_sessionid = { optional = true, example = "c76bcc3a7625abcc27b508c7db457ff1", explanation = "TikTok sessionid needed if you're using the TikTok TTS. Check documentation if you don't know how to obtain it." }
python_voice = { optional = false, default = "1", example = "1", explanation = "The index of the system tts voices (can be downloaded externally, run ptt.py to find value, start from zero)" } python_voice = { optional = false, default = "1", example = "1", explanation = "The index of the system tts voices (can be downloaded externally, run ptt.py to find value, start from zero)" }
py_voice_num = { optional = false, default = "2", example = "2", explanation = "The number of system voices (2 are pre-installed in Windows)" } py_voice_num = { optional = false, default = "2", example = "2", explanation = "The number of system voices (2 are pre-installed in Windows)" }
silence_duration = { optional = true, example = "0.1", explanation = "Time in seconds between TTS comments", default = 0.3, type = "float" } silence_duration = { optional = true, example = "0.1", explanation = "Time in seconds between TTS comments", default = 0.3, type = "float" }

Loading…
Cancel
Save