videoManager.js: add videoManager.js

This commit is contained in:
2025-03-10 17:56:56 +01:00
parent f85cead1dd
commit e38718b1fa

View File

@@ -28,9 +28,9 @@ async function deleteVideoProject(videoId) {
} }
async function createVideoWithList(projectId, pathList, duration, videoId) { async function createVideoWithList(projectId, pathList, duration, videoId) {
const tempFile = path.join('temp.txt'); const workdir = path.join(PROJECTS_DIR, 'storage', `${projectId}`);
const tempFile = path.join(workdir, 'temp.txt');
try { try {
const workdir = path.join(PROJECTS_DIR, 'storage', `${projectId}`);
const images = pathList; const images = pathList;
console.log('images:', images); console.log('images:', images);
@@ -41,7 +41,7 @@ async function createVideoWithList(projectId, pathList, duration, videoId) {
return numA - numB; return numA - numB;
}); });
// Déterminer l'id de la première et la dernière image utilisée // Déterminer l'id de la première et de la dernière image utilisée
const firstImageId = parseInt(path.basename(sortedImages[0]).match(/\d+/)[0], 10); const firstImageId = parseInt(path.basename(sortedImages[0]).match(/\d+/)[0], 10);
const lastImageId = parseInt(path.basename(sortedImages[sortedImages.length - 1]).match(/\d+/)[0], 10); const lastImageId = parseInt(path.basename(sortedImages[sortedImages.length - 1]).match(/\d+/)[0], 10);
@@ -66,41 +66,51 @@ async function createVideoWithList(projectId, pathList, duration, videoId) {
outputVideo outputVideo
]; ];
console.log('Lancement de ffmpeg en arrière-plan avec les arguments:', ffmpegArgs); console.log('Lancement de ffmpeg avec les arguments:', ffmpegArgs);
// Lancer ffmpeg en mode détaché // Lancer ffmpeg sans détachement
const child = spawn('ffmpeg', ffmpegArgs, { const child = spawn('ffmpeg', ffmpegArgs, {
detached: true, cwd: workdir,
stdio: 'ignore' detached: false,
stdio: 'pipe'
}); });
// Permettre au processus ffmpeg de continuer même si le parent se termine // Afficher les logs de ffmpeg
child.unref(); child.stdout.on('data', (data) => {
console.log('Video creation started in background:', outputVideo); console.log(`ffmpeg stdout: ${data}`);
});
child.stderr.on('data', (data) => {
console.error(`ffmpeg stderr: ${data}`);
});
return outputVideo; child.on('close', (code) => {
} catch (error) { console.log(`ffmpeg process exited with code ${code}`);
console.error('Error creating video:', error); // Vous pouvez ici gérer la suppression du fichier temporaire
serverError.sendError(error); if (fs.existsSync(tempFile)) {
} finally { fs.unlinkSync(tempFile);
// Attention : supprimer le fichier temporaire immédiatement pourrait poser problème console.log('Temporary file deleted:', tempFile);
// 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);
}
//changer status de la vidéo à 1 console.log('Video creation started:', outputVideo);
// Mettre à jour le statut de la vidéo dans la base de données
const query = 'UPDATE public.videos SET status = 1 WHERE id = $1'; const query = 'UPDATE public.videos SET status = 1 WHERE id = $1';
db.query(query, [videoId], (err, results) => { db.query(query, [videoId], (err, results) => {
if (err) { if (err) {
return serverError.sendError('Error updating video status:', res, err); return serverError.sendError('Error updating video status:', res, err);
} }
console.log('Video status updated:', results.rows[0]); console.log('Video status updated:', results.rows ? results.rows[0] : 'Aucune ligne retournée');
}); });
return outputVideo;
} catch (error) {
console.error('Error creating video:', error);
serverError.sendError(error);
} }
} }
async function createVideo(projectId) { async function createVideo(projectId) {
const tempFile = path.join('temp.txt'); const tempFile = path.join('temp.txt');
try { try {