TECHNICAL SPECS

Deep-dive technical documentation detailing stream probe parsing, subroutines, dynamic variable export, and FFmpeg execution flags.

FFprobe Metadata Extraction Engine

Audio Track Extractor issues a specialized ffprobe execution call using collision-proof temporary files (%temp%\stream_q_%random%_%random%.txt) to query key audio metadata streams into plain text:

"%FFPROBE_CMD%" -v error -select_streams a -show_entries stream=index,codec_name,sample_rate,channel_layout,bit_rate,bits_per_sample:stream_tags=language,title -of default=noprint_wrappers=1 "%filepath%" <nul > "%stq%"
Flag / Parameter Technical Purpose
-v error Restricts diagnostic output strictly to fatal errors, ensuring clean metadata streams.
-select_streams a Filters out video, subtitle, and attachment streams, parsing audio streams only.
-show_entries stream=... Requests index, codec name, sample rate, channels, bit rate, quantization bit-depth, ISO language tags, and track title.
-of default=noprint_wrappers=1 Outputs structured KEY=VALUE plain-text keypairs for easy line-by-line parsing inside Batch for /f loops.

FFmpeg Execution Routine

Once an audio stream index is selected by the user, the script executes direct bitstream extraction with strict overwrite prevention:

"%FFMPEG_CMD%" -nostdin -hide_banner -loglevel error -i "%filepath%" -map 0:%final_idx% -c:a %acodec% -map_metadata 0 -fflags +bitexact "%out%" -n <nul
Flag Functionality
-nostdin <nul Prevents FFmpeg from hijacking Command Prompt standard input during batch execution.
-map 0:%final_idx% Selects the exact target stream index chosen by the user in the UI menu.
-c:a copy / -c:a pcm_s24le Enforces raw stream copy (or exact quantization for uncompressed DVD/Blu-Ray PCM).
-map_metadata 0 Preserves global container metadata tags (album, title, commentary).
-fflags +bitexact Suppresses Lavf/Lavc timestamp signatures for clean, bit-exact header creation.
-n Prevents accidental file overwrites at the FFmpeg engine level as a secondary safeguard.

Batch Subroutine Scope Management

The script uses an atomic endlocal consolidation block to safely parse metadata with delayed expansion without leaking local variables or losing metadata attributes:

:parse_stream_data
set /a count+=1

setlocal enabledelayedexpansion
set "d=!cur_codec!"

:: Bit depth check
if defined cur_bps if "!cur_bps!" neq "0" if "!cur_bps!" neq "N/A" set "d=!d! !cur_bps!bit"

:: Sample rate with decimal precision (e.g., 44.1kHz)
if defined cur_sr if "!cur_sr!" neq "0" if "!cur_sr!" neq "N/A" (
    set /a "sr_k=!cur_sr! / 1000"
    set /a "sr_m=(!cur_sr! %% 1000) / 100"
    if !sr_m! gtr 0 (
        set "d=!d! !sr_k!.!sr_m!kHz"
    ) else (
        set "d=!d! !sr_k!kHz"
    )
)

if defined cur_ch if "!cur_ch!" neq "unknown" set "d=!d! !cur_ch!"

:: Bitrate validation via regex check
if defined cur_br (
    echo(!cur_br!| findstr /r "^[0-9][0-9]*$" >nul && (
        set /a "br_k=!cur_br! / 1000"
        set "d=!d! !br_k! kbps"
    )
)

if defined cur_lang set "d=!d! [!cur_lang!]"
if defined cur_title set "d=!d! - !cur_title!"

:: Consolidation: Export ALL variables atomically out of local scope
for /f "delims=" %%X in ("!d!") do (
    endlocal
    set "stream_%count%_display=%%X"
    set "stream_%count%_idx=%cur_idx%"
    set "stream_%count%_codec=%cur_codec%"
    set "stream_%count%_bps=%cur_bps%"
    set "stream_%count%_lang=%cur_lang%"
    set "stream_%count%_title=%cur_title%"
)
goto :eof

This approach uses setlocal enabledelayedexpansion inside the parsing routine to calculate fractional sample rates and validate numeric bit rates with findstr, then exports all attributes atomically to the parent execution scope inside a single endlocal loop.