101 lines
2.5 KiB
JavaScript
101 lines
2.5 KiB
JavaScript
// test-hls2.js
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const hlsManager = require('./helpers/hlsManager2');
|
|
|
|
const [, , inputArg, outputArg] = process.argv;
|
|
|
|
if (!inputArg || !outputArg) {
|
|
console.error(
|
|
'Uso: node test-hls.js "/percorso/video.mp4" "/percorso/output-hls"'
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
const inputPath = path.resolve(inputArg);
|
|
const outDir = path.resolve(outputArg);
|
|
|
|
if (!fs.existsSync(inputPath) || !fs.statSync(inputPath).isFile()) {
|
|
console.error(`File video non trovato: ${inputPath}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
if (inputPath === outDir || outDir.startsWith(inputPath + path.sep)) {
|
|
console.error('La directory di output non può coincidere con il file sorgente.');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('=== TEST HLS DIRETTO ===');
|
|
console.log('Sorgente:', inputPath);
|
|
console.log('Output:', outDir);
|
|
|
|
let jobId;
|
|
|
|
try {
|
|
jobId = hlsManager.startJob({
|
|
inputPath,
|
|
outDir,
|
|
segmentsNeeded: 3
|
|
});
|
|
} catch (err) {
|
|
console.error('Errore durante startJob:', err.stack || err.message);
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('Job ID:', jobId);
|
|
|
|
const startedAt = Date.now();
|
|
const timeoutMs = 15 * 60 * 1000;
|
|
let timer;
|
|
|
|
timer = setInterval(() => {
|
|
const job = hlsManager.getJob(jobId);
|
|
const elapsed = ((Date.now() - startedAt) / 1000).toFixed(1);
|
|
|
|
if (!job) {
|
|
console.error('Job non trovato nella mappa.');
|
|
clearInterval(timer);
|
|
process.exitCode = 1;
|
|
return;
|
|
}
|
|
|
|
const segments = fs.existsSync(outDir)
|
|
? fs.readdirSync(outDir).filter(
|
|
name => /^segment_\d+\.ts$/.test(name)
|
|
).length
|
|
: 0;
|
|
|
|
console.log(
|
|
`[${elapsed}s] status=${job.status}` +
|
|
` segments=${segments}` +
|
|
` ready=${job.ready}`
|
|
);
|
|
|
|
if (job.finishedAt) {
|
|
const playlist = path.join(outDir, 'master.m3u8');
|
|
const playlistExists = fs.existsSync(playlist);
|
|
|
|
console.log('\n=== RISULTATO ===');
|
|
console.log('Status:', job.status);
|
|
console.log('Exit code:', job.exitCode);
|
|
console.log('Segmenti:', segments);
|
|
console.log('Playlist presente:', playlistExists);
|
|
console.log('Output:', outDir);
|
|
|
|
if (playlistExists) {
|
|
console.log('\n--- master.m3u8 ---');
|
|
console.log(fs.readFileSync(playlist, 'utf8'));
|
|
}
|
|
|
|
clearInterval(timer);
|
|
process.exitCode =
|
|
job.exitCode === 0 && playlistExists && segments > 0 ? 0 : 1;
|
|
return;
|
|
}
|
|
|
|
if (Date.now() - startedAt > timeoutMs) {
|
|
console.error('Timeout: superati 15 minuti.');
|
|
clearInterval(timer);
|
|
process.exitCode = 124;
|
|
}
|
|
}, 1000);
|