Migration de la fonction de création de vidéo vers un nouveau module et suppression de l'ancienne implémentation
This commit is contained in:
2
db.js
2
db.js
@@ -1,6 +1,6 @@
|
|||||||
const { Client } = require('pg');
|
const { Client } = require('pg');
|
||||||
|
|
||||||
const local = false;
|
const local = true;
|
||||||
// Connexion à la base de données PostgreSQL
|
// Connexion à la base de données PostgreSQL
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
host: local ? 'mikoshi' : '172.30.0.2',
|
host: local ? 'mikoshi' : '172.30.0.2',
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ const express = require('express');
|
|||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const db = require('../db');
|
const db = require('../db');
|
||||||
const serverError = require('../utils/serverError');
|
const serverError = require('../utils/serverError');
|
||||||
|
const videoManager = require('../src/video/videoManager');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @swagger
|
* @swagger
|
||||||
|
|||||||
46
src/video/videoManager.js
Normal file
46
src/video/videoManager.js
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const { execSync } = require('child_process');
|
||||||
|
const serverError = require('../../utils/serverError');
|
||||||
|
const db = require('../../db');
|
||||||
|
const storageManager = require('../data/storageManager');
|
||||||
|
const PROJECTS_DIR = path.join('.');
|
||||||
|
|
||||||
|
async function createVideo(projectId) {
|
||||||
|
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 = storageManager.scanAllImages(dir);
|
||||||
|
console.log('images:', images);
|
||||||
|
|
||||||
|
// Créer un fichier temporaire pour la liste des images
|
||||||
|
const tempFile = path.join('temp.txt');
|
||||||
|
fs.writeFileSync(tempFile, images.map(image => `file '${image}'`).join('\n'));
|
||||||
|
|
||||||
|
const frameRate = 10;
|
||||||
|
const outputVideo = path.join(workdir, 'video.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);
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 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}`;
|
||||||
|
//execSync(ffmpegCommand);
|
||||||
|
|
||||||
|
module.exports = { createVideo };
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
const storageManager = require('../src/data/storageManager');
|
const storageManager = require('../src/data/storageManager');
|
||||||
|
const videoManager = require('../src/video/videoManager');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
console.log('Testing database functions...');
|
console.log('Testing database functions...');
|
||||||
@@ -16,4 +17,11 @@ function getSmileImage() {
|
|||||||
return path.join(__dirname, '../sample/smile.png');
|
return path.join(__dirname, '../sample/smile.png');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//test de lancement d'une création de vidéo sur le projet 1
|
||||||
|
videoManager.createVideo(1).then(res => {
|
||||||
|
console.log('3 - Video created:', res);
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('Error creating video:', err);
|
||||||
|
});
|
||||||
|
|
||||||
exports.getSmileImage = getSmileImage;
|
exports.getSmileImage = getSmileImage;
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
const fs = require('fs');
|
|
||||||
const path = require('path');
|
|
||||||
const { execSync } = require('child_process');
|
|
||||||
const serverError = require('../utils/serverError');
|
|
||||||
|
|
||||||
async function createVideo(projectId) {
|
|
||||||
const imageDir = `/storage/${projectId}`;
|
|
||||||
const outputVideo = `/storage/videos/output_${projectId}_video.mp4`;
|
|
||||||
const frameRate = 24;
|
|
||||||
const tempFile = `/storage/${projectId}/temp_file.txt`;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const images = fs.readdirSync(imageDir).filter(file => file.endsWith('.jpg'));
|
|
||||||
if (images.length === 0) {
|
|
||||||
throw new 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}`;
|
|
||||||
execSync(ffmpegCommand);
|
|
||||||
|
|
||||||
fs.unlinkSync(tempFile);
|
|
||||||
return { message: 'Video created successfully', videoPath: outputVideo };
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error(`Error creating video: ${error.message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = { createVideo };
|
|
||||||
Reference in New Issue
Block a user