Skip to content

Instantly share code, notes, and snippets.

@jonathasgouv
Last active May 5, 2024 15:10
Show Gist options
  • Save jonathasgouv/cf7e9d35678fdba3b536ba033209ed8d to your computer and use it in GitHub Desktop.
Save jonathasgouv/cf7e9d35678fdba3b536ba033209ed8d to your computer and use it in GitHub Desktop.
Download array of youtube videos as MP3
const fs = require('fs');
const ytdl = require('ytdl-core');
const sanitize = require("sanitize-filename");
async function downloadVideos(urls) {
try {
for (const url of urls) {
const info = await ytdl.getInfo(url);
const title = info.videoDetails.title;
const filename = sanitize(`${title}.mp3`);
ytdl(url, { filter: 'audioonly' })
.pipe(fs.createWriteStream(filename))
.on('finish', () => console.log(`"${filename}" foi baixado.`));
}
} catch (error) {
console.error('Ocorreu um erro ao baixar o vídeo:', error);
}
}
const urls = [
"https://www.youtube.com/watch?v=xvFZjo5PgG0",
"https://www.youtube.com/watch?v=dQw4w9WgXcQ"
];
downloadVideos(urls);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment