Skip to content

Instantly share code, notes, and snippets.

@hugeblank
Last active December 31, 2023 23:09
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugeblank/6c59ae6ea24f908d249f754141971e4f to your computer and use it in GitHub Desktop.
Save hugeblank/6c59ae6ea24f908d249f754141971e4f to your computer and use it in GitHub Desktop.
Tiktok speech to text parser
#!/bin/zsh
# Tiktok speech to text parser
# by hugeblank, April 2022
# API endpoint & oneshot reading and playing discovered by @scanlime
# https://twitter.com/scanlime/status/1512288857596653568
# First argument - string of text to read
# Second argument - voice to use
# Usage examples:
# ./tiktok.sh play "this is a test of my patience" en_us_002
# ./tiktok.sh save "this will get saved to out.mp3" en_us_009
# NOTE: The endpoint appears to have a hard limit of about 300 characters.
# Valid Voices (tested from 0-30 for each language):
# en_us_001 = Standard female
# en_us_002 = Standard female (same as 001 it seems.)
# en_us_006 = British male 1
# en_us_007 = Standard male 1
# en_us_009 = Standard male 2
# en_us_010 = British male 2
# en_uk_001 = Too British male 1
# en_uk_003 = Too British male 2
# en_au_001 = Australian female 1
# en_au_002 = Australian male 1
# For more, see: https://github.com/oscie57/tiktok-voice#voice-options
str=`echo $2 | tr ' ' '+'` # sanitize spaces
result=`curl -s -X POST 'https''://api16-normal-useast5.us.tiktokv.com/media/api/text/speech/invoke/?text_speaker='$3'&req_text='$str` # make request
statcode=`echo $result | jq .status_code` # make sure request was successful
if [ $statcode -ne 0 ]
then
echo "could not parse output"
echo $result
exit $statcode
fi
output=`echo -En $result | jq .data.v_str | base64 -di` # convert output
if [[ $1 == "play" ]]
then # play one-off sound
echo -En $output | ffplay -volume 10 -f mp3 -autoexit -i -
elif [[ $1 == "save" ]]
then # save to an output (out.mp3)
rm out.mp3 2>/dev/null
echo -En $output | ffmpeg -f mp3 -i - out.mp3
else # option one wasn't one of play or save
echo "Expected first argument to be 'save' or 'play'"
exit 1
fi
if [ $? -ne 0 ] # either ffplay or ffmpeg errored
then
# both make it very apparent that they errored, this is effectively moot.
echo "couldn't decode sound"
exit $?
fi
@frozenpandaman
Copy link

thanks a ton for this! i had to install jq for this (understandably), and had to modify line 43 to read as follows – on macOS where base64 works a bit differently, i think:

output=`echo -En $result | jq .data.v_str | tr -d '"' | base64 -d`

@JiBB
Copy link

JiBB commented Apr 26, 2022

A cleaner change to make things work on macOS, where base64 doesn't support the -i option to ignore invalid input characters, is to ask jq not to include them with the -r (--raw-output) flag:

output=`echo -En $result | jq -r .data.v_str | base64 -d` # convert output`

@frozenpandaman
Copy link

hah, that's a much better solution. thanks @JiBB!

@hugeblank
Copy link
Author

I don't know why I didn't mention this before, but this script broke a while ago. There's a fix that was just pushed to oscie57's tiktok-voice that I'm awaiting permission to implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment