我正在从express.js服务器的云存储桶中流一个视频文件,并且能够成功地将该流输送到前端的一个视频元素。但是,由于视频内容现在是逐步加载的,所以我无法在加载的视频中查找。
我从云存储对象获取视频流的node.js代码如下所示。
async function getVideoStream(gcloudFileName,res){
const bucket = storage.bucket(process.env.GCS_BUCKET);
const remoteFile = bucket.file(gcloudFileName);
return remoteFile.createReadStream().pipe(res)
}如何使这个视频流成为可能,以便用户可以通过视频的前端查找?的有益建议是非常感谢的。
编辑:
我尝试了@Rafael的答案,并使它在一定程度上发挥作用,但现在的问题是,流停止后,交付了最初的几块视频。同时,视频也停止播放。

发布于 2021-02-08 15:15:38
您需要使用远程请求,如@Szat拉丁美洲注释中提到的那样,这样服务器就可以一次发送文件块,而不是整个过程,您可以在这篇文章中找到完整的解释,但是应用到代码中,您可以执行以下操作:
async function getVideoStream(req,res){
const range = req.headers.range;
if (!range) {
res.status(400).send("Requires Range header");
}
const bucket = storage.bucket(process.env.GCS_BUCKET);
// I added the file name to your request's body
const remoteFile = bucket.file(req.body);
//calculates the chunck to be sent based on the video size
const videoSize = remoteFile.getMetadata().size;
const CHUNK_SIZE = 10 ** 6; // 1MB
const start = Number(range.replace(/\D/g, ""));
const end = Math.min(start + CHUNK_SIZE, videoSize - 1);
// Create headers
const contentLength = end - start + 1;
const headers = {
"Content-Range": `bytes ${start}-${end}/${videoSize}`,
"Accept-Ranges": "bytes",
"Content-Length": contentLength,
"Content-Type": "video/mp4",
};
// HTTP Status 206 for Partial Content
res.writeHead(206, headers);
//returns the chunk that you want
return remoteFile.createReadStream({ start, end }).pipe(res)
}https://stackoverflow.com/questions/66085162
复制相似问题