This code converts audio files within a Quake 3 game directory to the Opus format using the opusenc
command-line tool.
npm run import -- "convert quake 3 audio"
var path = require('path')
var fs = require('fs')
var importer = require('../Core')
var {glob} = importer.import("glob files")
var execCmd = importer.import("spawn child process")
var mkdirpSync = importer.import("mkdirp")
var { chext, chroot } = importer.import("changing file names")
var {audioTypes} = importer.import("quake 3 file whitelist")
async function convertAudio(root) {
var result = []
var output = path.join(path.dirname(root), path.basename(root) + '-converted')
var files = await glob(audioTypes.map(a => '**/*' + a), root)
for(var i = 0; i < files.length; i++) {
var outFile, inFile = files[i]
outFile = chroot(chext(inFile, '.opus'), root, output)
mkdirpSync(path.dirname(outFile))
await execCmd(`opusenc --bitrate 24 --vbr "${inFile}" "${outFile}"`, {quiet: false})
result.push(outFile)
}
return result
}
module.exports = convertAudio
const { glob, mkdirpSync } = require('importer')('../Core')
const execCmd = require('spawn-child-process')
const { chext, chroot } = require('changing-file-names')
const { audioTypes } = require('quake-3-file-whitelist')
/**
* Converts audio files in a given directory to opus format.
*
* @param {string} root - The root directory to scan for audio files.
* @returns {Promise} A list of paths to the converted audio files.
*/
async function convertAudio(root) {
// Define the output directory with a unique suffix
const output = `${path.dirname(root)}/ converted`;
// Get a list of audio files to convert
const files = await glob(audioTypes.map(a => `**/*.${a}`), root);
// Convert each audio file
const result = await Promise.all(files.map(async (file) => {
// Create the output file path
const outFile = chroot(chext(file, '.opus'), root, output);
// Create the output directory if it doesn't exist
await mkdirpSync(path.dirname(outFile));
// Run the opusenc command to convert the file
await execCmd(`opusenc --bitrate 24 --vbr "${file}" "${outFile}"`, { quiet: false });
// Return the path to the converted file
return outFile;
}));
return result;
}
module.exports = convertAudio;
This code snippet converts audio files in a Quake 3 game directory to the Opus format.
Here's a breakdown:
Dependencies:
path
: Node.js module for working with file paths.fs
: Node.js module for file system operations.importer
: Custom module likely containing utility functions.glob
: Module for finding files matching a pattern.spawn
: Module for running shell commands.mkdirp
: Module for creating directories recursively.changing file names
: Custom module for renaming files.quake 3 file whitelist
: Custom module defining supported audio types.convertAudio
Function:
glob
..opus
extension using chroot
and chext
.opusenc
to convert the audio file to Opus format with a bitrate of 24 kbps using execCmd
.result
array.Exports:
convertAudio
function for use in other parts of the application.Let me know if you have any more questions!