-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_video_cache.ts
More file actions
169 lines (144 loc) · 6.34 KB
/
Copy pathgenerate_video_cache.ts
File metadata and controls
169 lines (144 loc) · 6.34 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
import { mkdir, unlink } from 'fs/promises';
import { existsSync } from 'fs';
import path from 'path';
import { entities } from '../src/entities';
import type { Config } from '../src/types/config';
import { generateGridVideo } from '../src/grid_video';
const BASE_SEED = '0';
const OUTPUT_DIR = path.join(__dirname, '..', 'cache', 'video');
const IMAGES_DIR = path.join(__dirname, '..', 'cache', 'images');
const TREE_SCALE = 1;
/**
* VideoGenerator - Generates videos for each entity
* and saves them under cache/video
*/
export class VideoGenerator {
private baseSeed: string;
private outputDir: string;
constructor(baseSeed: string = BASE_SEED, outputDir: string = OUTPUT_DIR) {
this.baseSeed = baseSeed;
this.outputDir = outputDir;
}
private getSeed(index: number): string {
return `${this.baseSeed}${index.toString().padStart(4, '0')}`;
}
private async ensureOutputDir(): Promise<void> {
if (!existsSync(this.outputDir)) {
await mkdir(this.outputDir, { recursive: true });
console.log(`✅ Created output directory: ${this.outputDir}`);
}
}
async generateAll(forceOverwrite: boolean = false): Promise<void> {
console.log(`\n🎬 Video Generator`);
console.log(` Base Seed: ${this.baseSeed}`);
console.log(` Output: ${this.outputDir}\n`);
await this.ensureOutputDir();
console.log(`📦 Generating videos for ${entities.size} entities...\n`);
for (const [entityName, entity] of entities) {
const numVariations = entity.variants;
console.log(`🔄 Processing: ${entityName} (${numVariations} variants)`);
for (let i = 0; i < numVariations; i++) {
const gridOutputPath = path.join(this.outputDir, `${entityName}_${i}.webm`);
if (!forceOverwrite && existsSync(gridOutputPath)) {
console.log(` ⏩ Skipping ${entityName}_${i} (already cached)`);
continue;
}
const seed = this.getSeed(i);
const entityConfig: Config = {
photoOnly: false,
width: 480,
height: 480,
fps: 25,
durationSeconds: 30,
seed: seed,
filename: 'video.webm',
imageFilename: 'image.png',
padding: 80,
save_as_file: true
};
try {
const result = await entity.generate.generate(null as any, undefined, entityConfig);
if (result.videoPath) {
const treePngPath = path.join(IMAGES_DIR, `${entityName}_${i}.png`);
await generateGridVideo(treePngPath, result.videoPath, gridOutputPath, TREE_SCALE, "winter");
// Delete the original non-grid video
if (existsSync(result.videoPath) && result.videoPath.includes('/cache/')) {
await unlink(result.videoPath);
}
} else {
console.error(` ✗ No video generated for ${entityName}_${i}`);
}
} catch (error) {
console.error(` ✗ Error generating ${entityName}_${i}:`, error);
}
}
console.log(` ✓ Generated ${numVariations} variations for ${entityName}`);
}
console.log(`\n✅ Video generation complete!`);
console.log(` Output directory: ${this.outputDir}\n`);
}
async generateForEntity(entityName: string, forceOverwrite: boolean = false): Promise<void> {
await this.ensureOutputDir();
const entity = entities.get(entityName);
if (!entity) {
console.error(`Entity "${entityName}" not found. Available entities:`);
for (const name of entities.keys()) {
console.log(` - ${name}`);
}
return;
}
const numVariations = entity.variants;
console.log(`\n🎬 Video Generator - ${entityName}`);
console.log(` Base Seed: ${this.baseSeed}`);
console.log(` Variations: ${numVariations}\n`);
console.log(`🔄 Processing: ${entityName}`);
for (let i = 0; i < numVariations; i++) {
const gridOutputPath = path.join(this.outputDir, `${entityName}_${i}.webm`);
if (!forceOverwrite && existsSync(gridOutputPath)) {
console.log(` ⏩ Skipping ${entityName}_${i} (already cached)`);
continue;
}
const seed = this.getSeed(i);
const entityConfig: Config = {
photoOnly: false,
width: 480,
height: 480,
fps: 25,
durationSeconds: 30,
seed: seed,
filename: 'video.webm',
imageFilename: 'image.png',
padding: 80,
save_as_file: true
};
try {
const result = await entity.generate.generate(null as any, undefined, entityConfig);
if (result.videoPath) {
const treePngPath = path.join(IMAGES_DIR, `${entityName}_${i}.png`);
await generateGridVideo(treePngPath, result.videoPath, gridOutputPath, TREE_SCALE);
// Delete the original non-grid video
if (existsSync(result.videoPath) && result.videoPath.includes('/cache/')) {
await unlink(result.videoPath);
}
} else {
console.error(` ✗ No video generated for ${entityName}_${i}`);
}
} catch (error) {
console.error(` ✗ Error generating ${entityName}_${i}:`, error);
}
}
console.log(` ✓ Generated ${numVariations} variations`);
console.log(`\n✅ Done!`);
}
}
async function main() {
const generator = new VideoGenerator(BASE_SEED, OUTPUT_DIR);
const entityArg = process.argv[2];
const forceArg = process.argv.includes('--force');
if (entityArg) {
await generator.generateForEntity(entityArg, forceArg);
} else {
await generator.generateAll(forceArg);
}
}
main().catch(console.error);