Just to give some context, i'm trying to download an entire playlist on youtube by fetching the data from the youtube API, which will return an object containing all the ids and titles in the playlist. Then i have a function to download a single video, which i'm trying to use inside a for loop to go through all ids on the playlist, and i need to come up with some way to wait the full completion of a function before the next iteration, like a queue.
Function to download the video:
async function downloadVideo (videoId, videoTitle) {
const mp3String = `yt-dlp -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 -o ./music/reggae/"%(title)s.%(ext)s" ${videoId}`
console.log(`Download started. videoname: ${videoTitle}`);
const child = exec(mp3String, (err, res) => {
if (err) return console.log(err);
})
child.on('exit', () => {
console.log(`Download finished at: ./${videoTitle}.mp3`)
return
});
}
Function to download the entire playlist:
async function downloadEntirePlaylist (playlistID) {
playlistData = await (fetchPlaylist(playlistID)) // returns all ids and titles from playlist
for (let i=0;i<playlistData.idList.length;i++) {
await downloadVideo(playlistData.idList[i], playlistData.videoTitles[i])
}
// download -> wait till completion -> start another download ->> ...finish entire playlist and return
}
I tried using the await to actually stop the code before finishing the loop so downloadVideo() can actually finish before the loop ends, but it's not working...
On the output, they all start almost at the same time and the conversion of each one also starts basically together, and that's a big issue when it comes to playlists with more videos, since my pc starts frying and errors begin to stack.
Download started. videoname: C418 - Haggstrom - Minecraft Volume Alpha
Download started. videoname: C418 - Wet Hands - Minecraft Volume Alpha
Download started. videoname: C418 - Dry Hands - Minecraft Volume Alpha
Download started. videoname: C418 - Droopy likes your Face - Minecraft Volume Alpha
Download finished at: ./C418 - Droopy likes your Face - Minecraft Volume Alpha.mp3
Download finished at: ./C418 - Wet Hands - Minecraft Volume Alpha.mp3
Download finished at: ./C418 - Haggstrom - Minecraft Volume Alpha.mp3
Download finished at: ./C418 - Dry Hands - Minecraft Volume Alpha.mp3