fix(video): поддержка Range на dev-сервере + Cues в начале webm (seek в плеере)
This commit is contained in:
1 parent
3d5fbe45ee
commit
8c34d3fa97
3 files changed
+33
-4
No files matched your search
Binary file not shown.
@@ -190,6 +190,9 @@ async function toWebm(inputPath, outPath, statusCtx) {
|
|||||||
} else {
|
} else {
|
||||||
a.push("-an");
|
a.push("-an");
|
||||||
}
|
}
|
||||||
|
// Резервируем место под Cues в начале файла, чтобы браузер мог перематывать
|
||||||
|
// (иначе Cues пишутся в конец и duration=N/A, seek не работает).
|
||||||
|
a.push("-reserve_index_space", "50000");
|
||||||
a.push(outPath);
|
a.push(outPath);
|
||||||
const d = getDurationSec(inputPath);
|
const d = getDurationSec(inputPath);
|
||||||
await runFfmpegProgress(a, {
|
await runFfmpegProgress(a, {
|
||||||
|
|||||||
+30
-4
@@ -237,11 +237,37 @@ function serveStatic(req, res) {
|
|||||||
}
|
}
|
||||||
const ext = path.extname(filePath).toLowerCase();
|
const ext = path.extname(filePath).toLowerCase();
|
||||||
const mime = MIME[ext] || "application/octet-stream";
|
const mime = MIME[ext] || "application/octet-stream";
|
||||||
fs.readFile(filePath, (e2, data) => {
|
const total = stat.size;
|
||||||
if (e2) { res.writeHead(500); res.end("Error"); return; }
|
const range = req.headers.range;
|
||||||
res.writeHead(200, { "Content-Type": mime });
|
|
||||||
res.end(data);
|
// Поддержка Range-запросов — нужна браузеру для перемотки видео/аудио
|
||||||
|
if (range) {
|
||||||
|
const m = /^bytes=(\d*)-(\d*)$/.exec(range);
|
||||||
|
if (m) {
|
||||||
|
let start = m[1] ? parseInt(m[1], 10) : 0;
|
||||||
|
let end = m[2] ? parseInt(m[2], 10) : total - 1;
|
||||||
|
if (isNaN(start) || isNaN(end) || start > end || end >= total) {
|
||||||
|
res.writeHead(416, { "Content-Range": `bytes */${total}` });
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.writeHead(206, {
|
||||||
|
"Content-Type": mime,
|
||||||
|
"Content-Range": `bytes ${start}-${end}/${total}`,
|
||||||
|
"Accept-Ranges": "bytes",
|
||||||
|
"Content-Length": end - start + 1,
|
||||||
|
});
|
||||||
|
fs.createReadStream(filePath, { start, end }).pipe(res);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res.writeHead(200, {
|
||||||
|
"Content-Type": mime,
|
||||||
|
"Accept-Ranges": "bytes",
|
||||||
|
"Content-Length": total,
|
||||||
});
|
});
|
||||||
|
fs.createReadStream(filePath).pipe(res);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user