-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
196 lines (154 loc) · 5.37 KB
/
script.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const VIDEO_API_ENDPOINT = "https://maya-mongo-api.adaptable.app/crud"
const VIDEO_GENERATION_ENDPOINT = "http://localhost:5003"
async function generateVideo(currentStyle, prompt) {
let styles = {
"mickey-mouse": "Mickey Mouse",
spongebob: "SpongeBob",
}
let request = await fetch(`${VIDEO_GENERATION_ENDPOINT}/generate`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
title: styles[currentStyle],
story: prompt,
}),
})
let response = await request.json()
let videoId = response.uuid
let title = response.title
let description = response.description
let videoURL = `${VIDEO_GENERATION_ENDPOINT}/${videoId}.mp4`
let thumbnailURL = `${VIDEO_GENERATION_ENDPOINT}/${videoId}_0.png`
console.log({ videoURL, thumbnailURL, title, description })
fetch(`${VIDEO_GENERATION_ENDPOINT}/delete/${videoId}`)
return {
videoURL,
thumbnailURL,
title,
description,
}
}
let currentStyle = "mickey-mouse"
function selectCartoonStyle(style) {
const buttons = document.querySelectorAll(".cartoon-style-button")
buttons.forEach((button) => {
if (button.classList.contains(style)) {
button.classList.add("selected")
} else {
button.classList.remove("selected")
}
})
currentStyle = style
}
// Function to handle button clicks
function handleButtonClick(event) {
const style = event.target.dataset.style
selectCartoonStyle(style)
}
// Add click event listeners to buttons
const buttons = document.querySelectorAll(".cartoon-style-button")
buttons.forEach((button) => {
button.addEventListener("click", handleButtonClick)
})
// Select the default cartoon style (SpongeBob)
selectCartoonStyle(currentStyle)
let stopLoading = false
// Function to simulate loading
function simulateLoading() {
// Show loader inside the button
const loader = document.createElement("span")
loader.classList.add("loader")
// Remove any existing loaders before adding a new one
const existingLoader = document.querySelector("#generate-button .loader")
if (existingLoader) {
existingLoader.remove()
}
document.getElementById("generate-button").appendChild(loader)
// Disable the button during loading
document.getElementById("generate-button").classList.add("disabled")
stopLoading = () => {
// Remove loader
document.getElementById("generate-button").removeChild(loader)
// Enable the button again
document.getElementById("generate-button").classList.remove("disabled")
}
}
// Add click event listener to the "Generate Video" button
document
.getElementById("generate-button")
.addEventListener("click", async () => {
simulateLoading()
const prompt = document.getElementById("user-prompt").value
const { videoURL, thumbnailURL, title, description } =
await generateVideo(currentStyle, prompt)
const videoDiv = document.getElementById("generated-video")
const video = videoDiv.querySelector("video")
video.src = videoURL
video.poster = thumbnailURL
video.load()
setTitleAndDescription(title, description)
stopLoading()
})
async function sleep(timeInMilliSeconds) {
return new Promise((resolve) => setTimeout(resolve, timeInMilliSeconds))
}
function setTitleAndDescription(title, description) {
document.querySelector(".videoTitle").innerHTML = title
document.querySelector(".videoDescription").innerHTML = description
}
function constructThumbnail(asset) {
let thumbnail = document.createElement("div")
thumbnail.classList.add("thumbnail")
let img = document.createElement("img")
img.src = `${VIDEO_GENERATION_ENDPOINT}/${asset.uuid}_0.png`
img.classList.add("thumbnail-image")
img.alt = asset.episodeTitle
thumbnail.appendChild(img)
let thumbnailContent = document.createElement("div")
thumbnailContent.classList.add("thumbnail-content")
let thumbnailContentTitle = document.createElement("span")
thumbnailContentTitle.classList.add("thumbnail-content-title")
thumbnailContentTitle.innerHTML = asset.episodeTitle
thumbnailContent.appendChild(thumbnailContentTitle)
let thumbnailContentDuration = document.createElement("span")
thumbnailContentDuration.classList.add("thumbnail-content-duration")
thumbnailContentDuration.innerHTML = asset.duration + "s"
thumbnailContent.appendChild(thumbnailContentDuration)
thumbnail.appendChild(thumbnailContent)
return thumbnail
}
function constructThumbnails(assets) {
let thumbnails = document.createElement("div")
thumbnails.classList.add("thumbnails")
assets.forEach((asset) => {
let thumbnail = constructThumbnail(asset)
thumbnails.appendChild(thumbnail)
})
return thumbnails
}
setTimeout(async () => {
let response = await fetch(`${VIDEO_API_ENDPOINT}/all`)
let assets = await response.json()
let thumbnails = constructThumbnails(assets)
let rightColumn = document.getElementById("right-column")
rightColumn.removeChild(rightColumn.querySelector(".loader"))
rightColumn.appendChild(thumbnails)
})
document.addEventListener("click", (event) => {
if (
event.target.classList.contains("thumbnail-content-duration") ||
event.target.classList.contains("thumbnail-content-title") ||
event.target.classList.contains("thumbnail-content") ||
event.target.classList == "thumbnail-image"
) {
let thumbnail = event.target
while (!thumbnail.classList.contains("thumbnail")) {
thumbnail = thumbnail.parentNode
}
let img = thumbnail.querySelector(".thumbnail-image")
let videoURL = img.src.replace("_0.png", ".mp4")
window.open(videoURL, "_blank")
}
})