From 7baac5dcb7f634f30c0c9309a930b9243e193498 Mon Sep 17 00:00:00 2001 From: dakerboul Date: Mon, 10 Mar 2025 17:31:11 +0100 Subject: [PATCH] =?UTF-8?q?Modifier=20la=20fonction=20createVideoWithList?= =?UTF-8?q?=20pour=20utiliser=20spawn=20au=20lieu=20de=20execSync=20pour?= =?UTF-8?q?=20l'ex=C3=A9cution=20de=20ffmpeg=20en=20arri=C3=A8re-plan?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/video/videoManager.js | 50 +++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/video/videoManager.js b/src/video/videoManager.js index 4a75ba5..6f4b7ce 100644 --- a/src/video/videoManager.js +++ b/src/video/videoManager.js @@ -1,6 +1,6 @@ const fs = require('fs'); const path = require('path'); -const { execSync } = require('child_process'); +const { spawn } = require('child_process'); const serverError = require('../../utils/serverError'); const db = require('../../db'); @@ -28,13 +28,9 @@ async function deleteVideoProject(videoId) { } async function createVideoWithList(projectId, pathList, duration) { - //pathList étant la liste des chemins déjà triés const tempFile = path.join('temp.txt'); try { - // Trouver tous les fichiers image pour le projet donné const workdir = path.join(PROJECTS_DIR, 'storage', `${projectId}`); - const dir = path.join(PROJECTS_DIR, 'storage', `${projectId}`, 'images'); - console.log('dir:', dir); const images = pathList; console.log('images:', images); @@ -45,38 +41,50 @@ async function createVideoWithList(projectId, pathList, duration) { return numA - numB; }); - // En déduire l'id de la première et dernière image utilisée + // Déterminer l'id de la première et la dernière image utilisée const firstImageId = parseInt(path.basename(sortedImages[0]).match(/\d+/)[0], 10); const lastImageId = parseInt(path.basename(sortedImages[sortedImages.length - 1]).match(/\d+/)[0], 10); console.log('firstImageId:', firstImageId); console.log('lastImageId:', lastImageId); - // Créer un fichier temporaire pour la liste des images + // Créer le fichier temporaire pour la liste des images fs.writeFileSync(tempFile, sortedImages.map(image => `file '${image}'`).join('\n')); const frameRate = Math.ceil(sortedImages.length / parseInt(duration)); - - // le fichier final prend cette forme : {projectId}_{firstImageId}_{lastImageId}-{timestamp}.mp4 const timestamp = new Date().getTime(); const outputVideo = path.join(workdir, `${projectId}_${firstImageId}_${lastImageId}-${timestamp}.mp4`); - // Commande ffmpeg pour créer la vidéo - const ffmpegCommand = `ffmpeg -r ${frameRate} -f concat -safe 0 -i ${tempFile} -vsync vfr -pix_fmt yuv420p ${outputVideo}`; - console.log('Running ffmpeg command:', ffmpegCommand); - try { - execSync(ffmpegCommand, { stdio: 'ignore', detached: true }); - console.log('Video creation started in background:', outputVideo); - } catch (error) { - console.error('ffmpeg command failed:', error); - serverError.sendError(error); - } + // Préparer les arguments pour ffmpeg + const ffmpegArgs = [ + '-r', frameRate.toString(), + '-f', 'concat', + '-safe', '0', + '-i', tempFile, + '-vsync', 'vfr', + '-pix_fmt', 'yuv420p', + outputVideo + ]; + + console.log('Lancement de ffmpeg en arrière-plan avec les arguments:', ffmpegArgs); + + // Lancer ffmpeg en mode détaché + const child = spawn('ffmpeg', ffmpegArgs, { + detached: true, + stdio: 'ignore' + }); + + // Permettre au processus ffmpeg de continuer même si le parent se termine + child.unref(); + console.log('Video creation started in background:', outputVideo); + return outputVideo; } catch (error) { console.error('Error creating video:', error); serverError.sendError(error); } finally { - // Supprimer le fichier temporaire + // Attention : supprimer le fichier temporaire immédiatement pourrait poser problème + // si ffmpeg n'a pas encore fini de le lire. Envisagez un délai ou un mécanisme de nettoyage if (fs.existsSync(tempFile)) { fs.unlinkSync(tempFile); console.log('Temporary file deleted:', tempFile); @@ -117,7 +125,7 @@ async function createVideo(projectId) { // Commande ffmpeg pour créer la vidéo const ffmpegCommand = `ffmpeg -r ${frameRate} -f concat -safe 0 -i ${tempFile} -vsync vfr -pix_fmt yuv420p ${outputVideo}`; console.log('Running ffmpeg command:', ffmpegCommand); - execSync(ffmpegCommand); + spawn(ffmpegCommand); console.log('Video created successfully:', outputVideo); } catch (error) { console.error('Error creating video:', error);