Ajout de la gestion des chemins d'images et amélioration des messages de log dans plusieurs modules
This commit is contained in:
@@ -5,6 +5,59 @@ const serverError = require('../../utils/serverError');
|
||||
const db = require('../../db');
|
||||
const storageManager = require('../data/storageManager');
|
||||
const PROJECTS_DIR = path.join('.');
|
||||
const measureManager = require('../measure/measureManager');
|
||||
|
||||
async function createVideoWithList(projectId, pathList) {
|
||||
//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);
|
||||
|
||||
// Trier les images numériquement
|
||||
const sortedImages = images.sort((a, b) => {
|
||||
const numA = parseInt(path.basename(a).match(/\d+/)[0], 10);
|
||||
const numB = parseInt(path.basename(b).match(/\d+/)[0], 10);
|
||||
return numA - numB;
|
||||
});
|
||||
|
||||
// En déduire l'id de la première et 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
|
||||
fs.writeFileSync(tempFile, sortedImages.map(image => `file '${image}'`).join('\n'));
|
||||
|
||||
const frameRate = 10;
|
||||
|
||||
// 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);
|
||||
execSync(ffmpegCommand);
|
||||
console.log('Video created successfully:', outputVideo);
|
||||
return outputVideo;
|
||||
} catch (error) {
|
||||
console.error('Error creating video:', error);
|
||||
serverError(error);
|
||||
} finally {
|
||||
// Supprimer le fichier temporaire
|
||||
if (fs.existsSync(tempFile)) {
|
||||
fs.unlinkSync(tempFile);
|
||||
console.log('Temporary file deleted:', tempFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function createVideo(projectId) {
|
||||
const tempFile = path.join('temp.txt');
|
||||
@@ -23,6 +76,13 @@ async function createVideo(projectId) {
|
||||
return numA - numB;
|
||||
});
|
||||
|
||||
// En déduire l'id de la première et 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
|
||||
fs.writeFileSync(tempFile, sortedImages.map(image => `file '${image}'`).join('\n'));
|
||||
|
||||
@@ -46,4 +106,4 @@ async function createVideo(projectId) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { createVideo };
|
||||
module.exports = { createVideo, createVideoWithList };
|
||||
|
||||
Reference in New Issue
Block a user