-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mitsuki31/feature/add-internal-converter
Add internal MP3 converter feature
- Loading branch information
Showing
4 changed files
with
117 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* `acodecs-cvt` is an abbreviation for Audio Codecs Converter. | ||
* @author Ryuu Mitsuki | ||
* @since 0.2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'), | ||
path = require('path'), | ||
ffmpeg = require('fluent-ffmpeg'); | ||
|
||
const optionsMp3 = { | ||
format: 'mp3', | ||
bitrate: '128k', | ||
frequency: 44100, | ||
codec: 'libmp3lame', | ||
channels: 2 | ||
}; | ||
|
||
function convertToMp3(inFile, options = optionsMp3) { | ||
// Regular expressions for audio codecs | ||
const audioCodecRegex = /(mp3|aac|wav|flac|ogg|wma|opus|amr|m4a)/i, // All known extension names of audio file | ||
noExtRegex = new RegExp(`(.+)(?:\\.${audioCodecRegex.source})$`); // Get the file name without its extension | ||
|
||
// Rename file extension to MP3 for the output file | ||
const outFile = path.join( | ||
path.dirname(inFile), | ||
`${noExtRegex.exec(path.basename(inFile))[1]}.${options.format}` | ||
); | ||
|
||
// Store the file names only without their path directories | ||
const ioBaseFile = [ | ||
path.basename(inFile), | ||
path.basename(outFile) | ||
]; | ||
|
||
console.log(`Processing '${noExtRegex.exec(ioBaseFile[0])[1]}' ...`); | ||
ffmpeg(inFile) // Input | ||
// Options | ||
.toFormat(options.format) | ||
.audioBitrate(options.bitrate) | ||
.audioCodec(options.codec) | ||
.audioChannels(options.channels) | ||
.audioFrequency(options.frequency) | ||
|
||
// Handlers | ||
.on('error', (err) => { | ||
console.error(err); | ||
}) | ||
.on('progress', (info) => { | ||
const percentage = Math.floor(info.percent); | ||
// Write the progression percentage to the console | ||
process.stdout.write( | ||
`Progress: ${percentage < 0 ? 0 : percentage}%\r`); | ||
}) | ||
.on('end', () => { | ||
console.log('All done successfully.'); | ||
}) | ||
.save(outFile); // Output | ||
} | ||
|
||
|
||
module.exports = { | ||
optionsMp3, | ||
convertToMp3 | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"fluent-ffmpeg": "^2.1.2", | ||
"ytdl-core": "^4.11.5" | ||
} | ||
} |