Skip to content

aac-to-wav Audio Converter

Usage

example_aac_to_wav.sh
python scripts/aac_to_wav.py [-h] -i INPUT_DIR [-c CHANNEL] [-r RATE]
Batch-convert aac audios in a folder to wav format with ffmpeg.

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT_DIR, --input_dir INPUT_DIR
                        Directory of input audios to convert.
  -c CHANNEL, --channel CHANNEL
                        Number of audio channels in output.
  -r RATE, --rate RATE  Sample rate of audio output.

Example

python scripts/aac_to_wav.py --input_dir="dropbox/" -c 1 -r 16000 

scripts.aac_to_wav

convert_to_wav(input_audio_path, num_channels=1, sampling_rate=16000)

Convert aac audio file to wav at same directory.

Parameters:

Name Type Description Default
input_audio_path str

Path to aac file.

required
num_channels int

Number of output audio channels. Defaults to 1.

1
sampling_rate int

Output audio sampling rate. Defaults to 16_000.

16000

Returns:

Type Description
subprocess.CompletedProcess

Finished subprocess.

Source code in scripts/aac_to_wav.py
def convert_to_wav(
    input_audio_path: str, num_channels: int = 1, sampling_rate: int = 16_000
) -> subprocess.CompletedProcess:
    """
    Convert aac audio file to wav at same directory.

    Args:
        input_audio_path (str):
            Path to aac file.
        num_channels (int, optional):
            Number of output audio channels. Defaults to `1`.
        sampling_rate (int, optional):
            Output audio sampling rate. Defaults to `16_000`.

    Returns:
        subprocess.CompletedProcess:
            Finished subprocess.
    """
    # replace input file's extension to wav as output file path
    output_audio_path = Path(input_audio_path).with_suffix(".wav")

    # equivalent to:
    # ffmpeg -i {input_audio_path} -acodec pcm_s16le -ac {num_channels} \
    #       -ar {sampling_rate} {output_audio_path}
    job = subprocess.run(
        [
            "ffmpeg",
            "-loglevel",
            "quiet",
            "-hide_banner",
            "-y",
            "-i",
            input_audio_path,
            "-acodec",
            "pcm_s16le",
            "-ac",
            str(num_channels),
            "-ar",
            str(sampling_rate),
            str(output_audio_path),
        ],
        stderr=subprocess.DEVNULL,
        stdout=subprocess.DEVNULL,
        stdin=subprocess.PIPE,
    )

    return job

parse_args(args)

Utility argument parser function for batch audio conversion.

Parameters:

Name Type Description Default
args List[str]

List of arguments.

required

Returns:

Type Description
argparse.Namespace

Objects with arguments values as attributes.

Source code in scripts/aac_to_wav.py
def parse_args(args: List[str]) -> argparse.Namespace:
    """
    Utility argument parser function for batch audio conversion.

    Args:
        args (List[str]):
            List of arguments.

    Returns:
        argparse.Namespace:
            Objects with arguments values as attributes.
    """
    parser = argparse.ArgumentParser(
        prog="python scripts/aac_to_wav.py",
        description="Batch-convert aac audios in a folder to wav format with ffmpeg.",
    )

    parser.add_argument(
        "-i",
        "--input_dir",
        type=str,
        required=True,
        help="Directory of input audios to convert.",
    )
    parser.add_argument(
        "-c",
        "--channel",
        type=int,
        default=1,
        help="Number of audio channels in output.",
    )
    parser.add_argument(
        "-r", "--rate", type=int, default=16_000, help="Sample rate of audio output."
    )
    return parser.parse_args(args)