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}: