quake 3 | convert nonalpha | copy quake 3 script files | Search

This code converts audio files within a Quake 3 game directory to the Opus format using the opusenc command-line tool.

Run example

npm run import -- "convert quake 3 audio"

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

What the code could have been:

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:

  1. Dependencies:

  2. convertAudio Function:

  3. Exports:

Let me know if you have any more questions!