Created
June 4, 2025 14:39
-
-
Save shivanshtalwar0/8f9d8f8d9376bce45309b33131e77876 to your computer and use it in GitHub Desktop.
A reliable Class which will convert mjr to wav for janus without any errors
This file contains hidden or 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
const { spawn } = require('child_process'); | |
const path = require('path'); | |
const fs = require('fs'); | |
class JanusMjrConverter { | |
constructor(inputDir, mjrFilename, wavFilename) { | |
this.inputDir = path.resolve(inputDir); | |
this.mjrPath = path.join(this.inputDir, mjrFilename); | |
this.wavPath = path.join(this.inputDir, wavFilename); | |
this.totalCount = 0; | |
} | |
runJanusCommand(args) { | |
return new Promise((resolve, reject) => { | |
const process = spawn('janus-pp-rec', args); | |
let output = ''; | |
process.stdout.on('data', data => { | |
output += data.toString(); | |
}); | |
process.stderr.on('data', data => { | |
output += data.toString(); // Capture stderr as well (like Bash 2>&1) | |
}); | |
process.on('close', code => { | |
resolve(output); | |
}); | |
process.on('error', err => { | |
reject(err); | |
}); | |
}); | |
} | |
errorRtpPackets(output) { | |
return (output.match(/Late pre-reset packet: \d+/g) || []).length; | |
} | |
extractRtpPacketCount(output) { | |
const match = output.match(/Counted (\d+) frame packets/); | |
return match ? parseInt(match[1], 10) : 0; | |
} | |
async getIgnoreCount() { | |
const args = ['-p', this.mjrPath]; | |
const parsedOutput = await this.runJanusCommand(args); | |
const errorCount = this.errorRtpPackets(parsedOutput); | |
this.totalCount = this.extractRtpPacketCount(parsedOutput); | |
return this.totalCount - errorCount; | |
} | |
async convert() { | |
try { | |
const ignoreCount = await this.getIgnoreCount(); | |
const args = [ | |
'-i', ignoreCount.toString(), | |
'--restamp=2000', | |
'--restamp-packets=20', | |
'--restamp-min-th=300', | |
'--silence-distance=50', | |
'--audioskew=500', | |
this.mjrPath, | |
this.wavPath | |
]; | |
const output = await this.runJanusCommand(args); | |
if (!output.includes('Writing .wav')) { | |
console.warn('Invalid MJR file, skipping:', path.basename(this.mjrPath)); | |
console.log(output); | |
} else { | |
console.log(output); | |
console.log(`Total packets: ${this.totalCount}`); | |
} | |
} catch (err) { | |
console.error('Conversion failed:', err.message); | |
} | |
} | |
} | |
// Example usage: | |
const converter = new JanusMjrConverter( | |
'./input', | |
'435c9111d65d49192bec20202bfb0060-peer-audio.mjr', | |
'435c9111d65d49192bec20202bfb0060-peer-audio.wav' | |
); | |
converter.convert(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment