BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

ae9cc981 History
video-stream.js 34.3 KiB Download Raw
1'use strict';
2
3const { spawn } = require('child_process');
4const crypto = require('crypto');
5const fs = require('fs');
6const os = require('os');
7const path = require('path');
8const { clearCachesForFile, pruneOldImageSubtitleCaches, purgeSubtitleCacheDir } = require('./image-subtitles');
9
10const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');
11const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;
12const STREAM_READY_TIMEOUT_MS = 20 * 1000;
13const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;
14const STREAM_EXPIRY_SWEEP_MS = parseSweepInterval(process.env.VIDEO_STREAM_EXPIRY_SWEEP_MS, 60 * 1000);
15const ESTIMATED_SEGMENT_BYTES = 1024 * 1024;
16const ESTIMATED_SEGMENT_DURATION_SECONDS = 6;
17const STREAM_PRUNE_DELETE_BATCH_SIZE = parsePruneDeleteBatchSize(process.env.VIDEO_STREAM_PRUNE_DELETE_BATCH_SIZE);
18const MAX_STREAM_SEGMENT_RETENTION_COUNT = 360;
19const STREAM_SEGMENT_RETENTION_COUNT = parseSegmentRetentionCount(process.env.VIDEO_STREAM_SEGMENT_RETENTION_COUNT);
20const IMAGE_BASED_SUBTITLE_CODECS = new Set([
21 'hdmv_pgs_subtitle',
22 'pgssub',
23 'dvd_subtitle',
24 'dvdsub',
25 'dvb_subtitle',
26 'xsub',
27]);
28const sessions = new Map();
29const leases = new Map();
30const reusableSessionIdBySource = new Map();
31
32// Remove any .sup subtitle files left over from a previous server run.
33purgeSubtitleCacheDir();
34
35function purgeStreamRootDir ()
36{
37 try
38 {
39 fs.rmSync(STREAM_ROOT_DIR, { recursive: true, force: true });
40 } catch
41 {
42 // Best-effort cleanup.
43 }
44}
45
46function cleanupIdleStreamArtifacts ()
47{
48 if (sessions.size > 0) return;
49 if (leases.size > 0) return;
50 purgeStreamRootDir();
51}
52
53// Remove stale stream output folders left by previous runs.
54purgeStreamRootDir();
55
56// Periodically sweep stream artifacts when no streams are active.
57const streamRootCleanupTimer = setInterval(cleanupIdleStreamArtifacts, STREAM_IDLE_STOP_MS);
58if (streamRootCleanupTimer && typeof streamRootCleanupTimer.unref === 'function')
59{
60 streamRootCleanupTimer.unref();
61}
62
63// Ensure expired leases/sessions are reclaimed even without further stream requests.
64const streamExpirySweepTimer = setInterval(pruneExpiredSessions, STREAM_EXPIRY_SWEEP_MS);
65if (streamExpirySweepTimer && typeof streamExpirySweepTimer.unref === 'function')
66{
67 streamExpirySweepTimer.unref();
68}
69
70function parseSweepInterval (value, fallbackMs)
71{
72 const parsed = Number.parseInt(String(value || ''), 10);
73 if (Number.isInteger(parsed) && parsed >= 1000)
74 {
75 return clampNumber(parsed, 1000, 10 * 60 * 1000);
76 }
77 return fallbackMs;
78}
79
80function parseSegmentRetentionCount (value)
81{
82 const parsed = Number.parseInt(String(value || ''), 10);
83 if (Number.isInteger(parsed) && parsed >= 30)
84 {
85 return clampNumber(parsed, 30, MAX_STREAM_SEGMENT_RETENTION_COUNT);
86 }
87 return defaultSegmentRetentionCount();
88}
89
90function parsePruneDeleteBatchSize (value)
91{
92 const parsed = Number.parseInt(String(value || ''), 10);
93 if (Number.isInteger(parsed) && parsed >= 1)
94 {
95 return clampNumber(parsed, 1, 128);
96 }
97 return 16;
98}
99
100function readRaspberryPiModel ()
101{
102 try
103 {
104 return fs.readFileSync('/proc/device-tree/model', 'utf8').replace(/\u0000/g, '').trim();
105 } catch
106 {
107 return '';
108 }
109}
110
111function clampNumber (value, min, max)
112{
113 return Math.min(max, Math.max(min, value));
114}
115
116function currentCpuPressureFactor (cpuCount)
117{
118 if (typeof os.loadavg !== 'function') return 1;
119 if (!Number.isFinite(cpuCount) || cpuCount <= 0) return 1;
120
121 const [load1] = os.loadavg();
122 if (!Number.isFinite(load1) || load1 < 0) return 1;
123
124 const saturation = load1 / cpuCount;
125 if (saturation >= 1.5) return 0.72;
126 if (saturation >= 1.2) return 0.8;
127 if (saturation >= 1.0) return 0.88;
128 if (saturation >= 0.8) return 0.94;
129 return 1;
130}
131
132function defaultSegmentRetentionCount ()
133{
134 const totalMemBytes = Number(os.totalmem()) || 0;
135 const freeMemBytes = Number(os.freemem()) || 0;
136 const cpuCount = Math.max(1, ((os.cpus() || []).length || 1));
137 const arch = String(os.arch() || '').toLowerCase();
138 const piModel = readRaspberryPiModel().toLowerCase();
139 const isPi5 = piModel.includes('raspberry pi 5');
140 const isArm = arch === 'arm' || arch === 'arm64' || arch.startsWith('armv');
141 const cpuPressure = currentCpuPressureFactor(cpuCount);
142
143 const freeMemRatio = totalMemBytes > 0 ? (freeMemBytes / totalMemBytes) : 1;
144 const memoryPressure = freeMemRatio < 0.12 ? 0.72 : freeMemRatio < 0.2 ? 0.84 : freeMemRatio < 0.3 ? 0.92 : 1;
145
146 if (isPi5)
147 {
148 const memoryBudgetBytes = Math.floor(totalMemBytes * 0.03);
149 const byMemory = Math.floor(memoryBudgetBytes / ESTIMATED_SEGMENT_BYTES);
150 const byCpu = 120 + ((cpuCount - 2) * 20);
151 const combined = Math.min(byMemory, byCpu);
152 return clampNumber(Math.floor(combined * cpuPressure * memoryPressure), 100, 220);
153 }
154
155 if (isArm)
156 {
157 const memoryBudgetBytes = Math.floor(totalMemBytes * 0.025);
158 const byMemory = Math.floor(memoryBudgetBytes / ESTIMATED_SEGMENT_BYTES);
159 const byCpu = 90 + ((cpuCount - 2) * 14);
160 const combined = Math.min(byMemory, byCpu);
161 return clampNumber(Math.floor(combined * cpuPressure * memoryPressure), 80, 180);
162 }
163
164 const memoryBudgetBytes = Math.floor(totalMemBytes * 0.05);
165 const byMemory = Math.floor(memoryBudgetBytes / ESTIMATED_SEGMENT_BYTES);
166 const byCpu = 140 + ((cpuCount - 4) * 16);
167 const combined = Math.min(byMemory, byCpu);
168 return clampNumber(Math.floor(combined * cpuPressure * memoryPressure), 120, 320);
169}
170
171function hasInitialHlsAssets (outputDir)
172{
173 try
174 {
175 const entries = fs.readdirSync(outputDir, { withFileTypes: true });
176 return entries.some((entry) =>
177 {
178 if (!entry.isFile()) return false;
179 if (entry.name === 'master.m3u8') return true;
180 if (/^stream_.+\.m3u8(?:\.tmp)?$/i.test(entry.name)) return true;
181 if (/^stream_.+_segment_\d+\.ts(?:\.tmp)?$/i.test(entry.name)) return true;
182 return false;
183 });
184 } catch
185 {
186 return false;
187 }
188}
189
190function makeHttpError (status, message)
191{
192 const err = new Error(message);
193 err.status = status;
194 return err;
195}
196
197function ensureStreamRootDir ()
198{
199 fs.mkdirSync(STREAM_ROOT_DIR, { recursive: true });
200}
201
202function ffmpegPath ()
203{
204 return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';
205}
206
207function ffprobePath ()
208{
209 return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';
210}
211
212function pruneExpiredSessions ()
213{
214 const now = Date.now();
215
216 for (const [leaseId, lease] of leases)
217 {
218 if (lease.expiresAt > now) continue;
219 releaseVideoStreamSession(leaseId);
220 }
221
222 for (const [sessionId, session] of sessions)
223 {
224 if (session.expiresAt > now) continue;
225 destroySession(sessionId);
226 }
227}
228
229function refreshSession (session)
230{
231 session.expiresAt = Date.now() + STREAM_SESSION_TTL_MS;
232}
233
234function refreshLease (lease)
235{
236 lease.expiresAt = Date.now() + STREAM_SESSION_TTL_MS;
237}
238
239function destroySession (sessionId)
240{
241 const session = sessions.get(sessionId);
242 if (!session) return false;
243
244 sessions.delete(sessionId);
245
246 if (reusableSessionIdBySource.get(session.sourcePath) === session.id)
247 {
248 reusableSessionIdBySource.delete(session.sourcePath);
249 }
250
251 if (session.leaseIds)
252 {
253 for (const leaseId of session.leaseIds)
254 {
255 leases.delete(leaseId);
256 }
257 session.leaseIds.clear();
258 }
259
260 if (session.idleTimer)
261 {
262 clearTimeout(session.idleTimer);
263 session.idleTimer = null;
264 }
265
266 try
267 {
268 if (session.process) session.process.kill('SIGTERM');
269 } catch
270 {
271 // Ignore process cleanup failures.
272 }
273
274 fs.rmSync(session.outputDir, { recursive: true, force: true });
275
276 // Only clear file-wide subtitle caches when no other active stream session
277 // for the same source file remains.
278 if (!hasActiveSessionForSourcePath(session.sourcePath))
279 {
280 clearCachesForFile(session.sourcePath);
281 }
282
283 return true;
284}
285
286function pruneImageSubtitleCachesForSession (session)
287{
288 if (!session || !session.sourcePath) return;
289
290 const slowestViewerTimeSeconds = slowestViewerTimeSecondsForSourcePath(session.sourcePath);
291 if (!Number.isFinite(slowestViewerTimeSeconds)) return;
292 pruneOldImageSubtitleCaches(session.sourcePath, slowestViewerTimeSeconds);
293}
294
295function hasActiveSessionForSourcePath (sourcePath)
296{
297 const wantedPath = String(sourcePath || '');
298 if (!wantedPath) return false;
299
300 for (const session of sessions.values())
301 {
302 if (session && session.sourcePath === wantedPath) return true;
303 }
304
305 return false;
306}
307
308function slowestViewerTimeSecondsForSourcePath (sourcePath)
309{
310 const wantedPath = String(sourcePath || '');
311 if (!wantedPath) return null;
312
313 let slowestIndex = null;
314 let hasActiveViewers = false;
315
316 for (const session of sessions.values())
317 {
318 if (!session || session.sourcePath !== wantedPath) continue;
319 if (!session.leaseIds || session.leaseIds.size === 0) continue;
320
321 hasActiveViewers = true;
322 const sessionSlowestIndex = slowestLeaseSegmentIndex(session);
323 if (!Number.isInteger(sessionSlowestIndex)) return null;
324 if (slowestIndex === null || sessionSlowestIndex < slowestIndex)
325 {
326 slowestIndex = sessionSlowestIndex;
327 }
328 }
329
330 if (!hasActiveViewers || slowestIndex === null) return null;
331 return slowestIndex * ESTIMATED_SEGMENT_DURATION_SECONDS;
332}
333
334function getSessionForLease (leaseId)
335{
336 const lease = leases.get(leaseId);
337 if (!lease) return null;
338
339 const session = sessions.get(lease.sessionId);
340 if (!session)
341 {
342 leases.delete(leaseId);
343 return null;
344 }
345
346 return { lease, session };
347}
348
349function createLeaseForSession (session)
350{
351 const leaseId = crypto.randomBytes(18).toString('hex');
352 const lease = {
353 id: leaseId,
354 sessionId: session.id,
355 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
356 lastActivityAt: Date.now(),
357 lastSegmentIndex: null,
358 };
359 leases.set(leaseId, lease);
360 session.leaseIds.add(leaseId);
361 return lease;
362}
363
364function touchSessionActivity (session, lease)
365{
366 refreshSession(session);
367 session.lastActivityAt = Date.now();
368
369 if (lease)
370 {
371 refreshLease(lease);
372 lease.lastActivityAt = Date.now();
373 }
374}
375
376function clearIdleStopTimer (session)
377{
378 if (!session.idleTimer) return;
379 clearTimeout(session.idleTimer);
380 session.idleTimer = null;
381}
382
383function scheduleIdleStop (session)
384{
385 clearIdleStopTimer(session);
386 if (session.activeRequests > 0 || (session.leaseIds && session.leaseIds.size > 0)) return;
387
388 session.idleTimer = setTimeout(() =>
389 {
390 const activeSession = sessions.get(session.id);
391 if (!activeSession) return;
392 if (activeSession.activeRequests > 0 || (activeSession.leaseIds && activeSession.leaseIds.size > 0)) return;
393 if (Date.now() - activeSession.lastActivityAt < STREAM_IDLE_STOP_MS)
394 {
395 scheduleIdleStop(activeSession);
396 return;
397 }
398 destroySession(activeSession.id);
399 }, STREAM_IDLE_STOP_MS);
400}
401
402function startAssetRequest (session, lease)
403{
404 session.activeRequests += 1;
405 touchSessionActivity(session, lease);
406 clearIdleStopTimer(session);
407}
408
409function finishAssetRequest (session, lease)
410{
411 if (session.activeRequests > 0) session.activeRequests -= 1;
412
413 const activeSession = sessions.get(session.id);
414 if (!activeSession) return;
415
416 touchSessionActivity(activeSession, lease);
417 if (activeSession.activeRequests === 0 && activeSession.leaseIds.size === 0)
418 {
419 destroySession(activeSession.id);
420 }
421}
422
423function parseSegmentIndexFromAssetPath (assetPath)
424{
425 const baseName = path.basename(String(assetPath || ''));
426 const match = baseName.match(/^stream_.+_segment_(\d+)\.ts$/i);
427 if (!match) return null;
428
429 const segmentIndex = Number.parseInt(match[1], 10);
430 return Number.isInteger(segmentIndex) ? segmentIndex : null;
431}
432
433function runSegmentPrune (session)
434{
435 if (session.segmentPruneRunning) return;
436 if (session.segmentPrunePendingCutoff <= session.segmentPrunedUntil) return;
437
438 const cutoff = session.segmentPrunePendingCutoff;
439 const pruneStartedAt = Date.now();
440 session.segmentPruneRunning = true;
441
442 fs.promises.readdir(session.outputDir)
443 .then((entries) =>
444 {
445 const removals = [];
446 for (const entryName of entries)
447 {
448 const match = String(entryName).match(/^stream_.+_segment_(\d+)\.ts$/i);
449 if (!match) continue;
450 const segmentIndex = Number.parseInt(match[1], 10);
451 if (!Number.isInteger(segmentIndex)) continue;
452 if (segmentIndex > cutoff) continue;
453
454 removals.push(path.join(session.outputDir, entryName));
455 }
456 return deleteFilesInBatches(removals, STREAM_PRUNE_DELETE_BATCH_SIZE);
457 })
458 .then((deletedCount) =>
459 {
460 const completedAt = Date.now();
461 session.segmentPrunedUntil = Math.max(session.segmentPrunedUntil, cutoff);
462 session.lastSegmentPrune = {
463 cutoff,
464 deletedSegments: deletedCount,
465 durationMs: Math.max(0, completedAt - pruneStartedAt),
466 completedAt,
467 };
468 pruneImageSubtitleCachesForSession(session);
469 if (session.segmentPrunedUntil >= 0 && reusableSessionIdBySource.get(session.sourcePath) === session.id)
470 {
471 reusableSessionIdBySource.delete(session.sourcePath);
472 }
473 })
474 .catch(() =>
475 {
476 // Best effort cleanup; keep streaming even if pruning fails.
477 })
478 .finally(() =>
479 {
480 session.segmentPruneRunning = false;
481 if (session.segmentPrunePendingCutoff > session.segmentPrunedUntil)
482 {
483 runSegmentPrune(session);
484 }
485 });
486}
487
488async function deleteFilesInBatches (pathsToDelete, batchSize)
489{
490 const paths = Array.isArray(pathsToDelete) ? pathsToDelete : [];
491 if (!paths.length) return 0;
492
493 let deletedCount = 0;
494 for (let index = 0; index < paths.length; index += batchSize)
495 {
496 const batch = paths.slice(index, index + batchSize);
497 await Promise.all(batch.map((targetPath) =>
498 fs.promises.unlink(targetPath)
499 .then(() =>
500 {
501 deletedCount += 1;
502 })
503 .catch(() =>
504 {
505 // Ignore races while ffmpeg rotates temp files and request handlers read segments.
506 })
507 ));
508 }
509
510 return deletedCount;
511}
512
513function slowestLeaseSegmentIndex (session)
514{
515 if (!session.leaseIds.size) return null;
516
517 let minIndex = null;
518 for (const leaseId of session.leaseIds)
519 {
520 const lease = leases.get(leaseId);
521 if (!lease) continue;
522 if (!Number.isInteger(lease.lastSegmentIndex)) return null;
523 if (minIndex === null || lease.lastSegmentIndex < minIndex) minIndex = lease.lastSegmentIndex;
524 }
525
526 return minIndex;
527}
528
529function scheduleSegmentPrune (session, lease, segmentIndex)
530{
531 if (!Number.isInteger(segmentIndex) || segmentIndex < 0) return;
532
533 lease.lastSegmentIndex = segmentIndex;
534 const slowestIndex = slowestLeaseSegmentIndex(session);
535 if (!Number.isInteger(slowestIndex)) return;
536
537 const pruneCutoff = slowestIndex - STREAM_SEGMENT_RETENTION_COUNT;
538 if (pruneCutoff <= session.segmentPrunedUntil) return;
539
540 session.segmentPrunePendingCutoff = Math.max(session.segmentPrunePendingCutoff, pruneCutoff);
541 runSegmentPrune(session);
542}
543
544function runCommand (command, args)
545{
546 return new Promise((resolve, reject) =>
547 {
548 const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
549 let stdout = '';
550 let stderr = '';
551
552 child.stdout.on('data', (chunk) =>
553 {
554 stdout += chunk.toString();
555 });
556
557 child.stderr.on('data', (chunk) =>
558 {
559 stderr += chunk.toString();
560 });
561
562 child.on('error', (err) =>
563 {
564 if (err && err.code === 'ENOENT')
565 {
566 reject(makeHttpError(503, `${command} is not installed or not available in PATH.`));
567 return;
568 }
569 reject(err);
570 });
571
572 child.on('close', (code) =>
573 {
574 if (code === 0)
575 {
576 resolve({ stdout, stderr });
577 return;
578 }
579 reject(makeHttpError(500, stderr.trim() || `${command} exited with code ${code}.`));
580 });
581 });
582}
583
584async function probeInput (sourcePath)
585{
586 const { stdout } = await runCommand(ffprobePath(), [
587 '-v', 'error',
588 '-print_format', 'json',
589 '-show_streams',
590 '-show_format',
591 sourcePath,
592 ]);
593
594 let payload;
595 try
596 {
597 payload = JSON.parse(stdout || '{}');
598 } catch
599 {
600 throw makeHttpError(500, 'ffprobe returned invalid stream metadata.');
601 }
602
603 const typedIndexes = { video: 0, audio: 0, subtitle: 0 };
604 const streams = Array.isArray(payload.streams) ? payload.streams.map((stream) =>
605 {
606 const codecType = String(stream.codec_type || '').toLowerCase();
607 const relativeIndex = Object.prototype.hasOwnProperty.call(typedIndexes, codecType)
608 ? typedIndexes[codecType]++
609 : null;
610 return {
611 ...stream,
612 codecType,
613 relativeIndex,
614 tags: stream.tags || {},
615 disposition: stream.disposition || {},
616 };
617 }) : [];
618
619 return {
620 raw: payload,
621 durationSeconds: inputDurationSeconds(payload, streams),
622 video: streams.filter((stream) => stream.codecType === 'video'),
623 audio: streams.filter((stream) => stream.codecType === 'audio'),
624 subtitles: streams.filter((stream) => stream.codecType === 'subtitle'),
625 };
626}
627
628function parsePositiveNumber (value)
629{
630 const number = Number(value);
631 return Number.isFinite(number) && number > 0 ? number : null;
632}
633
634function parseDurationTagSeconds (value)
635{
636 const raw = String(value || '').trim();
637 if (!raw) return null;
638
639 const numeric = parsePositiveNumber(raw);
640 if (numeric) return numeric;
641
642 const match = raw.match(/^(\d+):([0-5]?\d):([0-5]?\d(?:\.\d+)?)$/);
643 if (!match) return null;
644
645 const hours = Number(match[1]);
646 const minutes = Number(match[2]);
647 const seconds = Number(match[3]);
648 if (!Number.isFinite(hours) || !Number.isFinite(minutes) || !Number.isFinite(seconds)) return null;
649 return hours * 3600 + minutes * 60 + seconds;
650}
651
652function inputDurationSeconds (payload, streams)
653{
654 const formatDuration = parsePositiveNumber(payload && payload.format && payload.format.duration);
655 if (formatDuration) return formatDuration;
656
657 for (const stream of streams)
658 {
659 const duration = parsePositiveNumber(stream.duration);
660 if (duration) return duration;
661 }
662
663 const formatTags = payload && payload.format && payload.format.tags;
664 const formatTagDuration = formatTags && parseDurationTagSeconds(formatTags.duration || formatTags.DURATION);
665 if (formatTagDuration) return formatTagDuration;
666
667 for (const stream of streams)
668 {
669 const tagDuration = parseDurationTagSeconds(stream.tags.duration || stream.tags.DURATION);
670 if (tagDuration) return tagDuration;
671 }
672
673 return null;
674}
675
676function sanitiseTrackValue (value, fallback)
677{
678 const cleaned = String(value || '').trim().replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '');
679 return cleaned || fallback;
680}
681
682function trackLanguage (stream)
683{
684 return sanitiseTrackValue(stream.tags.language || stream.tags.LANGUAGE || 'und', 'und').slice(0, 8);
685}
686
687function trackLabel (stream, fallback)
688{
689 const title = String(stream.tags.title || stream.tags.handler_name || '').trim();
690 if (title) return title;
691 const language = trackLanguage(stream);
692 if (language && language !== 'und') return `${fallback} (${language})`;
693 return fallback;
694}
695
696function findMatchingSidecarSrtPath (sourcePath)
697{
698 const absoluteSourcePath = path.resolve(String(sourcePath || ''));
699 const dirPath = path.dirname(absoluteSourcePath);
700 const baseName = path.parse(absoluteSourcePath).name;
701 const exactPath = path.join(dirPath, `${baseName}.srt`);
702
703 try
704 {
705 const exactStat = fs.statSync(exactPath);
706 if (exactStat.isFile()) return exactPath;
707 } catch
708 {
709 // Fall back to case-insensitive directory scan.
710 }
711
712 const wantedName = `${baseName}.srt`.toLowerCase();
713
714 try
715 {
716 const entries = fs.readdirSync(dirPath, { withFileTypes: true });
717 for (const entry of entries)
718 {
719 if (!entry.isFile()) continue;
720 if (entry.name.toLowerCase() !== wantedName) continue;
721 return path.join(dirPath, entry.name);
722 }
723 } catch
724 {
725 return null;
726 }
727
728 return null;
729}
730
731async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
732{
733 const extractedTracks = [];
734 const imageBasedTracks = [];
735 let skippedCount = 0;
736 let trackNumber = 0;
737
738 for (const stream of subtitleStreams)
739 {
740 const codecName = String(stream.codec_name || '').toLowerCase();
741
742 // Image-based subtitle formats will be shown as DOM overlays via frame extraction.
743 if (IMAGE_BASED_SUBTITLE_CODECS.has(codecName))
744 {
745 imageBasedTracks.push({
746 label: trackLabel(stream, `Subtitle ${stream.relativeIndex + 1}`),
747 language: trackLanguage(stream),
748 isDefault: !!stream.disposition.default,
749 streamIndex: stream.relativeIndex,
750 });
751 continue;
752 }
753
754 const fileName = `subtitle-${trackNumber}.vtt`;
755 const targetPath = path.join(outputDir, fileName);
756
757 try
758 {
759 await runCommand(ffmpegPath(), [
760 '-hide_banner',
761 '-loglevel', 'error',
762 '-nostdin',
763 '-y',
764 '-i', sourcePath,
765 '-map', `0:s:${stream.relativeIndex}`,
766 '-c:s', 'webvtt',
767 targetPath,
768 ]);
769 } catch
770 {
771 skippedCount += 1;
772 continue;
773 }
774
775 extractedTracks.push({
776 fileName,
777 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
778 language: trackLanguage(stream),
779 isDefault: !!stream.disposition.default,
780 });
781 trackNumber += 1;
782 }
783
784 const sidecarSrtPath = findMatchingSidecarSrtPath(sourcePath);
785 const hasSidecarSrt = !!sidecarSrtPath;
786 if (sidecarSrtPath)
787 {
788 const fileName = `subtitle-${trackNumber}.vtt`;
789 const targetPath = path.join(outputDir, fileName);
790 try
791 {
792 await runCommand(ffmpegPath(), [
793 '-hide_banner',
794 '-loglevel', 'error',
795 '-nostdin',
796 '-y',
797 '-i', sidecarSrtPath,
798 '-c:s', 'webvtt',
799 targetPath,
800 ]);
801
802 extractedTracks.push({
803 fileName,
804 label: `Subtitle ${trackNumber + 1} (external)`,
805 language: 'und',
806 isDefault: extractedTracks.length === 0 && imageBasedTracks.length === 0,
807 });
808 trackNumber += 1;
809 } catch
810 {
811 skippedCount += 1;
812 }
813 }
814
815 return {
816 tracks: extractedTracks,
817 imageBasedTracks,
818 hasSidecarSrt,
819 skippedCount,
820 };
821}
822
823function buildHlsArgs (sourcePath, outputDir, streamInfo)
824{
825 const args = [
826 '-hide_banner',
827 '-loglevel', 'error',
828 '-nostdin',
829 '-y',
830 '-i', sourcePath,
831 '-map', '0:v:0',
832 ];
833
834 for (const audioStream of streamInfo.audio)
835 {
836 args.push('-map', `0:a:${audioStream.relativeIndex}`);
837 }
838
839 args.push(
840 '-c:v', 'libx264',
841 '-preset', 'veryfast',
842 '-crf', '23',
843 '-pix_fmt', 'yuv420p',
844 '-g', '48',
845 '-sc_threshold', '0',
846 );
847
848 if (streamInfo.audio.length)
849 {
850 args.push('-c:a', 'aac', '-b:a', '160k', '-ac', '2');
851 } else
852 {
853 args.push('-an');
854 }
855
856 const variantMap = [];
857 if (streamInfo.audio.length)
858 {
859 variantMap.push('v:0,agroup:audio,name:video');
860 streamInfo.audio.forEach((audioStream, index) =>
861 {
862 let entry = `a:${index},agroup:audio,name:${sanitiseTrackValue(trackLabel(audioStream, `Audio ${index + 1}`), `audio-${index + 1}`)},language:${trackLanguage(audioStream)}`;
863 if (audioStream.disposition.default) entry += ',default:yes';
864 variantMap.push(entry);
865 });
866 } else
867 {
868 variantMap.push('v:0,name:video');
869 }
870
871 args.push(
872 '-f', 'hls',
873 '-hls_time', '4',
874 '-hls_list_size', '0',
875 '-hls_flags', 'independent_segments+temp_file',
876 '-master_pl_name', 'master.m3u8',
877 '-hls_segment_filename', path.join(outputDir, 'stream_%v_segment_%06d.ts'),
878 '-var_stream_map', variantMap.join(' '),
879 path.join(outputDir, 'stream_%v.m3u8'),
880 );
881
882 return args;
883}
884
885function startHlsTranscode (session, streamInfo)
886{
887 const args = buildHlsArgs(session.sourcePath, session.outputDir, streamInfo);
888 const child = spawn(ffmpegPath(), args, { stdio: ['ignore', 'ignore', 'pipe'] });
889 let stderr = '';
890
891 child.stderr.on('data', (chunk) =>
892 {
893 stderr += chunk.toString();
894 if (stderr.length > 16000) stderr = stderr.slice(-16000);
895 });
896
897 child.on('error', (err) =>
898 {
899 if (err && err.code === 'ENOENT')
900 {
901 session.error = makeHttpError(503, 'ffmpeg is not installed or not available in PATH.');
902 return;
903 }
904 session.error = err;
905 });
906
907 child.on('close', (code) =>
908 {
909 session.process = null;
910 if (code === 0 || session.error) return;
911 session.error = makeHttpError(500, stderr.trim() || `ffmpeg exited with code ${code}.`);
912 });
913
914 session.process = child;
915}
916
917function waitForFile (targetPath, session, timeoutMs = STREAM_READY_TIMEOUT_MS)
918{
919 const startedAt = Date.now();
920
921 return new Promise((resolve, reject) =>
922 {
923 const check = () =>
924 {
925 if (fs.existsSync(targetPath))
926 {
927 resolve();
928 return;
929 }
930
931 if (session.error)
932 {
933 reject(session.error);
934 return;
935 }
936
937 if (Date.now() - startedAt >= timeoutMs)
938 {
939 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
940 return;
941 }
942
943 setTimeout(check, 120);
944 };
945
946 check();
947 });
948}
949
950function waitForStreamStart (session, timeoutMs = STREAM_READY_TIMEOUT_MS)
951{
952 const startedAt = Date.now();
953
954 return new Promise((resolve, reject) =>
955 {
956 const check = () =>
957 {
958 if (fs.existsSync(path.join(session.outputDir, 'master.m3u8')) || hasInitialHlsAssets(session.outputDir))
959 {
960 resolve();
961 return;
962 }
963
964 if (session.error)
965 {
966 reject(session.error);
967 return;
968 }
969
970 if (!session.process)
971 {
972 reject(makeHttpError(500, 'Video stream preparation stopped unexpectedly.'));
973 return;
974 }
975
976 if (Date.now() - startedAt >= timeoutMs)
977 {
978 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
979 return;
980 }
981
982 setTimeout(check, 120);
983 };
984
985 check();
986 });
987}
988
989async function initialiseSession (session)
990{
991 const streamInfo = await probeInput(session.sourcePath);
992 if (!streamInfo.video.length)
993 {
994 throw makeHttpError(400, 'File does not contain a video stream.');
995 }
996
997 const primaryVideoStream = streamInfo.video[0] || {};
998 session.videoWidth = parsePositiveNumber(primaryVideoStream.width);
999 session.videoHeight = parsePositiveNumber(primaryVideoStream.height);
1000
1001 const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);
1002 session.subtitleTracks = subtitleResult.tracks;
1003 session.imageSubtitleTracks = subtitleResult.imageBasedTracks;
1004 session.subtitleSummary = {
1005 total: streamInfo.subtitles.length + (subtitleResult.hasSidecarSrt ? 1 : 0),
1006 converted: subtitleResult.tracks.length,
1007 imageBased: subtitleResult.imageBasedTracks.length,
1008 skipped: subtitleResult.skippedCount,
1009 };
1010 startHlsTranscode(session, streamInfo);
1011 session.durationSeconds = streamInfo.durationSeconds;
1012 await waitForStreamStart(session);
1013}
1014
1015async function createVideoStreamSession (sourcePath)
1016{
1017 pruneExpiredSessions();
1018 ensureStreamRootDir();
1019
1020 const sourceKey = String(sourcePath || '');
1021 let session = null;
1022
1023 const reusableSessionId = reusableSessionIdBySource.get(sourceKey);
1024 if (reusableSessionId)
1025 {
1026 const reusable = sessions.get(reusableSessionId);
1027 if (reusable && !reusable.error && reusable.segmentPrunedUntil < 0)
1028 {
1029 session = reusable;
1030 } else
1031 {
1032 reusableSessionIdBySource.delete(sourceKey);
1033 }
1034 }
1035
1036 if (!session)
1037 {
1038 const sessionId = crypto.randomBytes(18).toString('hex');
1039 const outputDir = path.join(STREAM_ROOT_DIR, sessionId);
1040 fs.mkdirSync(outputDir, { recursive: true });
1041
1042 session = {
1043 id: sessionId,
1044 sourcePath: String(sourcePath || ''),
1045 sourceKey: sourceKey,
1046 outputDir,
1047 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
1048 process: null,
1049 error: null,
1050 subtitleTracks: [],
1051 imageSubtitleTracks: [],
1052 subtitleSummary: { total: 0, converted: 0, imageBased: 0, skipped: 0 },
1053 readyPromise: null,
1054 activeRequests: 0,
1055 lastActivityAt: Date.now(),
1056 idleTimer: null,
1057 segmentPruneRunning: false,
1058 segmentPrunePendingCutoff: -1,
1059 segmentPrunedUntil: -1,
1060 lastSegmentPrune: null,
1061 leaseIds: new Set(),
1062 };
1063
1064 session.readyPromise = initialiseSession(session).catch((err) =>
1065 {
1066 session.error = err;
1067 throw err;
1068 });
1069
1070 sessions.set(session.id, session);
1071 reusableSessionIdBySource.set(sourceKey, session.id);
1072 }
1073
1074 if (!session.leaseIds) session.leaseIds = new Set();
1075 const lease = createLeaseForSession(session);
1076 touchSessionActivity(session, lease);
1077
1078 try
1079 {
1080 await session.readyPromise;
1081 } catch (err)
1082 {
1083 releaseVideoStreamSession(lease.id);
1084 throw err;
1085 }
1086
1087 return {
1088 sessionId: lease.id,
1089 durationSeconds: session.durationSeconds,
1090 videoWidth: session.videoWidth || null,
1091 videoHeight: session.videoHeight || null,
1092 subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),
1093 imageSubtitleTracks: (session.imageSubtitleTracks || []).map((track) => ({ ...track })),
1094 subtitleSummary: {
1095 total: Number(session.subtitleSummary.total || 0),
1096 converted: Number(session.subtitleSummary.converted || 0),
1097 imageBased: Number(session.subtitleSummary.imageBased || 0),
1098 skipped: Number(session.subtitleSummary.skipped || 0),
1099 },
1100 };
1101}
1102
1103function contentTypeForAsset (assetPath)
1104{
1105 const ext = path.extname(assetPath).toLowerCase();
1106 if (ext === '.m3u8') return 'application/vnd.apple.mpegurl';
1107 if (ext === '.ts') return 'video/mp2t';
1108 if (ext === '.vtt') return 'text/vtt; charset=utf-8';
1109 if (ext === '.aac') return 'audio/aac';
1110 if (ext === '.mp4' || ext === '.m4s') return 'video/mp4';
1111 return 'application/octet-stream';
1112}
1113
1114async function sendVideoStreamAsset (sessionId, assetPath, req, res)
1115{
1116 pruneExpiredSessions();
1117 const active = getSessionForLease(sessionId);
1118 if (!active) throw makeHttpError(404, 'Video stream session not found.');
1119
1120 const { session, lease } = active;
1121
1122 touchSessionActivity(session, lease);
1123 await session.readyPromise;
1124
1125 const relativeAssetPath = String(assetPath || 'master.m3u8').replace(/^[/\\]+/, '');
1126 const resolvedPath = path.resolve(session.outputDir, relativeAssetPath);
1127 const resolvedRoot = path.resolve(session.outputDir);
1128 if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(resolvedRoot + path.sep))
1129 {
1130 throw makeHttpError(404, 'Stream asset not found.');
1131 }
1132
1133 const segmentIndex = parseSegmentIndexFromAssetPath(relativeAssetPath);
1134 if (segmentIndex !== null) scheduleSegmentPrune(session, lease, segmentIndex);
1135
1136 await waitForFile(resolvedPath, session);
1137 res.set('Cache-Control', 'private, no-store');
1138 res.type(contentTypeForAsset(resolvedPath));
1139
1140 startAssetRequest(session, lease);
1141
1142 let settled = false;
1143 const finish = () =>
1144 {
1145 if (settled) return;
1146 settled = true;
1147 finishAssetRequest(session, lease);
1148 };
1149
1150 req.on('aborted', finish);
1151 res.on('close', finish);
1152 res.on('finish', finish);
1153 res.on('error', finish);
1154
1155 await new Promise((resolve, reject) =>
1156 {
1157 res.sendFile(resolvedPath, (err) =>
1158 {
1159 finish();
1160 if (!err)
1161 {
1162 resolve();
1163 return;
1164 }
1165
1166 if (err.code === 'ECONNABORTED' || err.message === 'Request aborted')
1167 {
1168 resolve();
1169 return;
1170 }
1171
1172 reject(err);
1173 });
1174 });
1175}
1176
1177function releaseVideoStreamSession (sessionId)
1178{
1179 const leaseId = String(sessionId || '');
1180 const active = getSessionForLease(leaseId);
1181 if (!active)
1182 {
1183 leases.delete(leaseId);
1184 return false;
1185 }
1186
1187 const { session } = active;
1188 leases.delete(leaseId);
1189 session.leaseIds.delete(leaseId);
1190
1191 if (session.leaseIds.size === 0)
1192 {
1193 return destroySession(session.id);
1194 }
1195
1196 return true;
1197}
1198
1199function collectOutputDirStats (outputDir)
1200{
1201 const stats = {
1202 exists: false,
1203 totalBytes: 0,
1204 totalFiles: 0,
1205 segmentFiles: 0,
1206 segmentBytes: 0,
1207 playlistFiles: 0,
1208 subtitleFiles: 0,
1209 };
1210
1211 if (!outputDir || !fs.existsSync(outputDir)) return stats;
1212 stats.exists = true;
1213
1214 let entries = [];
1215 try
1216 {
1217 entries = fs.readdirSync(outputDir, { withFileTypes: true });
1218 } catch
1219 {
1220 return stats;
1221 }
1222
1223 for (const entry of entries)
1224 {
1225 if (!entry.isFile()) continue;
1226 const name = entry.name;
1227 const fullPath = path.join(outputDir, name);
1228 let size = 0;
1229 try
1230 {
1231 const fileStat = fs.statSync(fullPath);
1232 size = Number(fileStat.size) || 0;
1233 } catch
1234 {
1235 size = 0;
1236 }
1237
1238 stats.totalFiles += 1;
1239 stats.totalBytes += size;
1240
1241 if (/\.ts$/i.test(name))
1242 {
1243 stats.segmentFiles += 1;
1244 stats.segmentBytes += size;
1245 continue;
1246 }
1247
1248 if (/\.m3u8$/i.test(name))
1249 {
1250 stats.playlistFiles += 1;
1251 continue;
1252 }
1253
1254 if (/\.(sup|vtt)$/i.test(name))
1255 {
1256 stats.subtitleFiles += 1;
1257 }
1258 }
1259
1260 return stats;
1261}
1262
1263function getVideoStreamDebugSnapshot ()
1264{
1265 const activeStreams = [];
1266
1267 for (const [sessionId, session] of sessions.entries())
1268 {
1269 activeStreams.push({
1270 sessionId,
1271 sourcePath: session.sourcePath,
1272 outputDir: session.outputDir,
1273 viewers: session.leaseIds ? session.leaseIds.size : 0,
1274 activeRequests: Number(session.activeRequests || 0),
1275 expiresAt: Number(session.expiresAt || 0),
1276 segmentRetentionCount: STREAM_SEGMENT_RETENTION_COUNT,
1277 segmentPruneDeleteBatchSize: STREAM_PRUNE_DELETE_BATCH_SIZE,
1278 segmentPrunePendingCutoff: Number(session.segmentPrunePendingCutoff || 0),
1279 segmentPrunedUntil: Number(session.segmentPrunedUntil || 0),
1280 slowestViewerSegmentIndex: slowestLeaseSegmentIndex(session) ?? -1,
1281 lastSegmentPrune: session.lastSegmentPrune || null,
1282 output: collectOutputDirStats(session.outputDir),
1283 });
1284 }
1285
1286 const knownSessionDirs = new Set(activeStreams.map((stream) => path.resolve(stream.outputDir || '')));
1287 const orphanedOutputDirs = [];
1288 try
1289 {
1290 const entries = fs.readdirSync(STREAM_ROOT_DIR, { withFileTypes: true });
1291 for (const entry of entries)
1292 {
1293 if (!entry.isDirectory()) continue;
1294 const dirPath = path.resolve(path.join(STREAM_ROOT_DIR, entry.name));
1295 if (knownSessionDirs.has(dirPath)) continue;
1296 orphanedOutputDirs.push({
1297 outputDir: dirPath,
1298 output: collectOutputDirStats(dirPath),
1299 });
1300 }
1301 } catch
1302 {
1303 // Stream root may not exist yet.
1304 }
1305
1306 return {
1307 streamRootDir: STREAM_ROOT_DIR,
1308 streamSegmentRetentionCount: STREAM_SEGMENT_RETENTION_COUNT,
1309 streamSegmentRetentionMax: MAX_STREAM_SEGMENT_RETENTION_COUNT,
1310 streamPruneDeleteBatchSize: STREAM_PRUNE_DELETE_BATCH_SIZE,
1311 activeSessionCount: sessions.size,
1312 activeLeaseCount: leases.size,
1313 activeStreams,
1314 orphanedOutputDirs,
1315 };
1316}
1317
1318function getActiveStreamCount ()
1319{
1320 return sessions.size;
1321}
1322
1323module.exports = {
1324 STREAM_ROOT_DIR,
1325 STREAM_SESSION_TTL_MS,
1326 STREAM_IDLE_STOP_MS,
1327 STREAM_SEGMENT_RETENTION_COUNT,
1328 createVideoStreamSession,
1329 sendVideoStreamAsset,
1330 releaseVideoStreamSession,
1331 getActiveStreamCount,
1332 getVideoStreamDebugSnapshot,
1333 cleanupIdleStreamArtifacts,
1334};