Ajout de la fonction createVideo pour générer une vidéo à partir des images d'un projet, avec gestion des erreurs centralisée.
This commit is contained in:
@@ -7,6 +7,7 @@ const fs = require('fs');
|
|||||||
const ffmpeg = require('../ffmpeg');
|
const ffmpeg = require('../ffmpeg');
|
||||||
const fileUtils = require('../utils/fileUtils');
|
const fileUtils = require('../utils/fileUtils');
|
||||||
const serverError = require('../utils/serverError');
|
const serverError = require('../utils/serverError');
|
||||||
|
const video = require('../utils/video');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @swagger
|
* @swagger
|
||||||
@@ -50,29 +51,14 @@ router.get('/projects', async (req, res) => {
|
|||||||
*/
|
*/
|
||||||
router.get('/projects/:id/create-video', async (req, res) => {
|
router.get('/projects/:id/create-video', async (req, res) => {
|
||||||
const projectId = req.params.id;
|
const projectId = req.params.id;
|
||||||
const imageDir = `/storage/${projectId}`;
|
|
||||||
const outputVideo = `/storage/${projectId}/videos/output_video.mp4`;
|
|
||||||
const frameRate = 24;
|
|
||||||
const tempFile = `/storage/${projectId}/temp_file.txt`;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const images = fs.readdirSync(imageDir).filter(file => file.endsWith('.jpg'));
|
const videoPath = await video.createVideo(projectId);
|
||||||
if (images.length === 0) {
|
res.json(videoPath);
|
||||||
return res.status(404).json({ 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}`;
|
|
||||||
require('child_process').execSync(ffmpegCommand);
|
|
||||||
|
|
||||||
fs.unlinkSync(tempFile);
|
|
||||||
res.status(200).json({ message: 'Video created successfully', videoPath: outputVideo });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
serverError.sendError('Error creating video:', res, error);
|
serverError.sendError('Error creating video:', res, error);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @swagger
|
* @swagger
|
||||||
|
|||||||
31
utils/video.js
Normal file
31
utils/video.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
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/${projectId}/videos/output_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 };
|
||||||
Reference in New Issue
Block a user