Files

31 lines
1.1 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const serverError = require('../utils/serverError');
async function createVideo(projectId) {
const imageDir = `/storage/${projectId}`;
const outputVideo = `/storage/videos/output_${projectId}_video.mp4`;
const frameRate = 24;
const tempFile = `/storage/${projectId}/temp_file.txt`;
try {
const images = fs.readdirSync(imageDir).filter(file => file.endsWith('.jpg'));
if (images.length === 0) {
throw new Error('No images found for this project');
}
const tempFileContent = images.map(img => `file '${path.join(imageDir, img)}'`).join('\n');
fs.writeFileSync(tempFile, tempFileContent);
const ffmpegCommand = `ffmpeg -r ${frameRate} -f concat -safe 0 -i ${tempFile} -vsync vfr -pix_fmt yuv420p ${outputVideo}`;
execSync(ffmpegCommand);
fs.unlinkSync(tempFile);
return { message: 'Video created successfully', videoPath: outputVideo };
} catch (error) {
throw new Error(`Error creating video: ${error.message}`);
}
}
module.exports = { createVideo };