fent/node-m3u8stream

How do you download the video into chunks?

mikemilla opened this issue · 1 comments

I am looking for the right way to add a live stream url in, and save it into ~20 second chunked mp4s.

Can someone post an example? I tried quite a few ways, but no luck messing the stream options.

Nevermind. If you need to know. Try this:

const fs = require('fs');
const readline = require('readline');
const m3u8stream = require('m3u8stream')

var index = 0;

const stream = m3u8stream(url);
stream.pipe(fs.createWriteStream(`media_${index}.mp4`));
stream.on('progress', (segment, totalSegments, downloaded) => {

    const percentage = (segment.num / totalSegments * 100).toFixed(2);

    readline.cursorTo(process.stdout, 0);
    process.stdout.write(
        `${segment.num} of ${totalSegments} segments ` +
        `(${percentage}%) ` +
        `${(downloaded / 1024 / 1024).toFixed(2)}MB downloaded`);
    
    if (percentage >= 100) {
        stream.unpipe();
        index++
        stream.pipe(fs.createWriteStream(`media_${index}.mp4`));
    }

});