0

我有一个 mp4 视频流进来,我想将它保存到磁盘,但我也想在它完成之前将已保存到磁盘的数据流式传输,以便我可以同时观看和保存。我目前的方法是将流传输到磁盘并从磁盘创建一个读取流,然后发送,但目前它只是加载一点然后停止。

这是我当前的代码

const express = require('express');
const axios = require('axios').default;
const fs = require('fs');
const Path = require('path');
const yargs = require('yargs');
const { hideBin } = require('yargs/helpers');

const argv = yargs(hideBin(process.argv)).argv;

const url = argv._[0];
const path = Path.resolve(argv._[1])

const app = express();

(async () => {
    const response = await axios({
        method: 'get',
        url: url,
        responseType: 'stream'
    });

    const ws = fs.createWriteStream(path);
    response.data.pipe(ws);
    
    app.get('/stream', (req, res) => {
        res.header('Content-Type', 'video/mp4')
        const rs = fs.createReadStream(path);
        
        rs.pipe(res);

        // rs.on('close',() => res.end());
    });
    
    app.listen(3000);
})()
4

0 回答 0