From 46dbcfd5f1f8d51b97b2d80a40649425f81c37b1 Mon Sep 17 00:00:00 2001 From: Kerboul Date: Tue, 14 Jan 2025 15:28:15 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20fonctionnalit=C3=A9=20de=20cr?= =?UTF-8?q?=C3=A9ation=20de=20vid=C3=A9o=20=C3=A0=20partir=20des=20images?= =?UTF-8?q?=20d'un=20projet=20sp=C3=A9cifique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/api.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/routes/api.js b/routes/api.js index 230e881..0e5ebdc 100644 --- a/routes/api.js +++ b/routes/api.js @@ -22,6 +22,53 @@ const file_watcher = require('../file_watcher'); // Assurez-vous d'importer le f */ router.get('/projects', projectController.getAllProjects); +/** + * @swagger + * /projects/{id}/create-video: + * get: + * description: Create a video from images of a specific project by project id + * parameters: + * - in: path + * name: id + * required: true + * description: Numeric ID of the project to create video for. + * schema: + * type: integer + * responses: + * 200: + * description: Video created successfully + * 404: + * description: No images found for this project + * 500: + * description: Internal server error + */ +router.get('/projects/:id/create-video', async (req, res) => { + const projectId = req.params.id; + const imageDir = path.join(__dirname, `../storage/${projectId}`); + const outputVideo = path.join(__dirname, `../storage/${projectId}/output_video.mp4`); + const frameRate = 24; + const tempFile = path.join(__dirname, `../storage/${projectId}/temp_file.txt`); + + try { + const images = fs.readdirSync(imageDir).filter(file => file.endsWith('.jpg')); + if (images.length === 0) { + 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) { + console.error('Error creating video:', error); + res.status(500).json({ error: 'Internal server error' }); + } +}); + /** * @swagger * /projects/{id}: