DEV Community

Discussion on: Twilio Hackathon Help Thread

 
philnash profile image
Phil Nash

Ah! Because it's a readable stream it will emit the data event with chunks of the data which you can write into your writable stream. Something like this should work:

const file = fs.createWriteStream("shortvid.flv");
const readable = ytdl(shortVideoUrl);
readable.addEventListener('data', chunk => {
  file.write(chunk);
});
readable.addEventListener('progress', (chunkByteLength, totalBytesDownloaded, totalBytes) => {
  console.log(`Downloaded ${totalBytesDownloaded} out of ${totalBytes}`);
})
readable.addEventListener('end', () => {
  file.end();
});

But, if you've switched to Python and you're happy with that, then that's cool too!

Thread Thread
 
technoplato profile image
Michael Lustig - halfjew22@gmail.com

If only the Twilio dev evang team could answer all of my Javascript queries 🤣

So just for my learning opportunity here - when you call file.write(chunk), it's appending those bytes to the file, and calling file.end() will close that file write stream and result in the video file automagically?

Side note about python: It's been awesome to stand up my hack. The last step of adding a message queue (which I thought would be the hardest) has been the easiest. Gotta love learning!

Thread Thread
 
philnash profile image
Phil Nash

Yes, that is the gist of it! Using the data and end events of the readable and calling write and end of the writeable is actually the equivalent of using readable.pipe(writeable). But, the documentation suggests you don't want to mix pipe and event based operations and using the progress event would mess that up.

I'm happy to answer JS questions if I can! I just happened to work with other readables/writeables a couple of weeks ago, so it was sort of fresh.

It's good to hear that the Python version has come on nicely for you. I love hackathons for the learning potential!

Thread Thread
 
technoplato profile image
Michael Lustig - halfjew22@gmail.com

Brilliant! Well I followed you so be expecting some in the future.

Python for whatever reason (or perhaps the competitive nature of the Hackathon) has me more excited about developing than I’ve been in a while, so thanks for that!

And thanks again for your assistance.