Deep-dive technical documentation covering polyglot execution blocks, flat FFprobe tag extraction, metadata normalization mappings, and race condition prevention.
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.
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
}
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" |
$ffmpegArgs = @("-y", "-v", "error", "-i", $filePath, "-c", "copy", "-map", "0:a", "-map_metadata", "-1", "-fflags", "+bitexact", "-metadata", "encoder=")
-map_metadata -1: Strips out all non-standard, junk, or corrupt global tags before writing normalized tags.-fflags +bitexact: Suppresses writing build timestamp signatures into output files.-metadata encoder=: Clears default encoder strings.-map 0:v?: Preserves embedded cover art video streams for formats other than Opus/Ogg.-id3v2_version 3: Enforces ID3v2.3 tagging specifically for MP3 files.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.