save current changes

This commit is contained in:
2025-08-16 11:52:56 +02:00
parent 693198f679
commit 1367f4571a
3 changed files with 466 additions and 2 deletions

View File

@ -0,0 +1,47 @@
import * as fs from 'fs/promises';
import * as path from 'path';
import axios from 'axios';
import dotenv from 'dotenv';
dotenv.config();
const COMFY_BASE_URL = process.env.COMFY_BASE_URL?.replace(/\/$/, '');
const COMFY_OUTPUT_DIR = process.env.COMFY_OUTPUT_DIR;
async function generateVideo(prompt: string, imagePath: string, newFileName: string): Promise<string> {
const workflow = JSON.parse(await fs.readFile('src/comfyworkflows/generate_video.json', 'utf-8'));
workflow['6']['inputs']['text'] = prompt;
workflow['52']['inputs']['image'] = imagePath;
const response = await axios.post(`${COMFY_BASE_URL}/prompt`, { prompt: workflow });
const promptId = response.data.prompt_id;
let history;
do {
await new Promise(resolve => setTimeout(resolve, 1000));
const historyResponse = await axios.get(`${COMFY_BASE_URL}/history/${promptId}`);
history = historyResponse.data[promptId];
} while (!history || Object.keys(history.outputs).length === 0);
const files = await fs.readdir(COMFY_OUTPUT_DIR!);
const generatedFiles = files.filter(file => file.endsWith('.mp4'));
const fileStats = await Promise.all(
generatedFiles.map(async (file) => {
const stat = await fs.stat(path.join(COMFY_OUTPUT_DIR!, file));
return { file, mtime: stat.mtime };
})
);
fileStats.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
const latestFile = fileStats[0].file;
const newFilePath = path.resolve('./generated', newFileName);
await fs.mkdir('./generated', { recursive: true });
await fs.rename(path.join(COMFY_OUTPUT_DIR!, latestFile), newFilePath);
return newFilePath;
}
export { generateVideo };