Alot of feats
This commit is contained in:
88
index.js
88
index.js
@@ -5,6 +5,8 @@ const fs = require('fs');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
const axios = require('axios');
|
||||||
|
var mpdParser = require('mpd-parser');
|
||||||
const softwareService = require('./services/softwares');
|
const softwareService = require('./services/softwares');
|
||||||
|
|
||||||
const BASE_PATH = process.env.DATA_PATH || `./data`;
|
const BASE_PATH = process.env.DATA_PATH || `./data`;
|
||||||
@@ -87,13 +89,15 @@ const runProgressCommand = (command) => {
|
|||||||
// Route pour démarrer le processus
|
// Route pour démarrer le processus
|
||||||
app.post('/start-process', async (req, res, next) => {
|
app.post('/start-process', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { mp4Filename, mpdUrl, keys, wantedResolution } = req.body;
|
const { mp4Filename, mpdUrl, keys, wantedResolution, wantedAudioTracks, wantedSubtitles } = req.body;
|
||||||
console.log(req.body);
|
console.log(req.body);
|
||||||
const job = await videoQueue.add({
|
const job = await videoQueue.add({
|
||||||
mp4Filename,
|
mp4Filename,
|
||||||
mpdUrl,
|
mpdUrl,
|
||||||
keys,
|
keys,
|
||||||
wantedResolution
|
wantedResolution,
|
||||||
|
wantedAudioTracks,
|
||||||
|
wantedSubtitles
|
||||||
});
|
});
|
||||||
res.json({ jobId: job.id });
|
res.json({ jobId: job.id });
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
@@ -183,30 +187,90 @@ const extractPercentage = (line) => {
|
|||||||
return Math.max(...percentages.filter((p) => p < 100));
|
return Math.max(...percentages.filter((p) => p < 100));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const parseMPDStream = async (mpdUrl) => {
|
||||||
|
const mpdResponse = await axios({
|
||||||
|
url: mpdUrl,
|
||||||
|
method: 'GET',
|
||||||
|
responseType: 'text'
|
||||||
|
});
|
||||||
|
const eventHandler = ({ type, message }) => console.log(`${type}: ${message}`);
|
||||||
|
|
||||||
|
const parsedManifest = mpdParser.parse(mpdResponse.data , { mpdUrl, eventHandler });
|
||||||
|
|
||||||
|
const obj = {
|
||||||
|
audioTracks: [],
|
||||||
|
videoTracks: [],
|
||||||
|
subtitles: []
|
||||||
|
};
|
||||||
|
for (const [key, value] of Object.entries(parsedManifest?.mediaGroups?.AUDIO?.audio)) {
|
||||||
|
for (let i = 0; i < value.playlists.length; i++) {
|
||||||
|
obj.audioTracks.push({
|
||||||
|
name: key,
|
||||||
|
language: value.language,
|
||||||
|
attributes: value.playlists[i].attributes
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const [key, value] of Object.entries(parsedManifest?.mediaGroups?.SUBTITLES?.subs)) {
|
||||||
|
for (let i = 0; i < value.playlists.length; i++) {
|
||||||
|
obj.subtitles.push({
|
||||||
|
name: key,
|
||||||
|
language: value.language,
|
||||||
|
attributes: value.playlists[i].attributes
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let i = 0; i < parsedManifest.playlists.length; i++) {
|
||||||
|
obj.videoTracks.push({
|
||||||
|
name: `${parsedManifest.playlists?.[i]?.attributes?.RESOLUTION?.width || 'N/C'}x${parsedManifest.playlists?.[i]?.attributes?.RESOLUTION?.height || 'N/C'}`,
|
||||||
|
codec: parsedManifest.playlists?.[i]?.attributes?.CODECS,
|
||||||
|
bandwidth: parsedManifest.playlists?.[i]?.attributes?.BANDWIDTH,
|
||||||
|
fps: parsedManifest.playlists?.[i]?.attributes?.['FRAME-RATE'],
|
||||||
|
resolution: {
|
||||||
|
width: parsedManifest.playlists?.[i]?.attributes?.RESOLUTION?.width,
|
||||||
|
height: parsedManifest.playlists?.[i]?.attributes?.RESOLUTION?.height
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
obj.videoTracks = obj.videoTracks.sort((a, b) => b.resolution.width - a.resolution.width);
|
||||||
|
return obj;
|
||||||
|
};
|
||||||
|
|
||||||
|
app.post('/processMPD', async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
res.json(await parseMPDStream(req.body.mpdUrl));
|
||||||
|
} catch(e) {
|
||||||
|
console.log(e);
|
||||||
|
next(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Processus de la file d'attente
|
// Processus de la file d'attente
|
||||||
videoQueue.process((job) => {
|
videoQueue.process((job) => {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const { mp4Filename, mpdUrl, keys, wantedResolution } = job.data;
|
console.log('Will launch job')
|
||||||
|
const { mp4Filename, mpdUrl, keys, wantedResolution, wantedAudioTracks, wantedSubtitles } = job.data;
|
||||||
const downloaderPath = softwareService.getLocalBinFileInfo('downloader').path;
|
const downloaderPath = softwareService.getLocalBinFileInfo('downloader').path;
|
||||||
const mp4decryptPath = softwareService.getLocalBinFileInfo('mp4decrypt').path;
|
const mp4decryptPath = softwareService.getLocalBinFileInfo('mp4decrypt').path;
|
||||||
const mp4TmpFilepath = path.join(TMP_PATH, `${mp4Filename}.mp4`);
|
const mp4TmpFilepath = path.join(TMP_PATH, `${mp4Filename}.mp4`);
|
||||||
const mp4FinalFilepath = path.join(OUTPUT_PATH, `${mp4Filename}.mp4`);
|
const mp4FinalFilepath = path.join(OUTPUT_PATH, `${mp4Filename}.mp4`);
|
||||||
console.log('1')
|
console.log('1')
|
||||||
|
//await parseMPDStream(mpdUrl);
|
||||||
const filesExist = await checkFilesExistance('encrypted', TMP_PATH);
|
const filesExist = await checkFilesExistance('encrypted', TMP_PATH);
|
||||||
console.log('2')
|
console.log('2')
|
||||||
if (filesExist.length === 0) {
|
if (filesExist.length === 0) {
|
||||||
const resPattern = {
|
// const resPattern = {
|
||||||
'4k': [3840, 2160],
|
// '4k': [3840, 2160],
|
||||||
'1080p': [1920, 1080],
|
// '1080p': [1920, 1080],
|
||||||
'720p': [1280, 720],
|
// '720p': [1280, 720],
|
||||||
'480p': [854, 480],
|
// '480p': [854, 480],
|
||||||
'360p': [640, 360],
|
// '360p': [640, 360],
|
||||||
'240p': [426, 240]
|
// '240p': [426, 240]
|
||||||
}[wantedResolution] || [1920, 1080];
|
// }[wantedResolution] || [1920, 1080];
|
||||||
console.log('Encrypted files not found, downloading...');
|
console.log('Encrypted files not found, downloading...');
|
||||||
job.progress(10);
|
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);
|
const { executeCommand, emitter } = runProgressCommand(`${downloaderPath} \"${mpdUrl}\" --save-dir ${TMP_PATH} --save-name ${mp4Filename}.mp4_encrypted --select-video res=\"${wantedResolution.resolution.width}*\" --select-audio lang=\"${wantedAudioTracks.map(elem => elem.language).join('|')}\":for=best2 --select-subtitle all`, true);
|
||||||
emitter.on('percentage', (percentage) => {
|
emitter.on('percentage', (percentage) => {
|
||||||
console.log(`Download Progression : ${percentage}%`);
|
console.log(`Download Progression : ${percentage}%`);
|
||||||
job.progress(Math.round(10 + (percentage / 5)));
|
job.progress(Math.round(10 + (percentage / 5)));
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.21.0",
|
"express": "^4.21.0",
|
||||||
"fs": "^0.0.1-security",
|
"fs": "^0.0.1-security",
|
||||||
|
"mpd-parser": "^1.3.0",
|
||||||
"path": "^0.12.7",
|
"path": "^0.12.7",
|
||||||
"tar": "^7.4.3",
|
"tar": "^7.4.3",
|
||||||
"unzipper": "^0.12.3"
|
"unzipper": "^0.12.3"
|
||||||
|
|||||||
Reference in New Issue
Block a user