Spaces:
Sleeping
Sleeping
File size: 5,181 Bytes
dec3ec5 |
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import requests
import json
from moviepy.editor import VideoFileClip
import os
API_KEY = "YTBjYzk5MjI1ZWQzNGViOTgxMTI0NjM2OGQ5NDc2OGEtMTczMzk5MjA5MA=="
AUDIO_URL = "https://drive.google.com/file/d/1CF8TjWwPWiIPJX3D1bjpMopinHt4iw1D/view?usp=sharing"
# API ENDPOINTS
#GET
VIDEO_GENERATION_STATUS = "https://api.heygen.com/v1/video_status.get"
LIST_TALKING_PHOTOS = "https://api.heygen.com/v1/talking_photo.list"
#POST
VIDEO_GENERATION = "https://api.heygen.com/v2/video/generate"
UPLOAD_PHOTO = "https://upload.heygen.com/v1/talking_photo"
def video_generation_payload(title, avatar_id, audio_url):
return {
"title": title,
"caption": False,
# "callback_id": "callback_id",
"dimension": {
"width": 720,
"height": 1280
},
"video_inputs": [
{
"character": {
"type": "avatar",
"avatar_id": avatar_id,
"scale": 1.0,
"avatar_style": "normal",
"offset": {
"x": 0.0,
"y": 0.0
},
"matting": False
},
"voice": {
"type": "audio",
"audio_url": audio_url
},
}
],
# "callback_url": "callback_url"
}
def talking_photo_payload(title, talking_photo_id, audio_url):
print(f"Creating video with title: {title}, talking_photo_id: {talking_photo_id}, audio_url: {audio_url}")
return {
"title": title,
"caption": False,
# "callback_id": "callback_id",
"dimension": {
"width": 720,
"height": 1280
},
"video_inputs": [
{
"character": {
"type": "talking_photo",
"talking_photo_id": talking_photo_id
},
"voice": {
"type": "audio",
"audio_url": audio_url
},
"talking_style": "stable",
"expression": "happy",
}
],
# "callback_url": "callback_url"
}
def upload_photo(file_path, type):
print(f"Uploading photo {file_path}")
with open(file_path, "rb") as f:
resp = requests.post(UPLOAD_PHOTO, data=f, headers={"Content-Type": type, "x-api-key": API_KEY}).json()
try:
talking_photo_id = resp["data"]["talking_photo_id"]
return talking_photo_id
except Exception as e:
print(f"Error uploading photo: {e}")
return resp
def list_talking_photos():
print("Listing talking photos")
headers = {
"accept": "application/json",
"x-api-key": API_KEY
}
response = requests.get(LIST_TALKING_PHOTOS, headers=headers).json()
return response
def download_video(video_id, video_url):
download_video = requests.get(video_url)
print(f"Downloading video {video_id}")
with open(f"{video_id}.mp4", "wb") as f:
f.write(download_video.content)
def create_video(title, avatar_id, audio_url):
print(f"Creating video with title: {title}, avatar_id: {avatar_id}, audio_url: {audio_url}")
headers = {
"accept": "application/json",
"content-type": "application/json",
"x-api-key": API_KEY
}
payload = talking_photo_payload(title, avatar_id, audio_url)
response = requests.post(VIDEO_GENERATION, json=payload, headers=headers).json()
try:
video_id = response["data"]["video_id"]
print(f"Video ID: {video_id}")
return video_id
except Exception as e:
print(f"Error creating video: {e}")
return response
def get_video_status(video_id):
headers = {
"accept": "application/json",
"x-api-key": API_KEY
}
response = requests.get(VIDEO_GENERATION_STATUS, headers=headers, params={"video_id": video_id}).json()
print(f"Video status: {response['data']['status']}")
return {
"id": response["data"]["id"],
"status": response["data"]["status"],
"video_url": response["data"]["video_url"],
"thumbnail_url": response["data"]["thumbnail_url"]
}
def split_video(video_id):
print(f"Splitting video {video_id}.mp4")
with open("clips_metadata.json", "r") as f:
clips_metadata = json.load(f)
video = VideoFileClip(f"{video_id}.mp4")
clips = []
for clip_data in clips_metadata:
start_time = clip_data["start_time_ms"]
end_time = clip_data["end_time_ms"]
# convert milliseconds to seconds
start_time /= 1000
end_time /= 1000
try:
clip_name = clip_data["file_name"].split(".")[0]+".mp4"
clip = video.subclip(start_time, end_time)
clips.append(clip)
except Exception as e:
print(f"Error in clip {clip_name}: {e}")
clips_dir = f"{video_id}_clips"
os.makedirs(clips_dir, exist_ok=True)
print(f"Saving clips to {clips_dir}")
for i, clip in enumerate(clips):
clip_path = os.path.join(clips_dir, clips_metadata[i]["file_name"].split(".")[0]+".mp4")
clip.write_videofile(clip_path, codec="libx264", audio_codec="aac") |