Last active
May 5, 2024 15:10
-
-
Save jonathasgouv/cf7e9d35678fdba3b536ba033209ed8d to your computer and use it in GitHub Desktop.
Download array of youtube videos as MP3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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