Sebastian Olsen Sebastian Olsen - 2 months ago 15
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) => {
console.log(destination)

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

job.on('src', function(info) {
console.log(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.')
})

console.log(job)
})
}


The
console.log
at the end logs:

Transcode {
domain: null,
_events:
{ 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',
options:
{ 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) {
console.log(err)
throw(err)
}
if (info.format !== 'mp3') {
throw('File must be mp3!')
}
console.log(info) // {format: 'mp3', ..}
});


The above code works just fine. but
sox.transcode
does not.

Answer

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

Comments