Skip to content

Instantly share code, notes, and snippets.

@ljcucc
Created June 19, 2023 00:32
Show Gist options
  • Save ljcucc/993c1e91f3e162c9855fec5c70b958b4 to your computer and use it in GitHub Desktop.
Save ljcucc/993c1e91f3e162c9855fec5c70b958b4 to your computer and use it in GitHub Desktop.
class Prompt {
rl;
constructor() {
const { stdin: input, stdout: output } = require('node:process');
const readline = require('node:readline');
this.rl = readline.createInterface({ input, output });
}
ask(prompt) {
return new Promise(resolve => {
this.rl.question(prompt, ans => resolve(ans));
});
}
}
const ytdl = require("ytdl-core");
const ffmpeg = require('fluent-ffmpeg');
const NodeID3 = require('node-id3')
function download(url) {
return new Promise(resolve => {
const video = ytdl(url, { quality: 'highestaudio', })
video.once("info", e => {
const cover = e.videoDetails.thumbnails[0];
const title = e.videoDetails.title;
const author = e.videoDetails.author;
console.log({title, author, cover})
const filename = `${path}/${title}.mp3`;
ffmpeg(video)
.audioBitrate(128)
.save(filename)
.on('end', () => {
NodeID3.write({
artist: author,
// APIC: cover,
album: albumName
}, filename);
resolve();
})
})
video.on('progress', (chunkLength, downloaded, total) => {
const percent = downloaded / total;
console.log(percent)
});
});
}
var path, albumName;
async function main() {
var p = new Prompt();
path = await p.ask("path: ");
albumName = await p.ask("name of the album: ");
var playlist = (await p.ask("url1 url2 url3 ... > ")).split(" ");
await Promise.all(
playlist.map(download)
)
return;
}
main().then(process.exit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment