Files
CrawlFlix-API/index.js
Joris Bertomeu de5a577190
All checks were successful
ci / Image build (push) Successful in 51s
ci / Deployment (push) Successful in 10s
Some fixes relative to job launch
2024-10-01 18:03:22 +02:00

301 lines
8.5 KiB
JavaScript

const express = require('express');
const Queue = require('bull');
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
const EventEmitter = require('events');
const softwareService = require('./services/softwares');
const BASE_PATH = process.env.DATA_PATH || `./data`;
const OUTPUT_PATH = `${BASE_PATH}/output`;
const TMP_PATH = `${BASE_PATH}/tmp`;
const app = express();
const port = 3000;
app.use(cors());
app.use(express.json());
const videoQueue = new Queue('crawlflix_queue', 'redis://192.168.1.222:32768');
videoQueue.on('error', (e) => {
console.log('An error occured', e);
});
const runCommand = (command) => {
console.log('⚙️ Will execute: ' + command);
return new Promise((resolve, reject) => {
const cmd = spawn(command, { shell: true });
let lastLog = '';
cmd.stdout.on('data', (data) => {
lastLog = data.toString();
process.stdout.write(data.toString());
});
cmd.stderr.on('data', (data) => {
lastLog = data.toString();
process.stderr.write(`stderr: ${data.toString()}`);
});
cmd.on('close', (code) => {
if (code === 0) {
resolve('Command executed successfully.');
} else {
reject(lastLog || 'Command failed with status code ' + code);
}
});
});
};
const runProgressCommand = (command) => {
console.log('⚙️ Will execute: ' + command);
const emitter = new EventEmitter();
const executeCommand = new Promise((resolve, reject) => {
const cmd = spawn(command, { shell: true });
let lastLog = '';
cmd.stdout.on('data', (data) => {
lastLog = data.toString();
const perc = extractPercentage(data.toString());
if (!perc) {
process.stdout.write(data.toString());
} else {
emitter.emit('percentage', perc);
}
});
cmd.stderr.on('data', (data) => {
lastLog = data.toString();
process.stderr.write(`stderr: ${data.toString()}`);
});
cmd.on('close', (code) => {
if (code === 0) {
resolve('Command executed successfully.');
} else {
reject(lastLog || 'Command failed with status code ' + code);
}
});
});
return { executeCommand, emitter };
};
// Route pour démarrer le processus
app.post('/start-process', async (req, res, next) => {
try {
const { mp4Filename, mpdUrl, keys, wantedResolution } = req.body;
console.log(req.body);
const job = await videoQueue.add({
mp4Filename,
mpdUrl,
keys,
wantedResolution
});
res.json({ jobId: job.id });
} catch(e) {
console.log(e);
next();
}
});
// Route pour vérifier le statut du job
app.get('/job-status/:jobId', async (req, res) => {
const job = await videoQueue.getJob(req.params.jobId);
if (job === null) {
res.status(404).json({ error: 'Job not found' });
} else {
const state = await job.getState();
const progress = job._progress;
res.json({ jobId: job.id, state, progress });
}
});
app.get('/jobs-status', async (req, res) => {
const jobs = await videoQueue.getJobs();
res.json(await Promise.all(jobs.splice(0, 25).map(async job => ({
id: job.id,
state: await job.getState(),
progress: job._progress,
addedOn: job.timestamp,
processedOn: job.processedOn,
finishedOn: job.finishedOn,
returnValue: job.returnvalue,
failedReason: job.failedReason,
data: job.data
}))));
});
app.get('/download/:filename', async (req, res) => {
const { filename } = req.params;
const file = path.join(process.cwd(), filename);
res.download(file);
});
app.get('/hello', async (req, res, next) => {
try {
res.json({
downloader: await softwareService.checkDownloaderUpdate(),
mp4decrypt: await softwareService.checkMp4decryptUpdate()
})
} catch(e) {
console.log(e);
next(e);
}
});
app.post('/processUpdate', async (req, res, next) => {
try {
console.log(req.body);
await softwareService.processUpdate(req.body);
res.end();
} catch(e) {
console.log(e);
next(e);
}
});
const checkFilesExistance = (pattern, path) => {
return new Promise(async (resolve, reject) => {
try {
const files = fs.readdirSync(path);
resolve(files.filter(file => file.includes(pattern)));
} catch(e) {
reject(e);
}
});
};
const extractPercentage = (line) => {
const s = line.split('%');
const percentages = [];
for (let i = 0; i < s.length; i++) {
if (s[i].length === 0) continue;
const ss = s[i].split(' ');
if (ss.length > 0 && !isNaN(ss[ss.length - 1]))
percentages.push(parseFloat(ss[ss.length - 1]));
}
if (percentages.length === 0) return null;
return Math.max(...percentages.filter((p) => p < 100));
}
// Processus de la file d'attente
videoQueue.process((job) => {
return new Promise(async (resolve, reject) => {
try {
const { mp4Filename, mpdUrl, keys, wantedResolution } = job.data;
const downloaderPath = softwareService.getLocalBinFileInfo('downloader').path;
const mp4decryptPath = softwareService.getLocalBinFileInfo('mp4decrypt').path;
const mp4TmpFilepath = path.join(TMP_PATH, `${mp4Filename}.mp4`);
const mp4FinalFilepath = path.join(OUTPUT_PATH, `${mp4Filename}.mp4`);
console.log('1')
const filesExist = await checkFilesExistance('encrypted', TMP_PATH);
console.log('2')
if (filesExist.length === 0) {
const resPattern = {
'4k': [3840, 2160],
'1080p': [1920, 1080],
'720p': [1280, 720],
'480p': [854, 480],
'360p': [640, 360],
'240p': [426, 240]
}[wantedResolution] || [1920, 1080];
console.log('Encrypted files not found, downloading...');
job.progress(10);
const { executeCommand, emitter } = runProgressCommand(`${downloaderPath} \"${mpdUrl}\" --save-name ${mp4TmpFilepath}_encrypted --select-video \"(?=.*${resPattern[0]})(?=.*${resPattern[1]})\" --select-audio lang=\"fr|en\":for=best2 --select-subtitle all`, true);
emitter.on('percentage', (percentage) => {
console.log(`Download Progression : ${percentage}%`);
job.progress(Math.round(10 + (percentage / 5)));
});
await executeCommand;
} else {
console.log('Encrypted files already exist, bypassing download...')
}
job.progress(30);
// Décryptage vidéo
await runCommand(`${mp4decryptPath} ${keys.map(k => `--key ${k.key}:${k.value}`).join(' ')} "${mp4TmpFilepath}_encrypted.mp4" "${mp4TmpFilepath}_decrypted.mp4"`);
job.progress(50);
// Décryptage audio
const audioFiles = fs.readdirSync(mp4TmpFilepath);
const finalAudio = [];
for (const file of audioFiles) {
if (file.startsWith(`${mp4TmpFilepath}_encrypted`) && file.endsWith('.m4a')) {
const baseName = path.basename(file, '.m4a');
await runCommand(`${mp4decryptPath} ${keys.map(k => `--key ${k.key}:${k.value}`).join(' ')} "${file}" "${baseName}_decrypted.m4a"`);
finalAudio.push(`${baseName}_decrypted.m4a`);
}
}
job.progress(70);
// Combinaison avec ffmpeg
let ffmpegCommand = `ffmpeg -y -i ${mp4TmpFilepath}_decrypted.mp4`;
let mapCommand = ' -map 0:v';
let inputIndex = 1;
for (const file of finalAudio) {
//if (file.startsWith(`${mp4Filename}_encrypted`) && file.endsWith('_decrypted.m4a')) {
ffmpegCommand += ` -i ${file}`;
mapCommand += ` -map ${inputIndex}:a`;
inputIndex++;
//}
}
ffmpegCommand += `${mapCommand} -c copy ${mp4FinalFilepath}.mp4`;
await runCommand(ffmpegCommand);
job.progress(90);
// Renommage des fichiers SRT
let counter = 1;
for (const file of audioFiles) {
if (file.startsWith(`${mp4TmpFilepath}_encrypted`) && file.endsWith('.srt')) {
fs.renameSync(file, `${mp4FinalFilepath}_${counter}.srt`);
counter++;
}
}
// Nettoyage (commenté pour correspondre au script original)
await runCommand(`rm ${mp4TmpFilepath}_encrypted* && rm ${mp4TmpFilepath}_decrypted*`);
job.progress(100);
resolve({ message: `File fetched and decrypted with success: ${mp4Filename}.mp4`, filePath: `${mp4FinalFilepath}.mp4`, fileName: `${mp4Filename}.mp4` });
} catch (error) {
console.log('Error while processing task', error)
reject(new Error(`${error.toString() || error}`));
}
});
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
const dirsToCheck = [{
path: BASE_PATH,
name: 'data'
}, {
path: OUTPUT_PATH,
name: 'output'
}, {
path: TMP_PATH,
name: 'tmp'
}];
for (let i = 0; i < dirsToCheck.length; i++) {
const dir = dirsToCheck[i];
if (!fs.existsSync(dir.path)) {
console.log(`Creating ${dir.name} directory...`);
fs.mkdirSync(dir.path);
}
}
softwareService.init();
});