Skip to content

audioutils

ensure_valid_in(fileIn, supports_pipe=True, preprocess=None, valid_type=ValidInputType.FLAC, caller=None)

Ensures valid input for any encoder that accepts flac (all of them). Passes existing file if no need to dither and is either wav or flac.

Source code in .venv/lib/python3.11/site-packages/muxtools/audio/audioutils.py
def ensure_valid_in(
    fileIn: AudioFile,
    supports_pipe: bool = True,
    preprocess: Preprocessor | Sequence[Preprocessor] | None = None,
    valid_type: ValidInputType = ValidInputType.FLAC,
    caller: Any = None,
) -> AudioFile | subprocess.Popen:
    """
    Ensures valid input for any encoder that accepts flac (all of them).
    Passes existing file if no need to dither and is either wav or flac.
    """
    if fileIn.has_multiple_tracks(caller):
        msg = f"'{fileIn.file.name}' is a container with multiple tracks.\n"
        msg += f"The first audio track will be {'piped' if supports_pipe else 'extracted'} using default ffmpeg."
        warn(msg, caller, 5)
    trackinfo = fileIn.get_trackinfo()
    container = fileIn.get_containerinfo()
    preprocess = sanitize_pre(preprocess)

    form = trackinfo.get_audio_format()
    if form:
        if form.is_lossy:
            danger(f"It's strongly recommended to not reencode lossy audio! ({trackinfo.codec_name})", caller, 5)
        elif form.should_not_transcode():
            warn("Encoding tracks with special DTS Features or Atmos is very much discouraged.", caller, 5)

    wont_process = not any([p.can_run(trackinfo, preprocess) for p in preprocess])

    if (form == AudioFormat.PCM and container.format_name.lower() == "wav") and wont_process:
        return fileIn
    if valid_type.allows_flac():
        valid_type = valid_type.remove_flac()
        if (form == AudioFormat.FLAC and container.format_name.lower() == "flac") and wont_process:
            return fileIn

    if valid_type == ValidInputType.FLAC:
        from .encoders import FF_FLAC

        if supports_pipe:
            return FF_FLAC(preprocess=preprocess).get_pipe(fileIn)
        else:
            return FF_FLAC(compression_level=0, preprocess=preprocess, output=os.path.join(get_temp_workdir(), "tempflac")).encode_audio(
                fileIn, temp=True
            )
    else:
        return get_pcm(fileIn, trackinfo, supports_pipe, preprocess, valid_type, caller)

has_libFDK()

Returns if whatever installation of ffmpeg being used has been compiled with libFDK

Source code in .venv/lib/python3.11/site-packages/muxtools/audio/audioutils.py
def has_libFDK() -> bool:
    """
    Returns if whatever installation of ffmpeg being used has been compiled with libFDK
    """
    exe = get_executable("ffmpeg")
    _, readout = communicate_stdout([exe, "-encoders"])
    for line in readout.splitlines():
        if "libfdk_aac" in line.lower():
            return True
    return False

qaac_compatcheck()

Checks if the qAAC installation has libflac and returns the qaac version.

Source code in .venv/lib/python3.11/site-packages/muxtools/audio/audioutils.py
def qaac_compatcheck() -> str:
    """
    Checks if the qAAC installation has libflac and returns the qaac version.
    """
    exe = get_executable("qaac")
    _, readout = communicate_stdout([exe, "--check"])
    if "libflac" not in readout.lower():
        raise error(
            "Your installation of qaac does not have libFLAC.\nIt is needed for proper piping from ffmpeg etc."
            + "\nYou can download it from https://github.com/xiph/flac/releases or run muxtools deps"
            + "\nFor installation check https://github.com/nu774/qaac/wiki/Installation",
            "QAAC",
        )

    if match := re.search(r"qaac (\d+\.\d+(?:\.\d+)?)", readout, re.I):
        return match.group(1)

    return "Unknown version"