Stream and Save

134 views Asked by At

I have a mp4 video stream coming in and I want to save it to disk but I also want to stream the data that has been saved to disk before it's finished so I can watch and save at the same time. My current is approach is to pipe the stream to disk and create a read stream from disk that is then sent, but currently it just loads a bit and then stops.

Here is my current code

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);
})()
0

There are 0 answers