save all changes
This commit is contained in:
88
src/generateVideo.ts
Normal file
88
src/generateVideo.ts
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import { query } from './lib/mysql';
|
||||||
|
import { generateVideo } from './lib/video-generator';
|
||||||
|
import { logger } from './lib/logger';
|
||||||
|
import dotenv from 'dotenv';
|
||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs/promises';
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
interface VideoRecord {
|
||||||
|
id: number;
|
||||||
|
genre: string;
|
||||||
|
sub_genre: string;
|
||||||
|
video_prompt: string;
|
||||||
|
image_path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const servers = [
|
||||||
|
{
|
||||||
|
baseUrl: process.env.SERVER1_COMFY_BASE_URL,
|
||||||
|
outputDir: process.env.SERVER1_COMFY_OUTPUT_DIR,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
baseUrl: process.env.SERVER2_COMFY_BASE_URL,
|
||||||
|
outputDir: process.env.SERVER2_COMFY_OUTPUT_DIR,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
async function processServerQueue(server: any, videos: VideoRecord[]) {
|
||||||
|
for (const video of videos) {
|
||||||
|
try {
|
||||||
|
const imageFileName = path.basename(video.image_path);
|
||||||
|
const serverImagePath = path.join(server.outputDir.replace('output','input'), imageFileName);
|
||||||
|
await fs.copyFile(video.image_path, serverImagePath);
|
||||||
|
|
||||||
|
const videoFileName = `${video.id}_${video.genre}_${video.sub_genre}.mp4`.replace(/\s/g, '_');
|
||||||
|
const videoPath = await generateVideo(
|
||||||
|
video.video_prompt,
|
||||||
|
imageFileName,
|
||||||
|
videoFileName,
|
||||||
|
server.baseUrl,
|
||||||
|
server.outputDir,
|
||||||
|
{ width: 720, height: 1280 }
|
||||||
|
);
|
||||||
|
|
||||||
|
const absolutePath = path.resolve(videoPath);
|
||||||
|
await query('UPDATE video SET video_path = ? WHERE id = ?', [absolutePath, video.id]);
|
||||||
|
logger.info(`Generated and saved video for video ${video.id} at ${absolutePath}`);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`Failed to generate video for video ${video.id}:`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
try {
|
||||||
|
const videosToProcess = await query(
|
||||||
|
"SELECT * FROM video WHERE video_prompt IS NOT NULL AND image_path IS NOT NULL AND (video_path IS NULL OR video_path = '')"
|
||||||
|
) as VideoRecord[];
|
||||||
|
|
||||||
|
if (videosToProcess.length === 0) {
|
||||||
|
logger.info('No videos to generate.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const queues: VideoRecord[][] = servers.map(() => []);
|
||||||
|
videosToProcess.forEach((video, index) => {
|
||||||
|
queues[index % servers.length].push(video);
|
||||||
|
});
|
||||||
|
|
||||||
|
const promises = servers.map((server, index) => {
|
||||||
|
if (!server.baseUrl || !server.outputDir) {
|
||||||
|
logger.warn(`Server ${index + 1} is not configured. Skipping.`);
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
return processServerQueue(server, queues[index]);
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all(promises);
|
||||||
|
logger.info('Finished generating all videos.');
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('An error occurred during video generation:', error);
|
||||||
|
} finally {
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
@ -55,7 +55,7 @@ async function generateVideo(
|
|||||||
|
|
||||||
const sourcePath = path.join(COMFY_OUTPUT_DIR!, latestFile);
|
const sourcePath = path.join(COMFY_OUTPUT_DIR!, latestFile);
|
||||||
await fs.copyFile(sourcePath, newFilePath);
|
await fs.copyFile(sourcePath, newFilePath);
|
||||||
await fs.unlink(sourcePath);
|
//await fs.unlink(sourcePath);
|
||||||
|
|
||||||
return newFilePath;
|
return newFilePath;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user