fent / node-m3u8stream

Concatenates segments from a m3u8/dash-mpd playlist into a consumable stream.
MIT License
217 stars 54 forks source link

How do you download the video into chunks? #89

Closed mikemilla closed 3 years ago

mikemilla commented 3 years ago

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.

mikemilla commented 3 years ago

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`));
    }

});