Skip to content

Instantly share code, notes, and snippets.

@paulwongx
Last active January 6, 2023 22:39
Show Gist options
  • Save paulwongx/dfcd511db3beb22c6e67914944fd3f28 to your computer and use it in GitHub Desktop.
Save paulwongx/dfcd511db3beb22c6e67914944fd3f28 to your computer and use it in GitHub Desktop.
Download Youtube mp3
import ytdl from "ytdl-core";
import fs from "fs";
import ffmpeg from "fluent-ffmpeg";
import readline from "readline";
// https://github.com/fent/node-ytdl-core/blob/master/example/convert_to_mp3.js
const id = 'nMfPqeZjc2c';
let stream = ytdl(id, {
quality: 'highestaudio',
});
let start = Date.now();
ffmpeg(stream)
.audioBitrate(128)
.save(`${__dirname}/${id}.mp3`)
.on('progress', (p:any) => {
readline.cursorTo(process.stdout, 0);
process.stdout.write(`${p.targetSize}kb downloaded`);
})
.on('end', () => {
console.log(`\ndone, thanks - ${(Date.now() - start) / 1000}s`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment