Skip to content

Commit

Permalink
Refactor the main entry file
Browse files Browse the repository at this point in the history
* Now the audio file will be converted to MP3 immediately after
  being downloaded.
* Refactored the output file path creation
* Added `await` operator on call of function `ytdl.downloadFromInfo`.
  • Loading branch information
mitsuki31 committed Nov 11, 2023
1 parent ec85003 commit 38c726f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const fs = require('fs'), // File system module
path = require('path'), // Path module
ytdl = require('ytdl-core'); // Youtube Downloader module

const { convertToMp3 } = require('./lib/acodecs-cvt');

/**
* Normalizes a YouTube Music URL to its original YouTube format.
*
Expand Down Expand Up @@ -91,18 +93,19 @@ function normalizeYtMusicUrl(url) {
filter: 'audioonly' // Filter audio only
});

// The file name format
const filename = `${titleVideo}.m4a`;
const outStream = fs.createWriteStream(
path.join('download', filename));
const filename = path.join('download', `${titleVideo}.m4a`);
const outStream = fs.createWriteStream(filename);

console.log(`Processing... '${titleVideo}' (${ytdl.getVideoID(url)})`);
// Start downloading the audio and save to file
ytdl.downloadFromInfo(info, { format: format })
await ytdl.downloadFromInfo(info, { format: format })
.pipe(outStream);
outStream.on('finish', () => {
console.log(`Finished: '${filename}' [${
console.log(`Finished: '${path.basename(filename)}' [${
(new Date()).toISOString()}]`);

// Convert to MP3
convertToMp3(filename);
});
});
});
Expand Down

0 comments on commit 38c726f

Please sign in to comment.