【问题标题】:Stream MP4 instead of downloading with Express流式传输 MP4 而不是使用 Express 下载
【发布时间】:2020-06-20 13:12:50
【问题描述】:

我有一个可以下载 mp4 文件的 URI,我正在尝试创建一个代理来流式传输该文件而不是下载它,但我尝试过的所有操作仍然是下载文件而不是流式传输它。 Content-Type 设置为 video/mp4,这是我能找到的唯一答案

res.contentType('video/mp4');
req.pipe(request.get(url.fullUrl)).pipe(res);

【问题讨论】:

    标签: node.js express http video-streaming


    【解决方案1】:

    我认为您必须创建一个可读流,然后将其传递给响应,如下所示:

    const path = 'video.mp4';
    
    const head = {
        'Content-Length': 123456, // Get the video file size,
        'Content-Type': 'video/mp4',
    }
    
    res.writeHead(200, head);
    fs.createReadStream(path).pipe(res);
    

    【讨论】:

    • 我设法让它用 req.pipe(request.get(url.fullUrl).pipe(res)) 加载视频播放器,但没有视频加载,也没有发送数据,可以' t 使用 fs.createReadStream 因为文件不在系统中
    猜你喜欢
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2017-09-08
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2015-03-13
    相关资源
    最近更新 更多