Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active April 15, 2023 19:06
Show Gist options
  • Save crazy4groovy/b4ccee4356ec3e85c979197e38023799 to your computer and use it in GitHub Desktop.
Save crazy4groovy/b4ccee4356ec3e85c979197e38023799 to your computer and use it in GitHub Desktop.
Text-to-audio MP3 AI API call via Eleven Labs, IP throttled (PowerShell)
import { parse } from 'https://deno.land/std/flags/mod.ts';
console.log('Example: deno run --allow-all .\ai-text-to-speech.deno.js --outFile="sample.mp3" --text="This is a sample audio body." --voice="Josh"')
const { outFile, text, voice = 'Adam' } = parse(Deno.args)
if (!outFile || !text) {
console.error("Missing required arg: outFile and/or text")
}
const opts = {
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9,uk;q=0.8",
"cache-control": "no-cache",
"content-type": "application/json",
"pragma": "no-cache",
"sec-ch-ua": "\"Google Chrome\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"Referer": "https://beta.elevenlabs.io/",
"Referrer-Policy": "strict-origin-when-cross-origin"
},
"body": `{\"text\":\"${text}\",\"model_id\":null,\"language_id\":null}`,
"method": "POST"
}
// language_id: "en-us", model_id: "prod"
const voices = {
Adam: 'text-to-speech/pNInz6obpgDQGcFmaJgB/stream',
Antoni: 'text-to-speech/ErXwobaYiN019PkySvjV/stream',
Arnold: 'text-to-speech/VR6AewLTigWG4xSOukaG/stream',
Bella: 'text-to-speech/EXAVITQu4vr4xnSDxMaL/stream',
Domi: 'text-to-speech/AZnzlk1XvdvUeBnXmlld/stream',
Elli: 'text-to-speech/MF3mGyEYCl7XYWbV9V6O/stream',
Josh: 'text-to-speech/TxGEqnHWrfWFTfGW9XjX/stream',
Rachel: 'text-to-speech/21m00Tcm4TlvDq8ikWAM/stream',
Sam: 'text-to-speech/yoZ06aMxZJJ28mfd3POQ/stream'
}
const response = await fetch(`https://api.elevenlabs.io/v1/${voices[voice]}`, opts);
if (response.ok) {
const data = await response.arrayBuffer();
await Deno.writeFile(outFile, new Uint8Array(data));
console.log(`Saved MP3 to ${outFile}`);
} else {
console.error(`Failed to fetch: ${response.status} ${response.statusText} ${await response.text()}`);
}
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[string]$Text,
[Parameter(Mandatory=$true)]
[string]$OutFile
)
Write-Host "This was taken from beta.elevenlabs.io -- if it stops working, find the call to /stream and copy what's needed??"
Write-Host "Args: -OutFile -Text"
Write-Host "Example: .\ai-text-to-mp3.ps1 -OutFile `"C:\Temp\111.mp3`" -Text `"The latest developments in AI, today.`""
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
Invoke-WebRequest `
-UseBasicParsing `
-Uri "https://api.elevenlabs.io/v1/text-to-speech/pNInz6obpgDQGcFmaJgB/stream" `
-Method "POST" `
-WebSession $session `
-Headers @{
"authority"="api.elevenlabs.io"
"method"="POST"
"path"="/v1/text-to-speech/pNInz6obpgDQGcFmaJgB/stream"
"scheme"="https"
"accept"="*/*"
"accept-encoding"="gzip, deflate, br"
"accept-language"="en-US,en;q=0.9,uk;q=0.8"
"cache-control"="no-cache"
"origin"="https://beta.elevenlabs.io"
"pragma"="no-cache"
"referer"="https://beta.elevenlabs.io/"
"sec-ch-ua"="`"Google Chrome`";v=`"111`", `"Not(A:Brand`";v=`"8`", `"Chromium`";v=`"111`""
"sec-ch-ua-mobile"="?0"
"sec-ch-ua-platform"="`"Windows`""
"sec-fetch-dest"="empty"
"sec-fetch-mode"="cors"
"sec-fetch-site"="same-site"
} `
-ContentType "application/json" `
-OutFile $OutFile `
-Body "{`"text`":`"$($Text)`",`"model_id`":null,`"language_id`":null}"
Write-Host "Completed download of MP3 to $($Text)."
Adam = text-to-speech/pNInz6obpgDQGcFmaJgB/stream
Antoni = text-to-speech/ErXwobaYiN019PkySvjV/stream
Arnold = text-to-speech/VR6AewLTigWG4xSOukaG/stream
Bella = text-to-speech/EXAVITQu4vr4xnSDxMaL/stream
Domi = text-to-speech/AZnzlk1XvdvUeBnXmlld/stream
Elli = text-to-speech/MF3mGyEYCl7XYWbV9V6O/stream
Josh = text-to-speech/TxGEqnHWrfWFTfGW9XjX/stream
Rachel = text-to-speech/21m00Tcm4TlvDq8ikWAM/stream
Sam = text-to-speech/yoZ06aMxZJJ28mfd3POQ/stream
Note: custom voice cloning!
https://beta.elevenlabs.io/voice-lab
Home page:
https://beta.elevenlabs.io/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment