-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_generator.py
88 lines (74 loc) · 3.26 KB
/
video_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
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
83
84
85
86
87
88
import requests
import json
import time
def make_video_call_request(usename: str, passwd: str, source_url:str, input_text:str):
auth = (usename, passwd)
url = "https://api.d-id.com/talks"
payload = {
"source_url": source_url,
"script": {
"type": "text",
"subtitles": "false",
"provider": {
"type": "microsoft",
"voice_id": "en-US-JennyNeural"
},
"input": input_text
},
"config": {
"fluent": "false",
"pad_audio": "0.0"
}
}
headers = {
"accept": "application/json",
"content-type": "application/json"
}
response = requests.post(url, auth=auth, json=payload, headers=headers)
print(response.text)
job_id = json.loads(response.text)["id"]
return job_id
def extract_video_link(usename: str, passwd: str, job_id: str):
auth = (usename, passwd)
url = f"https://api.d-id.com/talks/{job_id}"
headers = {
"accept": "application/json",
"content-type": "application/json"
}
response = requests.get(url, auth=auth, headers=headers)
print(response.text)
data = json.loads(response.text)["result_url"]
return data
def download_video(url, filename):
"""
Downloads a video from the given URL and saves it to the specified filename.
Args:
url (str): The URL of the video.
filename (str): The filename to save the video as.
"""
try:
response = requests.get(url, stream=True)
response.raise_for_status() # Raise an exception for unsuccessful requests
except requests.exceptions.RequestException as e:
print(f"Error downloading video: {e}")
return
# Check for video content type
if not response.headers.get('Content-Type', '').startswith('video/'):
print(f"URL '{url}' does not appear to contain a video.")
return
# Open the file for writing in binary mode
with open(filename, 'wb') as f:
for chunk in response.iter_content(1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
print(f"Video downloaded successfully to '{filename}'.")
if __name__ == "__main__":
job_id = make_video_call_request(usename="bmVlbEBhdmlhdG8uY29uc3VsdGluZw", passwd="nozKyVtVAVQlIwuTw3n9X", source_url="https://create-images-results.d-id.com/api_docs/assets/alice_getting_started_v3.png", input_text="""Hey Neel happy weekend,
I hope you're feeling okay today. It's important to take care of yourself, so make sure to get plenty of rest and stay hydrated by drinking lots of fluids like water, clear broth, or herbal tea. You might find over-the-counter pain relievers like Tylenol or Advil helpful for reducing fever and aches. A humidifier or a hot shower can also relieve congestion. Lastly, try to avoid contact with others to prevent spreading your illness. Take care and get well soon!""")
print(job_id)
print('-------------')
time.sleep(20)
video_url =extract_video_link(usename="bmVlbEBhdmlhdG8uY29uc3VsdGluZw", passwd="nozKyVtVAVQlIwuTw3n9X", job_id=job_id)
# video_url = extract_video_link(usename="bmVlbEBhdmlhdG8uY29uc3VsdGluZw", passwd="nozKyVtVAVQlIwuTw3n9X", job_id="tlk_haaNlxDsVAUBWN3lTT9fu")
print("---------")
download_video(url=video_url, filename="intro2.mp4")