test
@@ -2942,9 +2942,9 @@ async function renderStreamedVideoPreview (item, targetEl)
2942
2942
const prefs = loadUserVideoPrefs();
2943
2943
applyUserVideoTextTrackPrefs();
2944
2944
2945
if ((session.subtitleSummary && session.subtitleSummary.total > 0) && !(session.subtitleTracks || []).length)
2945
if ((session.subtitleSummary && session.subtitleSummary.skipped > 0))
2946
2946
{
2947
toast('This video has subtitle tracks, but none are compatible with web playback.', 'err');
2947
toast('Some subtitle tracks could not be converted for web playback.', 'err');
2948
2948
}
2949
2949
2950
2950
STATE.previewDownloadAction = () => preparePreviewDownload(item);
@@ -2983,6 +2983,122 @@ async function renderStreamedVideoPreview (item, targetEl)
2983
2983
2984
2984
ensureVideoPlaybackUi(player, session.durationSeconds);
2985
2985
2986
// -------------------------------------------------------------------------
2987
// Image-based subtitle overlays (PGS, DVD, etc.)
2988
// Extracted and displayed as PNG frames at the bottom of the video.
2989
// -------------------------------------------------------------------------
2990
const imageSubtitleTracks = session.imageSubtitleTracks || [];
2991
let activeImageStreamIndex = null;
2992
let imageOverlayContainer = null;
2993
let imageOverlayImg = null;
2994
let frameExtractionTimer = null;
2995
2996
const createImageOverlay = () =>
2997
{
2998
if (imageOverlayContainer) return;
2999
3000
imageOverlayContainer = document.createElement('div');
3001
imageOverlayContainer.className = 'video-subtitle-overlay';
3002
imageOverlayContainer.style.position = 'absolute';
3003
imageOverlayContainer.style.bottom = '0';
3004
imageOverlayContainer.style.left = '0';
3005
imageOverlayContainer.style.right = '0';
3006
imageOverlayContainer.style.display = 'none';
3007
imageOverlayContainer.style.textAlign = 'center';
3008
imageOverlayContainer.style.zIndex = '1';
3009
imageOverlayContainer.style.padding = '4px';
3010
imageOverlayContainer.style.backgroundColor = 'rgba(0, 0, 0, 0.3)';
3011
imageOverlayContainer.style.maxHeight = '30%';
3012
imageOverlayContainer.style.overflow = 'hidden';
3013
3014
imageOverlayImg = document.createElement('img');
3015
imageOverlayImg.style.maxHeight = '100%';
3016
imageOverlayImg.style.maxWidth = '100%';
3017
imageOverlayImg.style.display = 'none';
3018
imageOverlayImg.addEventListener('error', () => imageOverlayImg.style.display = 'none');
3019
3020
imageOverlayContainer.appendChild(imageOverlayImg);
3021
3022
const videoContainer = video.parentElement;
3023
videoContainer.style.position = 'relative';
3024
videoContainer.appendChild(imageOverlayContainer);
3025
};
3026
3027
const updateImageSubtitleFrame = async () =>
3028
{
3029
if (activeImageStreamIndex === null || !imageOverlayImg) return;
3030
3031
try
3032
{
3033
const currentTime = player.currentTime();
3034
const url = `/api/files/video-stream-subtitle-frame?filePath=${encodeURIComponent(item.path)}&streamIndex=${activeImageStreamIndex}&timeSeconds=${currentTime}`;
3035
3036
// Set a new src to trigger the load; use cache busting to ensure fresh frames
3037
imageOverlayImg.src = url + '&t=' + Date.now();
3038
imageOverlayImg.style.display = 'block';
3039
imageOverlayContainer.style.display = 'block';
3040
} catch
3041
{
3042
imageOverlayImg.style.display = 'none';
3043
}
3044
};
3045
3046
const scheduleFrameExtraction = () =>
3047
{
3048
if (frameExtractionTimer)
3049
{
3050
clearInterval(frameExtractionTimer);
3051
}
3052
3053
// Update frame every 100ms while playing
3054
if (!player.paused())
3055
{
3056
void updateImageSubtitleFrame();
3057
frameExtractionTimer = setInterval(updateImageSubtitleFrame, 100);
3058
}
3059
};
3060
3061
const handleImageTrackChange = async () =>
3062
{
3063
const allTextTracks = subtitleTracksFromPlayer(player);
3064
const activeImageEntry = imageSubtitleTracks.find(({ label }) =>
3065
allTextTracks.some((t) => t.label === label && String(t.mode || '') === 'showing'),
3066
);
3067
3068
const wantedStreamIndex = activeImageEntry ? activeImageEntry.streamIndex : null;
3069
3070
if (wantedStreamIndex === activeImageStreamIndex) return;
3071
3072
activeImageStreamIndex = wantedStreamIndex;
3073
3074
if (wantedStreamIndex === null)
3075
{
3076
if (imageOverlayContainer) imageOverlayContainer.style.display = 'none';
3077
if (frameExtractionTimer) clearInterval(frameExtractionTimer);
3078
} else
3079
{
3080
createImageOverlay();
3081
await updateImageSubtitleFrame();
3082
scheduleFrameExtraction();
3083
}
3084
};
3085
3086
// Add image subtitle tracks to VideoJS (as empty stubs so they show in menu)
3087
if (imageSubtitleTracks.length)
3088
{
3089
for (const imgTrack of imageSubtitleTracks)
3090
{
3091
player.addRemoteTextTrack({
3092
kind: 'subtitles',
3093
src: '/api/files/video-stream/empty.vtt',
3094
srclang: imgTrack.language || 'und',
3095
label: imgTrack.label,
3096
}, true);
3097
}
3098
}
3099
3100
// -------------------------------------------------------------------------
3101
2986
3102
let sentPartway = false;
2987
3103
let sentFully = false;
2988
3104
const markWatch = async () =>
@@ -3020,6 +3136,11 @@ async function renderStreamedVideoPreview (item, targetEl)
3020
3136
3021
3137
player.ready(() =>
3022
3138
{
3139
if (imageSubtitleTracks.length)
3140
{
3141
player.textTracks().addEventListener('change', handleImageTrackChange);
3142
}
3143
3023
3144
const volume = Number(prefs.volume);
3024
3145
if (Number.isFinite(volume))
3025
3146
{
@@ -3043,6 +3164,16 @@ async function renderStreamedVideoPreview (item, targetEl)
3043
3164
player.on('loadedmetadata', trackPrefs.applySavedTracks);
3044
3165
player.on('loadedplaylist', trackPrefs.applySavedTracks);
3045
3166
3167
player.on('play', scheduleFrameExtraction);
3168
player.on('pause', () =>
3169
{
3170
if (frameExtractionTimer)
3171
{
3172
clearInterval(frameExtractionTimer);
3173
frameExtractionTimer = null;
3174
}
3175
});
3176
player.on('seeking', updateImageSubtitleFrame);
3046
3177
player.on('timeupdate', markWatch);
3047
3178
player.on('ended', () =>
3048
3179
{
@@ -3058,6 +3189,15 @@ async function renderStreamedVideoPreview (item, targetEl)
3058
3189
3059
3190
STATE.previewCleanup = () =>
3060
3191
{
3192
if (imageSubtitleTracks.length)
3193
{
3194
try { player.textTracks().removeEventListener('change', handleImageTrackChange); } catch { /* ignore */ }
3195
}
3196
if (frameExtractionTimer)
3197
{
3198
clearInterval(frameExtractionTimer);
3199
frameExtractionTimer = null;
3200
}
3061
3201
releaseActivePreviewVideoStreamSession();
3062
3202
try { markWatch(); } catch { /* ignore */ }
3063
3203
try { trackPrefs.captureSelectedTracks(); } catch { /* ignore */ }
@@ -2942,9 +2942,9 @@ async function renderStreamedVideoPreview (item, targetEl)2942
const prefs = loadUserVideoPrefs();2942
const prefs = loadUserVideoPrefs();2943
applyUserVideoTextTrackPrefs();2943
applyUserVideoTextTrackPrefs();2944
2944
2945
if ((session.subtitleSummary && session.subtitleSummary.total > 0) && !(session.subtitleTracks || []).length)2945
if ((session.subtitleSummary && session.subtitleSummary.skipped > 0))2946
{2946
{2947
toast('This video has subtitle tracks, but none are compatible with web playback.', 'err');2947
toast('Some subtitle tracks could not be converted for web playback.', 'err');2948
}2948
}2949
2949
2950
STATE.previewDownloadAction = () => preparePreviewDownload(item);2950
STATE.previewDownloadAction = () => preparePreviewDownload(item);@@ -2983,6 +2983,122 @@ async function renderStreamedVideoPreview (item, targetEl)2983
2983
2984
ensureVideoPlaybackUi(player, session.durationSeconds);2984
ensureVideoPlaybackUi(player, session.durationSeconds);2985
2985
2986
// -------------------------------------------------------------------------2987
// Image-based subtitle overlays (PGS, DVD, etc.)2988
// Extracted and displayed as PNG frames at the bottom of the video.2989
// -------------------------------------------------------------------------2990
const imageSubtitleTracks = session.imageSubtitleTracks || [];2991
let activeImageStreamIndex = null;2992
let imageOverlayContainer = null;2993
let imageOverlayImg = null;2994
let frameExtractionTimer = null;2995
2996
const createImageOverlay = () =>2997
{2998
if (imageOverlayContainer) return;2999
3000
imageOverlayContainer = document.createElement('div');3001
imageOverlayContainer.className = 'video-subtitle-overlay';3002
imageOverlayContainer.style.position = 'absolute';3003
imageOverlayContainer.style.bottom = '0';3004
imageOverlayContainer.style.left = '0';3005
imageOverlayContainer.style.right = '0';3006
imageOverlayContainer.style.display = 'none';3007
imageOverlayContainer.style.textAlign = 'center';3008
imageOverlayContainer.style.zIndex = '1';3009
imageOverlayContainer.style.padding = '4px';3010
imageOverlayContainer.style.backgroundColor = 'rgba(0, 0, 0, 0.3)';3011
imageOverlayContainer.style.maxHeight = '30%';3012
imageOverlayContainer.style.overflow = 'hidden';3013
3014
imageOverlayImg = document.createElement('img');3015
imageOverlayImg.style.maxHeight = '100%';3016
imageOverlayImg.style.maxWidth = '100%';3017
imageOverlayImg.style.display = 'none';3018
imageOverlayImg.addEventListener('error', () => imageOverlayImg.style.display = 'none');3019
3020
imageOverlayContainer.appendChild(imageOverlayImg);3021
3022
const videoContainer = video.parentElement;3023
videoContainer.style.position = 'relative';3024
videoContainer.appendChild(imageOverlayContainer);3025
};3026
3027
const updateImageSubtitleFrame = async () =>3028
{3029
if (activeImageStreamIndex === null || !imageOverlayImg) return;3030
3031
try3032
{3033
const currentTime = player.currentTime();3034
const url = `/api/files/video-stream-subtitle-frame?filePath=${encodeURIComponent(item.path)}&streamIndex=${activeImageStreamIndex}&timeSeconds=${currentTime}`;3035
3036
// Set a new src to trigger the load; use cache busting to ensure fresh frames3037
imageOverlayImg.src = url + '&t=' + Date.now();3038
imageOverlayImg.style.display = 'block';3039
imageOverlayContainer.style.display = 'block';3040
} catch3041
{3042
imageOverlayImg.style.display = 'none';3043
}3044
};3045
3046
const scheduleFrameExtraction = () =>3047
{3048
if (frameExtractionTimer)3049
{3050
clearInterval(frameExtractionTimer);3051
}3052
3053
// Update frame every 100ms while playing3054
if (!player.paused())3055
{3056
void updateImageSubtitleFrame();3057
frameExtractionTimer = setInterval(updateImageSubtitleFrame, 100);3058
}3059
};3060
3061
const handleImageTrackChange = async () =>3062
{3063
const allTextTracks = subtitleTracksFromPlayer(player);3064
const activeImageEntry = imageSubtitleTracks.find(({ label }) =>3065
allTextTracks.some((t) => t.label === label && String(t.mode || '') === 'showing'),3066
);3067
3068
const wantedStreamIndex = activeImageEntry ? activeImageEntry.streamIndex : null;3069
3070
if (wantedStreamIndex === activeImageStreamIndex) return;3071
3072
activeImageStreamIndex = wantedStreamIndex;3073
3074
if (wantedStreamIndex === null)3075
{3076
if (imageOverlayContainer) imageOverlayContainer.style.display = 'none';3077
if (frameExtractionTimer) clearInterval(frameExtractionTimer);3078
} else3079
{3080
createImageOverlay();3081
await updateImageSubtitleFrame();3082
scheduleFrameExtraction();3083
}3084
};3085
3086
// Add image subtitle tracks to VideoJS (as empty stubs so they show in menu)3087
if (imageSubtitleTracks.length)3088
{3089
for (const imgTrack of imageSubtitleTracks)3090
{3091
player.addRemoteTextTrack({3092
kind: 'subtitles',3093
src: '/api/files/video-stream/empty.vtt',3094
srclang: imgTrack.language || 'und',3095
label: imgTrack.label,3096
}, true);3097
}3098
}3099
3100
// -------------------------------------------------------------------------3101
2986
let sentPartway = false;3102
let sentPartway = false;2987
let sentFully = false;3103
let sentFully = false;2988
const markWatch = async () =>3104
const markWatch = async () =>@@ -3020,6 +3136,11 @@ async function renderStreamedVideoPreview (item, targetEl)3020
3136
3021
player.ready(() =>3137
player.ready(() =>3022
{3138
{3139
if (imageSubtitleTracks.length)3140
{3141
player.textTracks().addEventListener('change', handleImageTrackChange);3142
}3143
3023
const volume = Number(prefs.volume);3144
const volume = Number(prefs.volume);3024
if (Number.isFinite(volume))3145
if (Number.isFinite(volume))3025
{3146
{@@ -3043,6 +3164,16 @@ async function renderStreamedVideoPreview (item, targetEl)3043
player.on('loadedmetadata', trackPrefs.applySavedTracks);3164
player.on('loadedmetadata', trackPrefs.applySavedTracks);3044
player.on('loadedplaylist', trackPrefs.applySavedTracks);3165
player.on('loadedplaylist', trackPrefs.applySavedTracks);3045
3166
3167
player.on('play', scheduleFrameExtraction);3168
player.on('pause', () =>3169
{3170
if (frameExtractionTimer)3171
{3172
clearInterval(frameExtractionTimer);3173
frameExtractionTimer = null;3174
}3175
});3176
player.on('seeking', updateImageSubtitleFrame);3046
player.on('timeupdate', markWatch);3177
player.on('timeupdate', markWatch);3047
player.on('ended', () =>3178
player.on('ended', () =>3048
{3179
{@@ -3058,6 +3189,15 @@ async function renderStreamedVideoPreview (item, targetEl)3058
3189
3059
STATE.previewCleanup = () =>3190
STATE.previewCleanup = () =>3060
{3191
{3192
if (imageSubtitleTracks.length)3193
{3194
try { player.textTracks().removeEventListener('change', handleImageTrackChange); } catch { /* ignore */ }3195
}3196
if (frameExtractionTimer)3197
{3198
clearInterval(frameExtractionTimer);3199
frameExtractionTimer = null;3200
}3061
releaseActivePreviewVideoStreamSession();3201
releaseActivePreviewVideoStreamSession();3062
try { markWatch(); } catch { /* ignore */ }3202
try { markWatch(); } catch { /* ignore */ }3063
try { trackPrefs.captureSelectedTracks(); } catch { /* ignore */ }3203
try { trackPrefs.captureSelectedTracks(); } catch { /* ignore */ }@@ -1075,13 +1075,19 @@ router.post('/video-stream', requireAuth, async (req, res, next) =>
1075
1075
sessionId: session.sessionId,
1076
1076
playlistUrl: `/api/files/video-stream/${session.sessionId}/master.m3u8`,
1077
1077
durationSeconds: session.durationSeconds || null,
1078
subtitleSummary: session.subtitleSummary || { total: 0, converted: 0, skipped: 0 },
1078
subtitleSummary: session.subtitleSummary || { total: 0, converted: 0, imageBased: 0, skipped: 0 },
1079
1079
subtitleTracks: session.subtitleTracks.map((track) => ({
1080
1080
label: track.label,
1081
1081
language: track.language,
1082
1082
default: !!track.isDefault,
1083
1083
src: `/api/files/video-stream/${session.sessionId}/${track.fileName}`,
1084
1084
})),
1085
imageSubtitleTracks: (session.imageSubtitleTracks || []).map((track) => ({
1086
label: track.label,
1087
language: track.language,
1088
default: !!track.isDefault,
1089
streamIndex: track.streamIndex,
1090
})),
1085
1091
});
1086
1092
} catch (err)
1087
1093
{
@@ -1117,6 +1123,93 @@ router.delete('/video-stream/:sessionId', requireAuth, (req, res) =>
1117
1123
res.json({ ok: true });
1118
1124
});
1119
1125
1126
// ---------------------------------------------------------------------------
1127
// GET /api/files/video-stream-subtitle-frame
1128
// Authenticated. Extracts a subtitle frame (image) at a given timestamp.
1129
// Query params: filePath, streamIndex, timeSeconds
1130
// ---------------------------------------------------------------------------
1131
router.get('/video-stream-subtitle-frame', requireAuth, async (req, res, next) =>
1132
{
1133
try
1134
{
1135
const filePath = String(req.query.filePath || '').trim();
1136
const streamIndex = Number.parseInt(String(req.query.streamIndex || ''), 10);
1137
const timeSeconds = Number.parseFloat(String(req.query.timeSeconds || ''), 10);
1138
1139
if (!filePath || !Number.isInteger(streamIndex) || streamIndex < 0 || !Number.isFinite(timeSeconds) || timeSeconds < 0)
1140
{
1141
return res.status(400).json({ error: 'filePath, streamIndex (non-negative int), and timeSeconds (non-negative number) are required.' });
1142
}
1143
1144
const { config, roots } = loadConfigAndRoots();
1145
const info = resolveRootForUser(filePath, roots, req.user, config);
1146
if (!info || !canReadInfo(req.user, info))
1147
{
1148
return res.status(403).json({ error: 'Access denied.' });
1149
}
1150
1151
if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory())
1152
{
1153
return res.status(404).json({ error: 'File not found.' });
1154
}
1155
1156
// Extract frame at the given timestamp using ffmpeg
1157
const { spawn } = require('child_process');
1158
const ffmpegPath = process.env.FFMPEG_PATH || 'ffmpeg';
1159
const timeStr = `${Math.floor(timeSeconds / 3600)}:${Math.floor((timeSeconds % 3600) / 60)}:${(timeSeconds % 60).toFixed(3)}`;
1160
1161
const child = spawn(ffmpegPath, [
1162
'-hide_banner',
1163
'-loglevel', 'error',
1164
'-ss', timeStr,
1165
'-i', filePath,
1166
'-map', `0:s:${streamIndex}`,
1167
'-frames:v', '1',
1168
'-c:v', 'png',
1169
'-f', 'image2pipe',
1170
'pipe:1',
1171
]);
1172
1173
let frameData = Buffer.alloc(0);
1174
let stderr = '';
1175
1176
child.stdout.on('data', (chunk) =>
1177
{
1178
frameData = Buffer.concat([frameData, chunk]);
1179
});
1180
1181
child.stderr.on('data', (chunk) =>
1182
{
1183
stderr += chunk.toString();
1184
});
1185
1186
child.on('close', (code) =>
1187
{
1188
if (code !== 0)
1189
{
1190
return res.status(500).json({ error: 'Failed to extract subtitle frame.' });
1191
}
1192
1193
if (frameData.length === 0)
1194
{
1195
return res.status(404).json({ error: 'No subtitle frame at this timestamp.' });
1196
}
1197
1198
res.set('Content-Type', 'image/png');
1199
res.set('Cache-Control', 'private, max-age=3600');
1200
res.send(frameData);
1201
});
1202
1203
child.on('error', (err) =>
1204
{
1205
return res.status(503).json({ error: 'ffmpeg unavailable.' });
1206
});
1207
} catch (err)
1208
{
1209
next(err);
1210
}
1211
});
1212
1120
1213
// ---------------------------------------------------------------------------
1121
1214
// GET /api/files/download-zip?dir=<absolute-path>
1122
1215
// Authenticated. Streams a zip of the given directory.
@@ -1075,13 +1075,19 @@ router.post('/video-stream', requireAuth, async (req, res, next) =>1075
sessionId: session.sessionId,1075
sessionId: session.sessionId,1076
playlistUrl: `/api/files/video-stream/${session.sessionId}/master.m3u8`,1076
playlistUrl: `/api/files/video-stream/${session.sessionId}/master.m3u8`,1077
durationSeconds: session.durationSeconds || null,1077
durationSeconds: session.durationSeconds || null,1078
subtitleSummary: session.subtitleSummary || { total: 0, converted: 0, skipped: 0 },1078
subtitleSummary: session.subtitleSummary || { total: 0, converted: 0, imageBased: 0, skipped: 0 },1079
subtitleTracks: session.subtitleTracks.map((track) => ({1079
subtitleTracks: session.subtitleTracks.map((track) => ({1080
label: track.label,1080
label: track.label,1081
language: track.language,1081
language: track.language,1082
default: !!track.isDefault,1082
default: !!track.isDefault,1083
src: `/api/files/video-stream/${session.sessionId}/${track.fileName}`,1083
src: `/api/files/video-stream/${session.sessionId}/${track.fileName}`,1084
})),1084
})),1085
imageSubtitleTracks: (session.imageSubtitleTracks || []).map((track) => ({1086
label: track.label,1087
language: track.language,1088
default: !!track.isDefault,1089
streamIndex: track.streamIndex,1090
})),1085
});1091
});1086
} catch (err)1092
} catch (err)1087
{1093
{@@ -1117,6 +1123,93 @@ router.delete('/video-stream/:sessionId', requireAuth, (req, res) =>1117
res.json({ ok: true });1123
res.json({ ok: true });1118
});1124
});1119
1125
1126
// ---------------------------------------------------------------------------1127
// GET /api/files/video-stream-subtitle-frame1128
// Authenticated. Extracts a subtitle frame (image) at a given timestamp.1129
// Query params: filePath, streamIndex, timeSeconds1130
// ---------------------------------------------------------------------------1131
router.get('/video-stream-subtitle-frame', requireAuth, async (req, res, next) =>1132
{1133
try1134
{1135
const filePath = String(req.query.filePath || '').trim();1136
const streamIndex = Number.parseInt(String(req.query.streamIndex || ''), 10);1137
const timeSeconds = Number.parseFloat(String(req.query.timeSeconds || ''), 10);1138
1139
if (!filePath || !Number.isInteger(streamIndex) || streamIndex < 0 || !Number.isFinite(timeSeconds) || timeSeconds < 0)1140
{1141
return res.status(400).json({ error: 'filePath, streamIndex (non-negative int), and timeSeconds (non-negative number) are required.' });1142
}1143
1144
const { config, roots } = loadConfigAndRoots();1145
const info = resolveRootForUser(filePath, roots, req.user, config);1146
if (!info || !canReadInfo(req.user, info))1147
{1148
return res.status(403).json({ error: 'Access denied.' });1149
}1150
1151
if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory())1152
{1153
return res.status(404).json({ error: 'File not found.' });1154
}1155
1156
// Extract frame at the given timestamp using ffmpeg1157
const { spawn } = require('child_process');1158
const ffmpegPath = process.env.FFMPEG_PATH || 'ffmpeg';1159
const timeStr = `${Math.floor(timeSeconds / 3600)}:${Math.floor((timeSeconds % 3600) / 60)}:${(timeSeconds % 60).toFixed(3)}`;1160
1161
const child = spawn(ffmpegPath, [1162
'-hide_banner',1163
'-loglevel', 'error',1164
'-ss', timeStr,1165
'-i', filePath,1166
'-map', `0:s:${streamIndex}`,1167
'-frames:v', '1',1168
'-c:v', 'png',1169
'-f', 'image2pipe',1170
'pipe:1',1171
]);1172
1173
let frameData = Buffer.alloc(0);1174
let stderr = '';1175
1176
child.stdout.on('data', (chunk) =>1177
{1178
frameData = Buffer.concat([frameData, chunk]);1179
});1180
1181
child.stderr.on('data', (chunk) =>1182
{1183
stderr += chunk.toString();1184
});1185
1186
child.on('close', (code) =>1187
{1188
if (code !== 0)1189
{1190
return res.status(500).json({ error: 'Failed to extract subtitle frame.' });1191
}1192
1193
if (frameData.length === 0)1194
{1195
return res.status(404).json({ error: 'No subtitle frame at this timestamp.' });1196
}1197
1198
res.set('Content-Type', 'image/png');1199
res.set('Cache-Control', 'private, max-age=3600');1200
res.send(frameData);1201
});1202
1203
child.on('error', (err) =>1204
{1205
return res.status(503).json({ error: 'ffmpeg unavailable.' });1206
});1207
} catch (err)1208
{1209
next(err);1210
}1211
});1212
1120
// ---------------------------------------------------------------------------1213
// ---------------------------------------------------------------------------1121
// GET /api/files/download-zip?dir=<absolute-path>1214
// GET /api/files/download-zip?dir=<absolute-path>1122
// Authenticated. Streams a zip of the given directory.1215
// Authenticated. Streams a zip of the given directory.@@ -12,6 +12,14 @@ const STREAM_READY_TIMEOUT_MS = 20 * 1000;
12
12
const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;
13
13
const ESTIMATED_SEGMENT_BYTES = 1024 * 1024;
14
14
const STREAM_SEGMENT_RETENTION_COUNT = parseSegmentRetentionCount(process.env.VIDEO_STREAM_SEGMENT_RETENTION_COUNT);
15
const IMAGE_BASED_SUBTITLE_CODECS = new Set([
16
'hdmv_pgs_subtitle',
17
'pgssub',
18
'dvd_subtitle',
19
'dvdsub',
20
'dvb_subtitle',
21
'xsub',
22
]);
15
23
const sessions = new Map();
16
24
const leases = new Map();
17
25
const reusableSessionIdBySource = new Map();
@@ -530,11 +538,26 @@ function trackLabel (stream, fallback)
530
538
async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
531
539
{
532
540
const extractedTracks = [];
541
const imageBasedTracks = [];
533
542
let skippedCount = 0;
534
543
let trackNumber = 0;
535
544
536
545
for (const stream of subtitleStreams)
537
546
{
547
const codecName = String(stream.codec_name || '').toLowerCase();
548
549
// Image-based subtitle formats will be shown as DOM overlays via frame extraction.
550
if (IMAGE_BASED_SUBTITLE_CODECS.has(codecName))
551
{
552
imageBasedTracks.push({
553
label: trackLabel(stream, `Subtitle ${stream.relativeIndex + 1}`),
554
language: trackLanguage(stream),
555
isDefault: !!stream.disposition.default,
556
streamIndex: stream.relativeIndex,
557
});
558
continue;
559
}
560
538
561
const fileName = `subtitle-${trackNumber}.vtt`;
539
562
const targetPath = path.join(outputDir, fileName);
540
563
@@ -567,6 +590,7 @@ async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)
567
590
568
591
return {
569
592
tracks: extractedTracks,
593
imageBasedTracks,
570
594
skippedCount,
571
595
};
572
596
}
@@ -747,9 +771,11 @@ async function initialiseSession (session)
747
771
748
772
const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);
749
773
session.subtitleTracks = subtitleResult.tracks;
774
session.imageSubtitleTracks = subtitleResult.imageBasedTracks;
750
775
session.subtitleSummary = {
751
776
total: streamInfo.subtitles.length,
752
777
converted: subtitleResult.tracks.length,
778
imageBased: subtitleResult.imageBasedTracks.length,
753
779
skipped: subtitleResult.skippedCount,
754
780
};
755
781
startHlsTranscode(session, streamInfo);
@@ -786,13 +812,15 @@ async function createVideoStreamSession (sourcePath)
786
812
787
813
session = {
788
814
id: sessionId,
789
sourcePath: sourceKey,
815
sourcePath: String(sourcePath || ''),
816
sourceKey: sourceKey,
790
817
outputDir,
791
818
expiresAt: Date.now() + STREAM_SESSION_TTL_MS,
792
819
process: null,
793
820
error: null,
794
821
subtitleTracks: [],
795
subtitleSummary: { total: 0, converted: 0, skipped: 0 },
822
imageSubtitleTracks: [],
823
subtitleSummary: { total: 0, converted: 0, imageBased: 0, skipped: 0 },
796
824
readyPromise: null,
797
825
activeRequests: 0,
798
826
lastActivityAt: Date.now(),
@@ -830,9 +858,11 @@ async function createVideoStreamSession (sourcePath)
830
858
sessionId: lease.id,
831
859
durationSeconds: session.durationSeconds,
832
860
subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),
861
imageSubtitleTracks: (session.imageSubtitleTracks || []).map((track) => ({ ...track })),
833
862
subtitleSummary: {
834
863
total: Number(session.subtitleSummary.total || 0),
835
864
converted: Number(session.subtitleSummary.converted || 0),
865
imageBased: Number(session.subtitleSummary.imageBased || 0),
836
866
skipped: Number(session.subtitleSummary.skipped || 0),
837
867
},
838
868
};
@@ -12,6 +12,14 @@ const STREAM_READY_TIMEOUT_MS = 20 * 1000;12
const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;12
const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;13
const ESTIMATED_SEGMENT_BYTES = 1024 * 1024;13
const ESTIMATED_SEGMENT_BYTES = 1024 * 1024;14
const STREAM_SEGMENT_RETENTION_COUNT = parseSegmentRetentionCount(process.env.VIDEO_STREAM_SEGMENT_RETENTION_COUNT);14
const STREAM_SEGMENT_RETENTION_COUNT = parseSegmentRetentionCount(process.env.VIDEO_STREAM_SEGMENT_RETENTION_COUNT);15
const IMAGE_BASED_SUBTITLE_CODECS = new Set([16
'hdmv_pgs_subtitle',17
'pgssub',18
'dvd_subtitle',19
'dvdsub',20
'dvb_subtitle',21
'xsub',22
]);15
const sessions = new Map();23
const sessions = new Map();16
const leases = new Map();24
const leases = new Map();17
const reusableSessionIdBySource = new Map();25
const reusableSessionIdBySource = new Map();@@ -530,11 +538,26 @@ function trackLabel (stream, fallback)530
async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)538
async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)531
{539
{532
const extractedTracks = [];540
const extractedTracks = [];541
const imageBasedTracks = [];533
let skippedCount = 0;542
let skippedCount = 0;534
let trackNumber = 0;543
let trackNumber = 0;535
544
536
for (const stream of subtitleStreams)545
for (const stream of subtitleStreams)537
{546
{547
const codecName = String(stream.codec_name || '').toLowerCase();548
549
// Image-based subtitle formats will be shown as DOM overlays via frame extraction.550
if (IMAGE_BASED_SUBTITLE_CODECS.has(codecName))551
{552
imageBasedTracks.push({553
label: trackLabel(stream, `Subtitle ${stream.relativeIndex + 1}`),554
language: trackLanguage(stream),555
isDefault: !!stream.disposition.default,556
streamIndex: stream.relativeIndex,557
});558
continue;559
}560
538
const fileName = `subtitle-${trackNumber}.vtt`;561
const fileName = `subtitle-${trackNumber}.vtt`;539
const targetPath = path.join(outputDir, fileName);562
const targetPath = path.join(outputDir, fileName);540
563
@@ -567,6 +590,7 @@ async function extractSubtitleTracks (sourcePath, outputDir, subtitleStreams)567
590
568
return {591
return {569
tracks: extractedTracks,592
tracks: extractedTracks,593
imageBasedTracks,570
skippedCount,594
skippedCount,571
};595
};572
}596
}@@ -747,9 +771,11 @@ async function initialiseSession (session)747
771
748
const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);772
const subtitleResult = await extractSubtitleTracks(session.sourcePath, session.outputDir, streamInfo.subtitles);749
session.subtitleTracks = subtitleResult.tracks;773
session.subtitleTracks = subtitleResult.tracks;774
session.imageSubtitleTracks = subtitleResult.imageBasedTracks;750
session.subtitleSummary = {775
session.subtitleSummary = {751
total: streamInfo.subtitles.length,776
total: streamInfo.subtitles.length,752
converted: subtitleResult.tracks.length,777
converted: subtitleResult.tracks.length,778
imageBased: subtitleResult.imageBasedTracks.length,753
skipped: subtitleResult.skippedCount,779
skipped: subtitleResult.skippedCount,754
};780
};755
startHlsTranscode(session, streamInfo);781
startHlsTranscode(session, streamInfo);@@ -786,13 +812,15 @@ async function createVideoStreamSession (sourcePath)786
812
787
session = {813
session = {788
id: sessionId,814
id: sessionId,789
sourcePath: sourceKey,815
sourcePath: String(sourcePath || ''),816
sourceKey: sourceKey,790
outputDir,817
outputDir,791
expiresAt: Date.now() + STREAM_SESSION_TTL_MS,818
expiresAt: Date.now() + STREAM_SESSION_TTL_MS,792
process: null,819
process: null,793
error: null,820
error: null,794
subtitleTracks: [],821
subtitleTracks: [],795
subtitleSummary: { total: 0, converted: 0, skipped: 0 },822
imageSubtitleTracks: [],823
subtitleSummary: { total: 0, converted: 0, imageBased: 0, skipped: 0 },796
readyPromise: null,824
readyPromise: null,797
activeRequests: 0,825
activeRequests: 0,798
lastActivityAt: Date.now(),826
lastActivityAt: Date.now(),@@ -830,9 +858,11 @@ async function createVideoStreamSession (sourcePath)830
sessionId: lease.id,858
sessionId: lease.id,831
durationSeconds: session.durationSeconds,859
durationSeconds: session.durationSeconds,832
subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),860
subtitleTracks: session.subtitleTracks.map((track) => ({ ...track })),861
imageSubtitleTracks: (session.imageSubtitleTracks || []).map((track) => ({ ...track })),833
subtitleSummary: {862
subtitleSummary: {834
total: Number(session.subtitleSummary.total || 0),863
total: Number(session.subtitleSummary.total || 0),835
converted: Number(session.subtitleSummary.converted || 0),864
converted: Number(session.subtitleSummary.converted || 0),865
imageBased: Number(session.subtitleSummary.imageBased || 0),836
skipped: Number(session.subtitleSummary.skipped || 0),866
skipped: Number(session.subtitleSummary.skipped || 0),837
},867
},838
};868
};@@ -776,7 +776,7 @@ describe('video streaming routes', () =>
776
776
expect(status).toBe(200);
777
777
expect(body.playlistUrl).toBe('/api/files/video-stream/sess-123/master.m3u8');
778
778
expect(body.durationSeconds).toBe(42.25);
779
expect(body.subtitleSummary).toEqual({ total: 0, converted: 0, skipped: 0 });
779
expect(body.subtitleSummary).toEqual({ total: 0, converted: 0, imageBased: 0, skipped: 0 });
780
780
expect(body.subtitleTracks).toEqual([
781
781
{
782
782
label: 'English',
@@ -776,7 +776,7 @@ describe('video streaming routes', () =>776
expect(status).toBe(200);776
expect(status).toBe(200);777
expect(body.playlistUrl).toBe('/api/files/video-stream/sess-123/master.m3u8');777
expect(body.playlistUrl).toBe('/api/files/video-stream/sess-123/master.m3u8');778
expect(body.durationSeconds).toBe(42.25);778
expect(body.durationSeconds).toBe(42.25);779
expect(body.subtitleSummary).toEqual({ total: 0, converted: 0, skipped: 0 });779
expect(body.subtitleSummary).toEqual({ total: 0, converted: 0, imageBased: 0, skipped: 0 });780
expect(body.subtitleTracks).toEqual([780
expect(body.subtitleTracks).toEqual([781
{781
{782
label: 'English',782
label: 'English',@@ -164,9 +164,11 @@ describe('video stream bootstrap', () =>
164
164
sessionId: expect.any(String),
165
165
durationSeconds: null,
166
166
subtitleTracks: [],
167
imageSubtitleTracks: [],
167
168
subtitleSummary: {
168
169
total: 0,
169
170
converted: 0,
171
imageBased: 0,
170
172
skipped: 0,
171
173
},
172
174
});
@@ -164,9 +164,11 @@ describe('video stream bootstrap', () =>164
sessionId: expect.any(String),164
sessionId: expect.any(String),165
durationSeconds: null,165
durationSeconds: null,166
subtitleTracks: [],166
subtitleTracks: [],167
imageSubtitleTracks: [],167
subtitleSummary: {168
subtitleSummary: {168
total: 0,169
total: 0,169
converted: 0,170
converted: 0,171
imageBased: 0,170
skipped: 0,172
skipped: 0,171
},173
},172
});174
});