-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews_video.py
79 lines (65 loc) · 2.46 KB
/
news_video.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
import requests
import json
import time
class VideoGenerator:
def __init__(self, api_key):
self.api_key = api_key
def generate_video(self, input_text, source_url):
url = "https://api.d-id.com/talks"
payload = {
"script": {
"type": "text",
"subtitles": "false",
"provider": {
"type": "microsoft",
"voice_id": "en-US-JennyNeural"
},
"ssml": "false",
"input": input_text
},
"config": {
"fluent": "false",
"pad_audio": "0.0"
},
"source_url": source_url
}
headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {self.api_key}"
}
# Initial request to generate video
response = requests.post(url, json=payload, headers=headers)
_response = response.json()
print("Initial Response: ", _response)
if 'status' not in _response:
print("Error in initial response:", _response)
return None
# Polling until the status is 'created'
while _response["status"] != "created":
print("Polling for status...")
time.sleep(10) # Wait before polling again
response = requests.post(url, json=payload, headers=headers)
_response = response.json()
if 'status' not in _response:
print("Error in polling response:", _response)
return None
talk_id = _response['id']
talk_url = f"{url}/{talk_id}"
headers = {
"accept": "application/json",
"authorization": f"Bearer {self.api_key}"
}
# Polling until the video generation is done
while True:
print("Polling for video status...")
response = requests.get(talk_url, headers=headers)
video_response = response.json()
if 'status' not in video_response:
print("Error in video response:", video_response)
return None
if video_response["status"] == "done":
break
time.sleep(10) # Wait before polling again
video_url = video_response["result_url"]
return video_url