Ytdl - Download audio file

Hello

i’m using ytdl-core in my meteor app. For the moment, I can receive the infos of a video, but now, I’m trying to download it but I’m getting lost with the ytdl-core doc … I’m trying to pipe the stream and using ‘fs’ but it doesn’t work …

Here is the React call of the method when I click on the download button:

handleClick() {
const videoUrl = "https://www.youtube.com/watch?v=73-jKLyDTJs";

Meteor.call('ytdl.download', videoUrl, function(err, result) {
  if (err) {
    console.log(err);
  } else {
    console.log(result.title);
  }
});
}

And now the Meteor Method:

import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import ytdl from 'ytdl-core';
import Future from 'fibers/future';
import fs from 'fs';

Meteor.methods({

  'ytdl.download'(videoUrl) {
    check(videoUrl, String);

    this.unblock();
    var future = new Future();

    ytdl.getInfo(videoUrl, function(err, info){
       var videoReadableStream = ytdl(videoUrl, { filter: 'audioonly'});
       var videoWritableStream = fs.createWriteStream('./' + info.title + '.mp3');
       var stream = videoReadableStream.pipe(videoWritableStream);
       console.log(stream);

       future.return(info);
     });
     return future.wait();
  },

});

The stream I log seems to display good infos and I can send good infos to the React component … But I don’t know how to download it …

Thanks for any help :slight_smile:

Cheers