Ajouter une route pour récupérer la progression de la création de vidéos et améliorer la gestion des erreurs dans la fonction createVideoWithList
This commit is contained in:
@@ -202,6 +202,46 @@ router.get('/videos/reset/:video_id', (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/videos/progress/:video_id', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const result = await db.query(`
|
||||||
|
SELECT
|
||||||
|
progress,
|
||||||
|
EXTRACT(EPOCH FROM (NOW() - started_at)) as elapsed,
|
||||||
|
eta,
|
||||||
|
status
|
||||||
|
FROM public.videos
|
||||||
|
WHERE id = $1
|
||||||
|
`, [req.params.video_id]);
|
||||||
|
|
||||||
|
if (result.rows.length === 0) {
|
||||||
|
return res.status(404).json({ error: 'Vidéo non trouvée' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const video = result.rows[0];
|
||||||
|
res.json({
|
||||||
|
progress: video.progress,
|
||||||
|
elapsed: video.elapsed,
|
||||||
|
eta: video.eta,
|
||||||
|
status: this.getStatusLabel(video.status)
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(500).json({ error: 'Erreur de récupération' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function getStatusLabel(status) {
|
||||||
|
const statusMap = {
|
||||||
|
0: 'En attente',
|
||||||
|
1: 'Terminé',
|
||||||
|
2: 'Échec',
|
||||||
|
3: 'En cours'
|
||||||
|
};
|
||||||
|
return statusMap[status] || 'Inconnu';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
router.get('/cat', (_, res) => {
|
router.get('/cat', (_, res) => {
|
||||||
const videoPath = dbTester.getCatVideo();
|
const videoPath = dbTester.getCatVideo();
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ const { execSync } = require('child_process');
|
|||||||
const { exec } = require('child_process');
|
const { exec } = require('child_process');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const execPromise = util.promisify(exec);
|
const execPromise = util.promisify(exec);
|
||||||
|
const { spawn } = require('child_process');
|
||||||
|
let globalProgress = {};
|
||||||
|
|
||||||
const serverError = require('../../utils/serverError');
|
const serverError = require('../../utils/serverError');
|
||||||
const db = require('../../db');
|
const db = require('../../db');
|
||||||
@@ -32,18 +34,17 @@ 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 tempFile = path.join('temp.txt');
|
||||||
let ffmpegSuccess = false;
|
let ffmpegProcess;
|
||||||
|
let cleanupDone = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const workdir = path.join(PROJECTS_DIR, 'storage', `${projectId}`);
|
// Configuration des chemins
|
||||||
const dir = path.join(PROJECTS_DIR, 'storage', `${projectId}`, 'images');
|
const workdir = path.join(PROJECTS_DIR, 'storage', projectId.toString());
|
||||||
|
|
||||||
// Vérification de l'existence du répertoire
|
|
||||||
if (!fs.existsSync(workdir)) {
|
if (!fs.existsSync(workdir)) {
|
||||||
fs.mkdirSync(workdir, { recursive: true });
|
fs.mkdirSync(workdir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Triage des images
|
// Tri des images
|
||||||
const sortedImages = pathList.sort((a, b) => {
|
const sortedImages = pathList.sort((a, b) => {
|
||||||
const numA = parseInt(path.basename(a).match(/\d+/)[0], 10);
|
const numA = parseInt(path.basename(a).match(/\d+/)[0], 10);
|
||||||
const numB = parseInt(path.basename(b).match(/\d+/)[0], 10);
|
const numB = parseInt(path.basename(b).match(/\d+/)[0], 10);
|
||||||
@@ -53,66 +54,140 @@ async function createVideoWithList(projectId, pathList, duration, videoId) {
|
|||||||
// Création du fichier temporaire
|
// Création du fichier temporaire
|
||||||
fs.writeFileSync(tempFile, sortedImages.map(image => `file '${image}'`).join('\n'));
|
fs.writeFileSync(tempFile, sortedImages.map(image => `file '${image}'`).join('\n'));
|
||||||
|
|
||||||
// Calcul du frame rate
|
// Calcul des paramètres vidéo
|
||||||
const frameRate = Math.ceil(sortedImages.length / parseInt(duration));
|
const totalFrames = sortedImages.length;
|
||||||
|
const frameRate = Math.ceil(totalFrames / parseInt(duration));
|
||||||
// Génération du nom de fichier
|
|
||||||
const timestamp = Date.now();
|
const timestamp = Date.now();
|
||||||
|
const firstImageId = path.basename(sortedImages[0]).match(/\d+/)[0];
|
||||||
|
const lastImageId = path.basename(sortedImages[sortedImages.length - 1]).match(/\d+/)[0];
|
||||||
const outputVideo = path.join(
|
const outputVideo = path.join(
|
||||||
workdir,
|
workdir,
|
||||||
`${projectId}_${path.basename(sortedImages[0], path.extname(sortedImages[0]))}_${path.basename(sortedImages[sortedImages.length - 1], path.extname(sortedImages[sortedImages.length - 1]))}-${timestamp}.mp4`
|
`${projectId}_${firstImageId}_${lastImageId}-${timestamp}.mp4`
|
||||||
);
|
);
|
||||||
|
|
||||||
// Commande FFmpeg
|
// Mise à jour initiale de la base de données
|
||||||
const ffmpegCommand = [
|
await db.query(`
|
||||||
'ffmpeg',
|
UPDATE public.videos
|
||||||
'-y', // Overwrite output file
|
SET
|
||||||
'-r', frameRate,
|
status = 3,
|
||||||
|
progress = 0,
|
||||||
|
started_at = NOW(),
|
||||||
|
updated_at = NOW(),
|
||||||
|
eta = NULL
|
||||||
|
WHERE id = $1
|
||||||
|
`, [videoId]);
|
||||||
|
|
||||||
|
// Configuration de FFmpeg
|
||||||
|
const ffmpegArgs = [
|
||||||
|
'-y',
|
||||||
|
'-r', frameRate.toString(),
|
||||||
'-f', 'concat',
|
'-f', 'concat',
|
||||||
'-safe', '0',
|
'-safe', '0',
|
||||||
'-i', tempFile,
|
'-i', tempFile,
|
||||||
'-vsync', 'vfr',
|
'-vsync', 'vfr',
|
||||||
'-pix_fmt', 'yuv420p',
|
'-pix_fmt', 'yuv420p',
|
||||||
outputVideo
|
outputVideo
|
||||||
].join(' ');
|
];
|
||||||
|
|
||||||
console.log(`Exécution de la commande FFmpeg: ${ffmpegCommand}`);
|
ffmpegProcess = spawn('ffmpeg', ffmpegArgs, {
|
||||||
const { stderr } = await execPromise(ffmpegCommand);
|
stdio: ['ignore', 'ignore', 'pipe']
|
||||||
|
});
|
||||||
|
|
||||||
// Vérification des erreurs FFmpeg
|
let lastUpdate = 0;
|
||||||
if (stderr.includes('Error') || stderr.includes('failed')) {
|
const startTime = Date.now();
|
||||||
throw new Error(`Erreur FFmpeg: ${stderr}`);
|
|
||||||
|
// Écoute de la sortie d'erreur pour la progression
|
||||||
|
ffmpegProcess.stderr.on('data', (data) => {
|
||||||
|
const output = data.toString();
|
||||||
|
const frameMatch = output.match(/frame=\s*(\d+)/);
|
||||||
|
|
||||||
|
if (frameMatch) {
|
||||||
|
const currentFrame = parseInt(frameMatch[1], 10);
|
||||||
|
const progress = Math.min((currentFrame / totalFrames) * 100, 99.99);
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
// Calcul de l'ETA
|
||||||
|
const elapsedSeconds = (now - startTime) / 1000;
|
||||||
|
const eta = elapsedSeconds / (currentFrame / totalFrames) - elapsedSeconds;
|
||||||
|
|
||||||
|
// Mise à jour max toutes les 500ms
|
||||||
|
if (now - lastUpdate > 500) {
|
||||||
|
db.query(`
|
||||||
|
UPDATE public.videos
|
||||||
|
SET
|
||||||
|
progress = $1,
|
||||||
|
eta = $2,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE id = $3
|
||||||
|
`, [progress, Math.round(eta), videoId]).catch(console.error);
|
||||||
|
|
||||||
|
console.log('Progress:', progress.toFixed(2), '%, ETA:', eta.toFixed(0), 's');
|
||||||
|
lastUpdate = now;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
ffmpegSuccess = true;
|
// Attente de la fin du processus
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
ffmpegProcess.on('close', async (code) => {
|
||||||
|
if (code === 0) {
|
||||||
|
try {
|
||||||
|
// Mise à jour finale
|
||||||
|
await db.query(`
|
||||||
|
UPDATE public.videos
|
||||||
|
SET
|
||||||
|
status = 1,
|
||||||
|
progress = 100,
|
||||||
|
eta = 0,
|
||||||
|
video_file = $1,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE id = $2
|
||||||
|
`, [outputVideo, videoId]);
|
||||||
|
resolve();
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reject(new Error(`FFmpeg process exited with code ${code}`));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Mise à jour de la base de données
|
ffmpegProcess.on('error', reject);
|
||||||
const updateStatusRes = await db.query(
|
});
|
||||||
'UPDATE public.videos SET status = $1, video_file = $2 WHERE id = $3 RETURNING *',
|
|
||||||
[1, outputVideo, videoId]
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log('Vidéo et statut mis à jour:', updateStatusRes.rows[0]);
|
|
||||||
return outputVideo;
|
return outputVideo;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erreur lors de la création vidéo:', error);
|
// Gestion des erreurs
|
||||||
|
console.error('Error in video creation:', error);
|
||||||
|
|
||||||
// Mise à jour du statut en erreur
|
try {
|
||||||
if (ffmpegSuccess) {
|
await db.query(`
|
||||||
await db.query(
|
UPDATE public.videos
|
||||||
'UPDATE public.videos SET status = $1 WHERE id = $2',
|
SET
|
||||||
[3, videoId] // 3 = statut erreur
|
status = 2,
|
||||||
);
|
progress = 0,
|
||||||
|
eta = 0,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE id = $1
|
||||||
|
`, [videoId]);
|
||||||
|
} catch (dbError) {
|
||||||
|
console.error('Database update error:', dbError);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw error;
|
throw error;
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
// Nettoyage du fichier temporaire
|
// Nettoyage
|
||||||
if (fs.existsSync(tempFile)) {
|
if (!cleanupDone) {
|
||||||
|
if (tempFile && fs.existsSync(tempFile)) {
|
||||||
fs.unlinkSync(tempFile);
|
fs.unlinkSync(tempFile);
|
||||||
}
|
}
|
||||||
|
if (ffmpegProcess) {
|
||||||
|
ffmpegProcess.kill();
|
||||||
|
}
|
||||||
|
cleanupDone = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user