-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_to_speech.py
82 lines (54 loc) · 1.91 KB
/
text_to_speech.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
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
import requests
import json
import os
import time
from api_keys import api_key
def text_to_speech(content):
url = "https://play.ht/api/v1/convert"
payload = {
"content": [content],
"voice": "en-US-JennyNeural"
}
headers = {
"accept": "application/json",
"content-type": "application/json",
"AUTHORIZATION": api_key.AUTHORIZATION,
"X-USER-ID": api_key.X_USER_ID
}
response = requests.post(url, json=payload, headers=headers)
response_text=response.text
response_dict=json.loads(response_text)
transcription_id = response_dict['transcriptionId']
print(transcription_id)
return transcription_id
def get_audio_file_url(transcription_id):
url = f'https://play.ht/api/v1/articleStatus?transcriptionId={transcription_id}'
headers = {
"accept": "application/json",
"AUTHORIZATION": "c0992c1fed8447c8b7e11c1d666c5e2f",
"X-USER-ID": "hteBfjHzFgT9MRZ6ehQ4vT2d8vz2"
}
print("Converting text to audio ....")
time.sleep(30)
response = requests.get(url, headers=headers)
print(response.text)
response_text=response.text
response_dict=json.loads(response_text)
audioUrl = response_dict['audioUrl']
print(audioUrl)
return audioUrl
def download_audio_file(audioUrl):
url=audioUrl
directory_path = "audio"
file_name = "audio_file.mp3"
# Create the directory if it doesn't exist
if not os.path.exists(directory_path):
os.makedirs(directory_path)
file_path = os.path.join(directory_path, file_name)
response = requests.get(url)
if response.status_code == 200:
with open(file_path, "wb") as file:
file.write(response.content)
print("File downloaded and saved successfully.")
else:
print("Failed to download the file. Status code:", response.status_code)