-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3.js
102 lines (86 loc) · 3.17 KB
/
mp3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const fs = require('fs');
const ytdl = require('ytdl-core');
const cliProgress = require('cli-progress');
const prompt = require('prompt-sync')();
// Function to display progress bar
function showProgressBar(totalSize) {
const progressBar = new cliProgress.SingleBar({
format: 'Downloading | {bar} | {percentage}% | ETA: {eta}s | {value}/{total} MB',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true,
});
progressBar.start(totalSize, 0);
return progressBar;
}
// Function to download and convert video to MP3
async function downloadMP3(url) {
try {
// Fetch video info
const info = await ytdl.getInfo(url);
const videoDetails = info.videoDetails;
const { title, lengthSeconds } = videoDetails;
const author = videoDetails.author.name;
const thumbnailUrl = videoDetails.thumbnails.pop().url;
console.log('Video Details:');
console.log(`Title: ${title}`);
console.log(`Author: ${author}`);
console.log(`Thumbnail URL: ${thumbnailUrl}`);
console.log('\n');
// Prompt for the output file name
let fileName = prompt('Enter the output file name (without extension)' +
' or press enter to use the default file name: ');
if (!fileName) {
fileName = title
}
fileName = fileName
.replace(/[\s-+|.]+/g, '_')
.replace(/_+/g, '_')
let folderName
let onHomeMusic = prompt("Do you wish to store the mp3 in the home music directory?" +
"\nEnter (y|Y|yes) for 'yes' and any other for 'no': ")
if (['y', 'yes'].includes(onHomeMusic.toLocaleLowerCase().trim())) {
folderName = `${require('os').homedir()}/Music`
} else {
folderName = prompt('Enter the output folder path: ');
if (folderName) {
if (!fs.existsSync(folderName)) {
console.error('Submitted folder path does not exist. Please submit a valid folder path.');
return;
}
} else {
folderName = 'mp3'
// create folder if not exist
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName);
}
}
}
const outputFilePath = `${folderName}/${fileName}.mp3`;
// Start the download and conversion process
const audioReadableStream = ytdl(url, { filter: 'audioonly', quality: 'highestaudio' });
const outputStream = fs.createWriteStream(outputFilePath);
const progressBar = showProgressBar(lengthSeconds);
audioReadableStream.pipe(outputStream);
// Update progress bar based on data received
audioReadableStream.on('data', (chunk) => {
progressBar.increment(chunk.length);
});
// Handle download completion
audioReadableStream.on('end', () => {
progressBar.stop();
console.log(`\nAudio downloaded and converted successfully! Saved as: ${outputFilePath}`);
});
// Handle errors during download
audioReadableStream.on('error', (error) => {
progressBar.stop();
console.error('Error occurred during download:', error);
});
} catch (error) {
console.error('Error fetching video info:', error);
}
}
// Prompt for YouTube video URL
const videoUrl = prompt('Enter the YouTube video URL: ');
// Start the download process
downloadMP3(videoUrl);