-
Notifications
You must be signed in to change notification settings - Fork 35
/
voiceover_generator.py
28 lines (26 loc) · 1.01 KB
/
voiceover_generator.py
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
import requests
import os
import time
def generate_voiceover(story, save_file=False):
headers = {
"xi-api-key": os.getenv("ELEVENLABS_API_KEY"),
"Content-Type": "application/json",
"accept": "audio/mpeg"
}
data = {
"text": story + "..Comment with your favorite fact...",
"voice_settings": {"stability": 0.3, "similarity_boost": 0.3}
}
response = requests.post("https://api.elevenlabs.io/v1/text-to-speech/AZnzlk1XvdvUeBnXmlld", headers=headers, json=data)
if response.status_code == 200:
if save_file:
with open("file.mp3", "wb") as f:
f.write(response.content)
return response.content
else:
print(f"Error while generating voiceover with status code {response.status_code}")
return None
def save_voiceover(voiceover_content, timestamp):
voiceover_filename = f"voiceover_{timestamp}.mp3"
with open(voiceover_filename, "wb") as f:
f.write(voiceover_content)