TECHNICAL SPECS

Deep-dive technical documentation covering polyglot execution blocks, flat FFprobe tag extraction, metadata normalization mappings, and race condition prevention.

Polyglot Payload Extraction Mechanics

Name & Tag Fix uses a polyglot Batch/PowerShell wrapper. When executed as a batch file, CMD reads the top section and calls PowerShell to extract the embedded PowerShell script below the <#START_PS#> marker:

powershell -NoProfile -Command "(Get-Content -LiteralPath '%~f0' -Raw) -split '<#START_PS#>\r?\n' | Select-Object -Last 1 | Set-Content -LiteralPath '%TempPS1%' -Encoding UTF8"

This allows the script to be saved as a simple .bat file for drag-and-drop support, while running powerful PowerShell code internally without requiring separate .ps1 script files.

FFprobe Flat Format Metadata Probing

To avoid PowerShell JSON parsing crashes caused by case-insensitive duplicate keys in media tags, the script queries FFprobe using the flat key-value format:

$probeOut = & $ffprobePath -v quiet -show_format -show_streams -of flat $filePath

The output is parsed line-by-line using regular expressions to unescape quotes and handle embedded multiline tags cleanly:

if ($line -match '\.tags\.([^=]+)=(.*)$') {
    $key = $matches[1].ToLower()
    $val = $matches[2]
    if ($val -match '^"(.*)"$') {
        $val = $matches[1] -replace '\\"', '"' -replace '\\n', "`n"
    }
    $tags[$key] = $val
}

Standardized Tag Mapping Matrix

Name & Tag Fix normalizes inconsistent tag keys across formats (such as albumartist vs album_artist, or date vs year) into standard FFmpeg metadata tags:

Standard Key Source Tag Fallbacks FFmpeg Execution Flag
Title tags['title'] -metadata Title="Value"
Album Artist album_artist, albumartist -metadata "Album Artist=Value"
Track Reconstructed track / tracktotal -metadata Track="03/12"
Disc Reconstructed disc / disctotal -metadata Disc="1/2"
Original Date originaldate, originalyear -metadata "Original Date=Value"
Publisher publisher, label -metadata Publisher="Value"

Base FFmpeg Flag Architecture

$ffmpegArgs = @("-y", "-v", "error", "-i", $filePath, "-c", "copy", "-map", "0:a", "-map_metadata", "-1", "-fflags", "+bitexact", "-metadata", "encoder=")

GUID Temp Collision Prevention

To support parallel processing and prevent file lock conflicts when running multiple script instances simultaneously, temporary working files are tagged with a unique 8-character GUID string:

$randStr = [guid]::NewGuid().ToString().Substring(0,8)
$tempFile = Join-Path $dir "temp_stripped_${randStr}_$fileName"

Once FFmpeg completes processing successfully, the original source file is removed and the temporary file is renamed to the target filename atomically.