Sebastian Olsen Sebastian Olsen - 1 year ago 143
Node.js Question

Converting audio with sox in nodejs

I have made sure that all that sox needs to function is installed, and I've also installed the sox package for node. Yet, this does not seem to work.

function encode(file, destination, quality) {
return new Promise((resolve, reject) => {

let job = sox.transcode(file, destination, {
sampleRate: 44100,
format: 'mp3',
bitRate: quality * 1024,

job.on('src', function(info) {

job.on('progress', (amountDone, amountTotal) => {
console.log("progress", amountDone, amountTotal);

job.on('error', (err) => {
reject('Could not transcode mp3.')

job.on('end', () => {
resolve('mp3 transcoded.')


at the end logs:

Transcode {
domain: null,
{ src: [Function],
progress: [Function],
error: [Function],
end: [Function] },
_eventsCount: 4,
_maxListeners: undefined,
inputFile: 'C:\\Users\\User\\Documents\\App\\Media\\media\\uploads\\audio/2016/269/1/0//10c746ef62374c6ab1f2ecfc36705618/original.mp3',
outputFile: 'C:\\Users\\User\\Documents\\App\\Media\\media\\uploads\\audio/2016/269/1/0//10c746ef62374c6ab1f2ecfc36705618/128.mp3',
{ sampleRate: 44100,
format: 'mp3',
bitRate: 196608,
channelCount: 2,
compressionQuality: 5 } }

There are no errors, so I wonder if something here is failing silently. Identifying a file works just fine:

let original = (path + 'original.mp3')

sox.identify(original, function(err, info) {
if (err) {
if (info.format !== 'mp3') {
throw('File must be mp3!')
console.log(info) // {format: 'mp3', ..}

The above code works just fine. but
does not.

Answer Source

The example code mentions a job.start() that (presumably) starts the transcoding job. I don't see that being called in your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download