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 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)
    minfo = MediaInfo.parse(fileIn.file)
    trackinfo = fileIn.get_mediainfo(minfo)
    container = fileIn.get_containerinfo(minfo)
    has_containerfmt = container is not None and hasattr(container, "format") and container.format is not None
    preprocess = sanitize_pre(preprocess)

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

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

    if (form == "wave" or (has_containerfmt and container.format.lower() == "wave")) and wont_process:
        return fileIn
    if valid_type.allows_flac():
        valid_type = valid_type.remove_flac()
        if (form == "flac" or (has_containerfmt and container.format.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 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")
    p = subprocess.run([exe, "-encoders"], capture_output=True, text=True)
    output = p.stderr + p.stdout
    for line in output.splitlines():
        if "libfdk_aac" in line.lower():
            return True
    return False

has_libFLAC()

Returns if whatever installation of qaac being used has libFLAC and as such can accept flac input

Source code in muxtools/audio/audioutils.py
def has_libFLAC() -> bool:
    """
    Returns if whatever installation of qaac being used has libFLAC
    and as such can accept flac input
    """
    exe = get_executable("qaac")
    p = subprocess.run([exe, "--check"], capture_output=True, text=True)
    output = p.stderr + p.stdout
    for line in output.splitlines():
        if "libflac" in line.lower():
            return True
    return False

is_fancy_codec(track)

Tries to check if a track is some fancy DTS (X, Headphone X) or TrueHD with Atmos

Parameters:

Name Type Description Default
track Track

Input track to check

required
Source code in muxtools/audio/audioutils.py
def is_fancy_codec(track: Track) -> bool:
    """
    Tries to check if a track is some fancy DTS (X, Headphone X) or TrueHD with Atmos

    :param track:   Input track to check
    """
    codec_id = str(track.codec_id).casefold()
    if codec_id == "A_TRUEHD".casefold() or "truehd" in track.commercial_name.lower():
        if "atmos" in track.commercial_name.lower():
            return True
        if not hasattr(track, "format_additionalfeatures") or not track.format_additionalfeatures:
            return False
        # If it contains something other than the fallback AC-3 track it's probably atmos
        return "ch" in track.format_additionalfeatures
    elif codec_id == "A_DTS".casefold() or "dts" in track.format.lower():
        # Not even lossless if this doesn't exist
        if not hasattr(track, "format_additionalfeatures") or not track.format_additionalfeatures:
            return False
        # If those additional features contain something after removing "XLL" its some fancy stuff
        return bool(re.sub("XLL", "", str(track.format_additionalfeatures).strip(), re.IGNORECASE))

    return False