BlueGit

timfa filemanager

Files

filemanager / server / video-stream.js

video-stream.js

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