Skip to content

Instantly share code, notes, and snippets.

@akatopo
Last active December 28, 2018 16:37
Show Gist options
  • Save akatopo/be631c9dd54b3d5e2ba02296d409e54c to your computer and use it in GitHub Desktop.
Save akatopo/be631c9dd54b3d5e2ba02296d409e54c to your computer and use it in GitHub Desktop.
convert audio files to mp3 with ffmpeg
#!/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