-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
285 lines (243 loc) · 10.2 KB
/
Copy pathapp.js
File metadata and controls
285 lines (243 loc) · 10.2 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const canvas = document.getElementById('render-canvas');
const ctx = canvas.getContext('2d');
const setupPanel = document.getElementById('setup-panel');
const progressSection = document.getElementById('progress-section');
const progressFill = document.getElementById('progress-fill');
const progressText = document.getElementById('progress-text');
const progressStages = document.getElementById('progress-stages');
const framesPreview = document.getElementById('frames-preview');
const framesGrid = document.getElementById('frames-grid');
const outputSection = document.getElementById('output-section');
const outputVideo = document.getElementById('output-video');
const downloadBtn = document.getElementById('download-btn');
const generateBtn = document.getElementById('generate-btn');
const newVideoBtn = document.getElementById('new-video-btn');
const stages = [
{ id: 'init', text: 'Initializing...', icon: 'Loading' },
{ id: 'gen', text: 'Generating AI images', icon: 'Generating' },
{ id: 'process', text: 'Processing frames', icon: 'Processing' },
{ id: 'encode', text: 'Creating video', icon: 'Creating' },
{ id: 'done', text: 'Complete!', icon: 'Done' }
];
const stylePrompts = {
'cinematic': 'cinematic lighting, dramatic, film still, 8k uhd, high quality',
'anime': 'anime style, manga art, vibrant colors, high quality anime',
'realistic': 'photorealistic, ultra detailed, 8k uhd, sharp focus',
'fantasy': 'fantasy art, magical, ethereal, enchanted, detailed illustration',
'3d': '3d render, octane render, unreal engine, cgi, volumetric lighting',
'watercolor': 'watercolor painting, soft colors, artistic, gallery quality',
'cyberpunk': 'cyberpunk, neon lights, futuristic city, rain, dark atmosphere',
'oil': 'oil painting, classical art, rich colors, canvas texture, masterpiece',
'pixel': 'pixel art, retro game style, 16-bit, detailed pixel artwork',
'comic': 'comic book style, bold lines, vibrant colors, graphic novel art'
};
function updateStages(currentId) {
progressStages.innerHTML = '';
stages.forEach((stage, idx) => {
const currentIdx = stages.findIndex(s => s.id === currentId);
const div = document.createElement('div');
div.className = 'stage-item ' + (stage.id === currentId ? 'stage-active' : idx < currentIdx ? 'stage-done' : 'stage-waiting');
div.innerHTML = `[${stage.icon}] ${stage.text}`;
progressStages.appendChild(div);
});
}
function setProgress(pct) {
progressFill.style.width = `${Math.min(pct, 100)}%`;
progressText.textContent = `${Math.min(pct, 100)}%`;
}
function showProgress() {
progressSection.classList.remove('hidden');
outputSection.classList.add('hidden');
framesPreview.classList.add('hidden');
framesGrid.innerHTML = '';
setProgress(0);
}
function hideProgress() {
progressSection.classList.add('hidden');
}
function showFrames(urls) {
framesPreview.classList.remove('hidden');
framesGrid.innerHTML = '';
urls.forEach((url, i) => {
const div = document.createElement('div');
div.className = 'frame-item';
div.innerHTML = `<img src="${url}" alt="Frame ${i+1}"><span class="frame-num">${i+1}</span>`;
framesGrid.appendChild(div);
});
}
function loadImage(src) {
return new Promise((resolve, reject) => {
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => resolve(img);
img.onerror = reject;
img.src = src;
});
}
async function generateAIVideo() {
const prompt = document.getElementById('video-prompt').value.trim();
if (!prompt) {
alert('Please describe your video');
return;
}
const style = document.getElementById('video-style').value;
const numFrames = parseInt(document.getElementById('num-frames').value);
const [width, height] = document.getElementById('resolution').value.split('x').map(Number);
const fps = parseInt(document.getElementById('fps').value);
const transition = document.getElementById('transition').value;
const model = document.getElementById('model').value;
const fullPrompt = `${prompt}, ${stylePrompts[style]}`;
showProgress();
setupPanel.classList.add('hidden');
updateStages('init');
generateBtn.disabled = true;
try {
updateStages('gen');
const frameUrls = [];
const seeds = [];
for (let i = 0; i < numFrames; i++) {
seeds.push(Math.floor(Math.random() * 999999));
}
for (let i = 0; i < numFrames; i++) {
setProgress(5 + Math.round((i / numFrames) * 40));
updateStages('gen');
const seed = seeds[i];
const modelName = model === 'turbo' ? 'turbo' : 'flux';
const url = `https://image.pollinations.ai/prompt/${encodeURIComponent(fullPrompt)}?width=${width}&height=${height}&seed=${seed}&model=${modelName}&nologo=true`;
await new Promise(resolve => {
const img = new Image();
img.crossOrigin = 'anonymous';
img.onload = () => {
frameUrls.push(url);
resolve();
};
img.onerror = () => resolve();
img.src = url;
});
}
if (frameUrls.length === 0) {
throw new Error('Failed to generate any images');
}
showFrames(frameUrls);
updateStages('process');
setProgress(50);
const transitionFrames = 5;
const allCanvases = [];
for (let i = 0; i < frameUrls.length; i++) {
const img = await loadImage(frameUrls[i]);
if (transition === 'zoom') {
for (let f = 0; f <= transitionFrames; f++) {
canvas.width = width;
canvas.height = height;
const scale = 1 + (f * 0.08);
const drawW = width * scale;
const drawH = height * scale;
const x = (width - drawW) / 2;
const y = (height - drawH) / 2;
ctx.drawImage(img, x, y, drawW, drawH);
allCanvases.push(canvas.toDataURL('image/jpeg', 0.92));
}
} else if (transition === 'pan') {
for (let f = 0; f <= transitionFrames; f++) {
canvas.width = width;
canvas.height = height;
const scale = 1.4;
const drawW = width * scale;
const drawH = height * scale;
const x = -((drawW - width) * (f / transitionFrames));
const y = (height - drawH) / 2;
ctx.drawImage(img, x, y, drawW, drawH);
allCanvases.push(canvas.toDataURL('image/jpeg', 0.92));
}
} else if (transition === 'fade' && i > 0) {
const prevImg = await loadImage(frameUrls[i - 1]);
for (let f = 0; f <= transitionFrames; f++) {
canvas.width = width;
canvas.height = height;
const alpha = f / transitionFrames;
ctx.globalAlpha = 1 - alpha;
ctx.drawImage(prevImg, 0, 0, width, height);
ctx.globalAlpha = alpha;
ctx.drawImage(img, 0, 0, width, height);
ctx.globalAlpha = 1;
allCanvases.push(canvas.toDataURL('image/jpeg', 0.92));
}
} else {
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, width, height);
for (let f = 0; f < 3; f++) {
allCanvases.push(canvas.toDataURL('image/jpeg', 0.92));
}
}
setProgress(50 + Math.round((i / frameUrls.length) * 25));
}
updateStages('encode');
setProgress(80);
const videoBlob = await recordVideo(allCanvases, fps);
const videoUrl = URL.createObjectURL(videoBlob);
showOutput(videoUrl, 'ai-video.webm');
updateStages('done');
setProgress(100);
} catch (error) {
alert('Error: ' + error.message);
}
hideProgress();
setupPanel.classList.remove('hidden');
generateBtn.disabled = false;
}
function recordVideo(dataUrls, fps) {
return new Promise((resolve, reject) => {
canvas.width = 768;
canvas.height = 512;
const stream = canvas.captureStream(fps);
const mediaRecorder = new MediaRecorder(stream, {
mimeType: 'video/webm;codecs=vp9',
videoBitsPerSecond: 2500000
});
const chunks = [];
mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) chunks.push(e.data);
};
mediaRecorder.onstop = () => {
const blob = new Blob(chunks, { type: 'video/webm' });
resolve(blob);
};
mediaRecorder.onerror = () => reject(new Error('Recording failed'));
mediaRecorder.start();
let frameIndex = 0;
const img = new Image();
function drawNextFrame() {
if (frameIndex >= dataUrls.length) {
setTimeout(() => mediaRecorder.stop(), 100);
return;
}
img.onload = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
frameIndex++;
setTimeout(drawNextFrame, 1000 / fps);
};
img.onerror = () => {
frameIndex++;
drawNextFrame();
};
img.src = dataUrls[frameIndex];
}
drawNextFrame();
});
}
function showOutput(url, filename) {
outputSection.classList.remove('hidden');
outputVideo.src = url;
downloadBtn.href = url;
downloadBtn.download = filename;
}
newVideoBtn.addEventListener('click', () => {
outputSection.classList.add('hidden');
setupPanel.classList.remove('hidden');
});
generateBtn.addEventListener('click', generateAIVideo);
document.getElementById('video-prompt').addEventListener('keypress', (e) => {
if (e.key === 'Enter' && e.ctrlKey) generateAIVideo();
});