BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

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