Last active
April 24, 2024 19:42
-
-
Save RaschidJFR/fc70d468d5e25b18843c7d6effe4fbc1 to your computer and use it in GitHub Desktop.
Convert mp3 files to wav recursively using ffmpeg in node.js
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
// Convert mp3 files recursively to wav using [fluent-ffmpeg](https://github.com/fluent-ffmpeg/node-fluent-ffmpeg) for [node.js](https://nodejs.org) | |
// | |
// 1. Install fluent-ffmpeg: `npm install fluent-ffmpeg` | |
// 2. Run this script: `node mp3ToWav.js [path/to/file/or/folder]` | |
convertMp3ToWav = function (input) { | |
let segments = input.split('/'); | |
let filename = segments[segments.length - 1]; | |
let extension = filename.split('.')[1]; | |
let name = filename.split('.')[0]; | |
let folder = input.replace(filename, ''); | |
let output = folder + name + '.wav'; | |
console.log("\Converting file %s", output) | |
var ffmpeg = require('fluent-ffmpeg'); | |
var command = ffmpeg(input) | |
.inputFormat('mp3') | |
.audioCodec('pcm_s16le') | |
.format('wav') | |
.save(output) | |
return output; | |
} | |
convertFiles = function (path, options) { | |
return new Promise((resolve, reject) => { | |
// Load modules | |
const fs = require('fs'); | |
// Is argument a file? | |
if (fs.statSync(path).isFile()) { | |
// mp3 | |
if (path.endsWith(options.from)) { | |
let result = convertMp3ToWav(path, options); | |
console.log(result); | |
resolve() | |
} | |
} | |
console.log('\nCrawling directory \'%s\'', path); | |
// Search for all audio files in folder | |
fs.readdir(path, (err, files) => { | |
let readFolderActions = []; | |
// Process all found files | |
if (files) { | |
files.forEach(file => { | |
let filePath = path + '/' + file; | |
let readItem = null; | |
// is folder | |
if (fs.statSync(filePath).isDirectory()) { | |
readItem = convertFiles(filePath, options); | |
} | |
// Not folder | |
else { | |
// is PDF | |
if (file.endsWith(options.from)) { | |
convertMp3ToWav(filePath, options); | |
} | |
} | |
readFolderActions.push(readItem); | |
}); | |
} else { | |
reject('Directorio %s not found.', path); | |
} | |
// Wait for all actions to be processed | |
Promise.all(readFolderActions).then((results) => { | |
resolve(); | |
}) | |
}) | |
}); | |
} | |
convertFiles(process.argv[2], {from: 'mp3'}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment