BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

video-stream.js 15.5 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');
8
9const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');
10const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;
11const STREAM_READY_TIMEOUT_MS = 20 * 1000;
12const STREAM_IDLE_STOP_MS = 20 * 1000;
13const sessions = new Map();
14
15function hasInitialHlsAssets (outputDir)
16{
17 try
18 {
19 const entries = fs.readdirSync(outputDir, { withFileTypes: true });
20 return entries.some((entry) =>
21 {
22 if (!entry.isFile()) return false;
23 if (entry.name === 'master.m3u8') return true;
24 if (/^stream_\d+\.m3u8(?:\.tmp)?$/i.test(entry.name)) return true;
25 if (/^stream_\d+_segment_\d+\.ts(?:\.tmp)?$/i.test(entry.name)) return true;
26 return false;
27 });
28 } catch
29 {
30 return false;
31 }
32}
33
34function makeHttpError (status, message)
35{
36 const err = new Error(message);
37 err.status = status;
38 return err;
39}
40
41function ensureStreamRootDir ()
42{
43 fs.mkdirSync(STREAM_ROOT_DIR, { recursive: true });
44}
45
46function ffmpegPath ()
47{
48 return String(process.env.FFMPEG_PATH || '').trim() || 'ffmpeg';
49}
50
51function ffprobePath ()
52{
53 return String(process.env.FFPROBE_PATH || '').trim() || 'ffprobe';
54}
55
56function pruneExpiredSessions ()
57{
58 const now = Date.now();
59 for (const [sessionId, session] of sessions)
60 {
61 if (session.expiresAt > now) continue;
62 destroySession(sessionId);
63 }
64}
65
66function refreshSession (session)
67{
68 session.expiresAt = Date.now() + STREAM_SESSION_TTL_MS;
69}
70
71function destroySession (sessionId)
72{
73 const session = sessions.get(sessionId);
74 if (!session) return false;
75
76 sessions.delete(sessionId);
77
78 if (session.idleTimer)
79 {
80 clearTimeout(session.idleTimer);
81 session.idleTimer = null;
82 }
83
84 try
85 {
86 if (session.process) session.process.kill('SIGTERM');
87 } catch
88 {
89 // Ignore process cleanup failures.
90 }
91
92 fs.rmSync(session.outputDir, { recursive: true, force: true });
93 return true;
94}
95
96function touchSessionActivity (session)
97{
98 refreshSession(session);
99 session.lastActivityAt = Date.now();
100}
101
102function clearIdleStopTimer (session)
103{
104 if (!session.idleTimer) return;
105 clearTimeout(session.idleTimer);
106 session.idleTimer = null;
107}
108
109function scheduleIdleStop (session)
110{
111 clearIdleStopTimer(session);
112 if (session.activeRequests > 0) return;
113
114 session.idleTimer = setTimeout(() =>
115 {
116 const activeSession = sessions.get(session.id);
117 if (!activeSession) return;
118 if (activeSession.activeRequests > 0) return;
119 if (Date.now() - activeSession.lastActivityAt < STREAM_IDLE_STOP_MS)
120 {
121 scheduleIdleStop(activeSession);
122 return;
123 }
124 destroySession(activeSession.id);
125 }, STREAM_IDLE_STOP_MS);
126}
127
128function startAssetRequest (session)
129{
130 session.activeRequests += 1;
131 touchSessionActivity(session);
132 clearIdleStopTimer(session);
133}
134
135function finishAssetRequest (session)
136{
137 if (session.activeRequests > 0) session.activeRequests -= 1;
138 touchSessionActivity(session);
139 if (session.activeRequests === 0) scheduleIdleStop(session);
140}
141
142function runCommand (command, args)
143{
144 return new Promise((resolve, reject) =>
145 {
146 const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
147 let stdout = '';
148 let stderr = '';
149
150 child.stdout.on('data', (chunk) =>
151 {
152 stdout += chunk.toString();
153 });
154
155 child.stderr.on('data', (chunk) =>
156 {
157 stderr += chunk.toString();
158 });
159
160 child.on('error', (err) =>
161 {
162 if (err && err.code === 'ENOENT')
163 {
164 reject(makeHttpError(503, `${command} is not installed or not available in PATH.`));
165 return;
166 }
167 reject(err);
168 });
169
170 child.on('close', (code) =>
171 {
172 if (code === 0)
173 {
174 resolve({ stdout, stderr });
175 return;
176 }
177 reject(makeHttpError(500, stderr.trim() || `${command} exited with code ${code}.`));
178 });
179 });
180}
181
182async function probeInput (sourcePath)
183{
184 const { stdout } = await runCommand(ffprobePath(), [
185 '-v', 'error',
186 '-print_format', 'json',
187 '-show_streams',
188 '-show_format',
189 sourcePath,
190 ]);
191
192 let payload;
193 try
194 {
195 payload = JSON.parse(stdout || '{}');
196 } catch
197 {
198 throw makeHttpError(500, 'ffprobe returned invalid stream metadata.');
199 }
200
201 const typedIndexes = { video: 0, audio: 0, subtitle: 0 };
202 const streams = Array.isArray(payload.streams) ? payload.streams.map((stream) =>
203 {
204 const codecType = String(stream.codec_type || '').toLowerCase();
205 const relativeIndex = Object.prototype.hasOwnProperty.call(typedIndexes, codecType)
206 ? typedIndexes[codecType]++
207 : null;
208 return {
209 ...stream,
210 codecType,
211 relativeIndex,
212 tags: stream.tags || {},
213 disposition: stream.disposition || {},
214 };
215 }) : [];
216
217 return {
218 raw: payload,
219 durationSeconds: inputDurationSeconds(payload, streams),
220 video: streams.filter((stream) => stream.codecType === 'video'),
221 audio: streams.filter((stream) => stream.codecType === 'audio'),
222 subtitles: streams.filter((stream) => stream.codecType === 'subtitle'),
223 };
224}
225
226function parsePositiveNumber (value)
227{
228 const number = Number(value);
229 return Number.isFinite(number) && number > 0 ? number : null;
230}
231
232function parseDurationTagSeconds (value)
233{
234 const raw = String(value || '').trim();
235 if (!raw) return null;
236
237 const numeric = parsePositiveNumber(raw);
238 if (numeric) return numeric;
239
240 const match = raw.match(/^(\d+):([0-5]?\d):([0-5]?\d(?:\.\d+)?)$/);
241 if (!match) return null;
242
243 const hours = Number(match[1]);
244 const minutes = Number(match[2]);
245 const seconds = Number(match[3]);
246 if (!Number.isFinite(hours) || !Number.isFinite(minutes) || !Number.isFinite(seconds)) return null;
247 return hours * 3600 + minutes * 60 + seconds;
248}
249
250function inputDurationSeconds (payload, streams)
251{
252 const formatDuration = parsePositiveNumber(payload && payload.format && payload.format.duration);
253 if (formatDuration) return formatDuration;
254
255 for (const stream of streams)
256 {
257 const duration = parsePositiveNumber(stream.duration);
258 if (duration) return duration;
259 }
260
261 const formatTags = payload && payload.format && payload.format.tags;
262 const formatTagDuration = formatTags && parseDurationTagSeconds(formatTags.duration || formatTags.DURATION);
263 if (formatTagDuration) return formatTagDuration;
264
265 for (const stream of streams)
266 {
267 const tagDuration = parseDurationTagSeconds(stream.tags.duration || stream.tags.DURATION);
268 if (tagDuration) return tagDuration;
269 }
270
271 return null;
272}
273
274function sanitiseTrackValue (value, fallback)
275{
276 const cleaned = String(value || '').trim().replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '');
277 return cleaned || fallback;
278}
279
280function trackLanguage (stream)
281{
282 return sanitiseTrackValue(stream.tags.language || stream.tags.LANGUAGE || 'und', 'und').slice(0, 8);
283}
284
285function trackLabel (stream, fallback)
286{
287 const title = String(stream.tags.title || stream.tags.handler_name || '').trim();
288 if (title) return title;
289 const language = trackLanguage(stream);
290 if (language && language !== 'und') return `${fallback} (${language})`;
291 return fallback;
292}
293
294async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
295{
296 const extractedTracks = [];
297 let skippedCount = 0;
298 let trackNumber = 0;
299
300 for (const stream of subtitleStreams)
301 {
302 const fileName = `subtitle-${trackNumber}.vtt`;
303 const targetPath = path.join(outputDir, fileName);
304
305 try
306 {
307 await runCommand(ffmpegPath(), [
308 '-hide_banner',
309 '-loglevel', 'error',
310 '-nostdin',
311 '-y',
312 '-i', sourcePath,
313 '-map', `0:s:${stream.relativeIndex}`,
314 '-c:s', 'webvtt',
315 targetPath,
316 ]);
317 } catch
318 {
319 skippedCount += 1;
320 continue;
321 }
322
323 extractedTracks.push({
324 fileName,
325 label: trackLabel(stream, `Subtitle ${trackNumber + 1}`),
326 language: trackLanguage(stream),
327 isDefault: !!stream.disposition.default,
328 });
329 trackNumber += 1;
330 }
331
332 return {
333 tracks: extractedTracks,
334 skippedCount,
335 };
336}
337
338function buildHlsArgs (sourcePath, outputDir, streamInfo)
339{
340 const args = [
341 '-hide_banner',
342 '-loglevel', 'error',
343 '-nostdin',
344 '-y',
345 '-i', sourcePath,
346 '-map', '0:v:0',
347 ];
348
349 for (const audioStream of streamInfo.audio)
350 {
351 args.push('-map', `0:a:${audioStream.relativeIndex}`);
352 }
353
354 args.push(
355 '-c:v', 'libx264',
356 '-preset', 'veryfast',
357 '-crf', '23',
358 '-pix_fmt', 'yuv420p',
359 '-g', '48',
360 '-sc_threshold', '0',
361 );
362
363 if (streamInfo.audio.length)
364 {
365 args.push('-c:a', 'aac', '-b:a', '160k', '-ac', '2');
366 } else
367 {
368 args.push('-an');
369 }
370
371 const variantMap = [];
372 if (streamInfo.audio.length)
373 {
374 variantMap.push('v:0,agroup:audio,name:video');
375 streamInfo.audio.forEach((audioStream, index) =>
376 {
377 let entry = `a:${index},agroup:audio,name:${sanitiseTrackValue(trackLabel(audioStream, `Audio ${index + 1}`), `audio-${index + 1}`)},language:${trackLanguage(audioStream)}`;
378 if (audioStream.disposition.default) entry += ',default:yes';
379 variantMap.push(entry);
380 });
381 } else
382 {
383 variantMap.push('v:0,name:video');
384 }
385
386 args.push(
387 '-f', 'hls',
388 '-hls_time', '4',
389 '-hls_list_size', '0',
390 '-hls_flags', 'independent_segments+temp_file',
391 '-master_pl_name', 'master.m3u8',
392 '-hls_segment_filename', path.join(outputDir, 'stream_%v_segment_%06d.ts'),
393 '-var_stream_map', variantMap.join(' '),
394 path.join(outputDir, 'stream_%v.m3u8'),
395 );
396
397 return args;
398}
399
400function startHlsTranscode (session, streamInfo)
401{
402 const args = buildHlsArgs(session.sourcePath, session.outputDir, streamInfo);
403 const child = spawn(ffmpegPath(), args, { stdio: ['ignore', 'ignore', 'pipe'] });
404 let stderr = '';
405
406 child.stderr.on('data', (chunk) =>
407 {
408 stderr += chunk.toString();
409 if (stderr.length > 16000) stderr = stderr.slice(-16000);
410 });
411
412 child.on('error', (err) =>
413 {
414 if (err && err.code === 'ENOENT')
415 {
416 session.error = makeHttpError(503, 'ffmpeg is not installed or not available in PATH.');
417 return;
418 }
419 session.error = err;
420 });
421
422 child.on('close', (code) =>
423 {
424 session.process = null;
425 if (code === 0 || session.error) return;
426 session.error = makeHttpError(500, stderr.trim() || `ffmpeg exited with code ${code}.`);
427 });
428
429 session.process = child;
430}
431
432function waitForFile (targetPath, session, timeoutMs = STREAM_READY_TIMEOUT_MS)
433{
434 const startedAt = Date.now();
435
436 return new Promise((resolve, reject) =>
437 {
438 const check = () =>
439 {
440 if (fs.existsSync(targetPath))
441 {
442 resolve();
443 return;
444 }
445
446 if (session.error)
447 {
448 reject(session.error);
449 return;
450 }
451
452 if (Date.now() - startedAt >= timeoutMs)
453 {
454 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
455 return;
456 }
457
458 setTimeout(check, 120);
459 };
460
461 check();
462 });
463}
464
465function waitForStreamStart (session, timeoutMs = STREAM_READY_TIMEOUT_MS)
466{
467 const startedAt = Date.now();
468
469 return new Promise((resolve, reject) =>
470 {
471 const check = () =>
472 {
473 if (fs.existsSync(path.join(session.outputDir, 'master.m3u8')) || hasInitialHlsAssets(session.outputDir))
474 {
475 resolve();
476 return;
477 }
478
479 if (session.error)
480 {
481 reject(session.error);
482 return;
483 }
484
485 if (!session.process)
486 {
487 reject(makeHttpError(500, 'Video stream preparation stopped unexpectedly.'));
488 return;
489 }
490
491 if (Date.now() - startedAt >= timeoutMs)
492 {
493 reject(makeHttpError(504, 'Timed out while preparing the video stream.'));
494 return;
495 }
496
497 setTimeout(check, 120);
498 };
499
500 check();
501 });
502}
503
504async function initialiseSession (session)
505{
506 const streamInfo = await probeInput(session.sourcePath);
507 if (!streamInfo.video.length)
508 {
509 throw makeHttpError(400, 'File does not contain a video stream.');
510 }
511
512 const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);
513 session.subtitleTracks = subtitleResult.tracks;
514 session.subtitleSummary = {
515 total: streamInfo.subtitles.length,
516 converted: subtitleResult.tracks.length,
517 skipped: subtitleResult.skippedCount,
518 };
519 startHlsTranscode(session, streamInfo);
520 session.durationSeconds = streamInfo.durationSeconds;
521 await waitForStreamStart(session);
522}
523
524async function createVideoStreamSession (sourcePath)
525{
526 pruneExpiredSessions();
527 ensureStreamRootDir();
528
529 const sessionId = crypto.randomBytes(18).toString('hex');
530 const outputDir = path.join(STREAM_ROOT_DIR, sessionId);
531 fs.mkdirSync(outputDir, { recursive: true });
532
533 const session = {
534 id: sessionId,
535 sourcePath,
536 outputDir,
537 expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
538 process: null,
539 error: null,
540 subtitleTracks: [],
541 subtitleSummary: { total: 0, converted: 0, skipped: 0 },
542 readyPromise: null,
543 activeRequests: 0,
544 lastActivityAt: Date.now(),
545 idleTimer: null,
546 };
547
548 session.readyPromise = initialiseSession(session).catch((err) =>
549 {
550 session.error = err;
551 throw err;
552 });
553
554 sessions.set(sessionId, session);
555 await session.readyPromise;
556
557 return {
558 sessionId,
559 durationSeconds: session.durationSeconds,
560 subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),
561 subtitleSummary: {
562 total: Number(session.subtitleSummary.total || 0),
563 converted: Number(session.subtitleSummary.converted || 0),
564 skipped: Number(session.subtitleSummary.skipped || 0),
565 },
566 };
567}
568
569function contentTypeForAsset (assetPath)
570{
571 const ext = path.extname(assetPath).toLowerCase();
572 if (ext === '.m3u8') return 'application/vnd.apple.mpegurl';
573 if (ext === '.ts') return 'video/mp2t';
574 if (ext === '.vtt') return 'text/vtt; charset=utf-8';
575 if (ext === '.aac') return 'audio/aac';
576 if (ext === '.mp4' || ext === '.m4s') return 'video/mp4';
577 return 'application/octet-stream';
578}
579
580async function sendVideoStreamAsset (sessionId, assetPath, req, res)
581{
582 pruneExpiredSessions();
583 const session = sessions.get(sessionId);
584 if (!session) throw makeHttpError(404, 'Video stream session not found.');
585
586 touchSessionActivity(session);
587 await session.readyPromise;
588
589 const relativeAssetPath = String(assetPath || 'master.m3u8').replace(/^[/\\]+/, '');
590 const resolvedPath = path.resolve(session.outputDir, relativeAssetPath);
591 const resolvedRoot = path.resolve(session.outputDir);
592 if (resolvedPath !== resolvedRoot && !resolvedPath.startsWith(resolvedRoot + path.sep))
593 {
594 throw makeHttpError(404, 'Stream asset not found.');
595 }
596
597 await waitForFile(resolvedPath, session);
598 res.set('Cache-Control', 'private, no-store');
599 res.type(contentTypeForAsset(resolvedPath));
600
601 startAssetRequest(session);
602
603 let settled = false;
604 const finish = () =>
605 {
606 if (settled) return;
607 settled = true;
608 finishAssetRequest(session);
609 };
610
611 req.on('aborted', finish);
612 res.on('close', finish);
613 res.on('finish', finish);
614 res.on('error', finish);
615
616 await new Promise((resolve, reject) =>
617 {
618 res.sendFile(resolvedPath, (err) =>
619 {
620 finish();
621 if (!err)
622 {
623 resolve();
624 return;
625 }
626
627 if (err.code === 'ECONNABORTED' || err.message === 'Request aborted')
628 {
629 resolve();
630 return;
631 }
632
633 reject(err);
634 });
635 });
636}
637
638function releaseVideoStreamSession (sessionId)
639{
640 pruneExpiredSessions();
641 return destroySession(String(sessionId || ''));
642}
643
644module.exports = {
645 STREAM_ROOT_DIR,
646 STREAM_SESSION_TTL_MS,
647 STREAM_IDLE_STOP_MS,
648 createVideoStreamSession,
649 sendVideoStreamAsset,
650 releaseVideoStreamSession,
651};