Last active
December 28, 2018 16:37
-
-
Save akatopo/be631c9dd54b3d5e2ba02296d409e54c to your computer and use it in GitHub Desktop.
convert audio files to mp3 with ffmpeg
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
#!/usr/bin/env node | |
const path = require('path'); | |
const execSync = require('child_process').execSync; | |
const { | |
which, | |
test, | |
mkdir, | |
ls | |
} = require('shelljs'); | |
const outputFolderPath = './mp3'; | |
if (!which('ffmpeg')) { | |
throw new Error('ffmpeg required'); | |
} | |
if (test('-e', outputFolderPath) && !test('-d', outputFolderPath)) { | |
throw new Error(`Output folder ${outputFolderPath} already exists and is not a directory`) | |
} | |
mkdir('-p', outputFolderPath) | |
ls() | |
.filter((filename) => !test('-d', filename)) | |
.forEach((filename) => { | |
const basename = path.basename(filename, path.extname(filename)); | |
const outputPath = path.join(outputFolderPath, `${basename}.mp3`); | |
try { | |
execSync( | |
`ffmpeg -i "${filename}" -acodec libmp3lame -aq 4 "${outputPath}"`, | |
{ stdio: 'inherit' } | |
); | |
} | |
catch (ex) {} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment