Deep-dive technical documentation covering polyglot execution blocks, JSON probing, APEv2 footer truncation, ReplayGain regex filtering, and thread-safe GUID processing.
Name & Tag Fix uses a hybrid Batch/PowerShell polyglot structure[cite: 1, 2]. When executed, Windows CMD processes the batch header and calls PowerShell to extract the embedded script below the <#START_PS#> marker[cite: 1]:
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 drag-and-drop support on Windows without requiring users to unblock or run standalone .ps1 script files[cite: 1, 2].
To safely inspect containers and detect audio codecs without parsing errors, the script queries FFprobe in JSON mode[cite: 1]:
$probeJsonRaw = & $ffprobePath -v quiet -show_format -show_streams -print_format json $filePath | Out-String
The script parses both format-level and stream-level metadata blocks, inspecting codec_name to safely skip ALAC .m4a files (preventing FFmpeg MP4 atom stripping) while allowing AAC .m4a files through:
if ($ext -eq '.m4a' -and $codecName -eq 'alac') {
Write-Host "[SKIP] ALAC .m4a skipped (FFmpeg strips MP4 atoms): $fileName" -ForegroundColor Yellow
return
}
Legacy tagging applications (dBpoweramp, LAME, MP3Gain) frequently attach binary APEv2 footers at the end of MP3 files. Because FFmpeg's stream copy passes trailing binary footers through untouched, Name & Tag Fix natively inspects and truncates APEv2 footers via binary stream seeking prior to FFmpeg processing:
function Strip-ApeTag($file) {
$fs = [System.IO.File]::Open($file, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
if ($fs.Length -gt 32) {
$fs.Seek(-32, [System.IO.SeekOrigin]::End)
# Checks for 'APETAGEX' header and truncates footer by tag size
if ($header -eq "APETAGEX") { $fs.SetLength($fs.Length - $tagSize) }
}
$fs.Close()
}
Furthermore, all gain-related tags (including replaygain_*, playgain_*, and rg_*) are dynamically filtered out using regular expressions:
if ($key -match 'gain') { continue }
Name & Tag Fix normalizes inconsistent tag keys across formats into clean standard metadata[cite: 1]:
| Normalized Key | Input Source Aliases | MP3 (ID3v2.3) Handling | FLAC / VorbisComment Handling |
|---|---|---|---|
Title |
title |
Canonical title frame |
Title-Cased Title |
Album Artist |
albumartist, album_artist, albumartistsort |
Canonical album_artist |
Title-Cased Album Artist |
Original Date |
originaldate, originalyear, original_date, tory |
Mapped to ID3 TORY / originalyear |
Title-Cased Original Date |
Media |
media, tmed |
Mapped to ID3 TMED |
Title-Cased Media |
Sort Tags |
artistsort, albumsort, titlesort, composersort |
Canonical artist_sort, album_sort |
Title-Cased Artist Sort, Album Sort |
$ffmpegArgs = @(
"-y", "-v", "error", "-i", $filePath, "-c", "copy", "-map", "0:a",
"-map_metadata", "-1", "-map_metadata:s:a", "-1", "-fflags", "+bitexact"
)
# Zeroes out container & stream metadata explicitly
$tagsToZeroOut = @('encoder', 'creation_time', 'handler_name', 'vendor_id', 'major_brand', 'minor_version')
foreach ($tZero in $tagsToZeroOut) {
$ffmpegArgs += "-metadata", "${tZero}="
$ffmpegArgs += "-metadata:s:a", "${tZero}="
}
-map_metadata -1 & -map_metadata:s:a -1: Completely wipes global and stream-level container headers[cite: 1].-fflags +bitexact: Suppresses writing build timestamp signatures into output files[cite: 1].-map 0:v?: Preserves attached cover art streams for non-Opus containers[cite: 1].-id3v2_version 3: Enforces ID3v2.3 tag writing for MP3 compatibility[cite: 1].To support parallel processing and eliminate race conditions during file renaming, temporary files are tagged with an 8-character unique GUID string[cite: 1]:
$randStr = [guid]::NewGuid().ToString().Substring(0,8)
$tempFile = Join-Path $dir "temp_stripped_${randStr}_$fileName"
Once FFmpeg completes processing, the original file is safely replaced and atomically updated to the target filename[cite: 1].