I'm using the libraries from npm-module called ytdl-core and ffmpeg for NodeJS I have 2 functions from those modules, one is to download video and other to get audio from Youtube videos. Both functions are asynchronized, but in my algortithme one depends of the other. First, I want to download the audio, and after I want get the video of the audio source, and I don't want to execute both asynchronized, I want synchronized operation
This is my code:
var stream = app.ytdl(req.body.linkYoutube,{quality: 18});
//First, download audio
app.ffmpeg(stream)
.audioBitrate(128)
.save(`${__dirname}/music.mp3`)
.on('progress', (p) => {
app.readline.cursorTo(process.stdout, 0);
process.stdout.write(`${p.targetSize}kb downloaded`);
})
.on('end', () => {
res.status(200).download('./controllers/music.mp3');
});
//Second, download video of that audio
app.ytdl(req.body.linkYoutube, { filter : (format) =>
format.container === 'mp4' &&
format.qualityLabel ==='720p'// &&
// format.itag === 18
})
.pipe(app.fs.createWriteStream('video.mp4'));
I know that it's not good, but It's a good exercice to learn asynchronisme, Thanks!