106 lines
3.0 KiB
JavaScript
Executable File
106 lines
3.0 KiB
JavaScript
Executable File
const readline = require("readline");
|
||
const ytdl = require("ytdl-core");
|
||
const ffmpeg = require("fluent-ffmpeg");
|
||
const id3 = require('node-id3');
|
||
const axios = require('axios');
|
||
var request = require("request").defaults({ encoding: null });
|
||
const chalk = require('chalk');
|
||
|
||
/**
|
||
* log function for logging in one shell line.
|
||
* @param something the thing that should be logged.
|
||
*/
|
||
const log = (something) => {
|
||
readline.clearLine(process.stdout, 0);
|
||
readline.cursorTo(process.stdout, 0);
|
||
process.stdout.write(something);
|
||
}
|
||
|
||
const convertTime = (timeString /* '00:00:46.27' */) => {
|
||
const timeParts = timeString.split(':');
|
||
let seconds = Number(timeParts[2]);
|
||
seconds = seconds + (Number(timeParts[1]) * 60) + (Number(timeParts[0]) * 60 * 60);
|
||
return seconds;
|
||
};
|
||
|
||
if (process.argv.length !== 5) {
|
||
console.log(chalk.red('Not getting metadata for this song!'));
|
||
}
|
||
|
||
ffmpeg.setFfmpegPath(__dirname + '/ffmpeg');
|
||
|
||
log(chalk.yellow("ℹ️ Getting information from YouTube..."));
|
||
|
||
ytdl.getInfo(process.argv[2], (err, info) => {
|
||
let stream = ytdl(process.argv[2], {
|
||
quality: "highestaudio"
|
||
//filter: 'audioonly',
|
||
});
|
||
|
||
const path = `${__dirname}/music/${info.title}.mp3`;
|
||
|
||
log(chalk.yellow("🏁 Starting download..."));
|
||
|
||
ffmpeg(stream)
|
||
.audioBitrate(128)
|
||
.save(path)
|
||
.on("progress", p => {
|
||
const progress = convertTime(p.timemark);
|
||
log(
|
||
chalk.blue(
|
||
`⬇️ ${Math.floor(
|
||
(progress / Number(info.length_seconds)) * 100
|
||
)}% downloaded.`
|
||
)
|
||
);
|
||
})
|
||
.on('error', err => {
|
||
log(chalk.red('Error: ' + err.message))
|
||
process.exit();
|
||
})
|
||
.on("end", () => {
|
||
if (process.argv.length === 5) {
|
||
// Retreiving info from itunes api and writing tags to downloaded file.
|
||
const artist = process.argv[3];
|
||
const song = process.argv[4];
|
||
|
||
log(chalk.yellow("🎵 Calling iTunes api..."));
|
||
|
||
axios.get(`https://itunes.apple.com/search?term=${artist} ${song}&entity=song`).then(res => {
|
||
request.get(res.data.results[0].artworkUrl100, function(err, _res, body) {
|
||
|
||
log(chalk.green("✅ Retreived information!"));
|
||
|
||
var tags = {
|
||
title: song,
|
||
artist: res.data.results[0].artistName,
|
||
album: res.data.results[0].collectionName,
|
||
image: {
|
||
mime: "jpeg",
|
||
type: {
|
||
id: 3,
|
||
name: "front cover"
|
||
},
|
||
imageBuffer: body
|
||
},
|
||
date: res.data.results[0].releaseDate,
|
||
fileType: "mp3",
|
||
genre: res.data.results[0].primaryGenreName
|
||
};
|
||
|
||
log(chalk.yellow("✍🏻 Writing tags to mp3 file..."));
|
||
|
||
id3.update(tags, path);
|
||
|
||
log(chalk.green(`✅ Successfully downloaded ${info.title}.mp3!\n`));
|
||
|
||
process.exit();
|
||
});
|
||
});
|
||
} else {
|
||
log(chalk.green(`✅ Successfully downloaded ${info.title}.mp3!`));
|
||
|
||
process.exit();
|
||
}
|
||
});
|
||
}); |