
复制# Copyright 2021 Microsoft # This script will repair a FLAC file that has been corrupted by Media Foundation in reference to KB5003430. # Refer to KB5003430 for further information param( [parameter(Mandatory=$true,竟件教解决
HelpMessage="The path to the FLAC file that has been corrupted by Media Foundation", ValueFromRemainingArguments=$true)] [ValidateScript({ -not [String]::IsNullOrEmpty($_) -and (Test-Path $_) })] [String]$File ) # We need to back up the current file incase we have any errors $FileDirectory = Split-Path -Resolve $File $Filename = Split-Path -Leaf -Resolve $File $FullPath = Join-Path -Resolve $FileDirectory $Filename $Filename = [String]::Format("Backup_{0:yyyyMMdd_hhmmss}_{1}", [DateTime]::Now, $Filename) $BackupLocation = Join-Path $FileDirectory $Filename Write-Output"Microsoft FLAC Repair Tool. This tool will repair a FLAC audio file that was corrupted when editing its details." Write-Output"Affected File: $FullPath" Write-Output"A backup of the file will be made: $BackupLocation" Write-Output"Do you wish to continue?" $choice=$host.ui.PromptForChoice("Fixing FLAC Script", "Do you wish to continue", (&Yes, &No), 1) function ParseStreamInfoMetadataBlock([System.IO.FileStream]$stream) { $blockType = $stream.ReadByte() $lastBlock = ($blockType -shr 7) -ne 0 $blockType = $blockType -band 0x7F if ($blockType -ne 0) { return $false } $blockSize = (($stream.ReadByte() -shl 16) -bor ($stream.ReadByte() -shl 8) -bor $stream.ReadByte()) if ($blockSize -lt 34) { return $false } $minAudioBlockSize = ($stream.ReadByte() -shl 8) -bor $stream.ReadByte() $maxAudioBlockSize = ($stream.ReadByte() -shl 8) -bor $stream.ReadByte() if ($minAudioBlockSize -lt 16 -or $maxAudioBlockSize -lt 16) { return $false } $minFrameSize = (($stream.ReadByte() -shl 16) -bor ($stream.ReadByte() -shl 8) -bor $stream.ReadByte()) $maxFrameSize = (($stream.ReadByte() -shl 16) -bor ($stream.ReadByte() -shl 8) -bor $stream.ReadByte()) $sampleInfo = (($stream.ReadByte() -shl 24) -bor ($stream.ReadByte() -shl 16) -bor ($stream.ReadByte() -shl 8) -bor $stream.ReadByte()) $sampleRate = $sampleInfo -shr 12 $channelCount = (($sampleInfo -shr 9) -band 0x7) + 1 $bitsPerSample = (($sampleInfo -shr 4) -band 0x1F) + 1 [UInt64]$sampleCount = (($stream.ReadByte() -shl 24) -bor ($stream.ReadByte() -shl 16) -bor ($stream.ReadByte() -shl 8) -bor $stream.ReadByte()) $sampleCount = (([UInt64]$sampleInfo -band 0xF) -shl 32) -bor $sampleCount $MD5HashBytes = New-Object byte[] 16 $stream.Read($MD5HashBytes, 0, $MD5HashBytes.Length) $MD5Hash = [Guid]($MD5HashBytes) if ($sampleRate -eq 0) { return $false } # Passing these checks means that we likely have a stream info header and can rebuild the file Write-Output"File Stream Information" Write-Output"Sample Rate: $sampleRate" Write-Output"Audio Channels: $channelCount" Write-Output"Sample Depth: $bitsPerSample" Write-Output"MD5 Audio Sample Hash: $MD5Hash" return $true } if ($choice -eq 0) { Copy-Item $FullPath -Destination $BackupLocation -Force $stream = [System.IO.File]::Open($FullPath, [System.IO.FileMode]::Open) $stream.Seek(4, [System.IO.SeekOrigin]::Begin) while ($stream.ReadByte() -eq 0) {} # We now need to figure outwhere a valid FLAC metadata frame begins # We are likely pointing to the last byte of the size member so well seek back 4 bytes and retry $flacDataStartPosition = $stream.Position - 4 $stream.Seek($flacDataStartPosition, [System.IO.SeekOrigin]::Begin) while (-not(ParseStreamInfoMetadataBlock($stream))) { $flacDataStartPosition = $flacDataStartPosition + 1 $stream.Seek($flacDataStartPosition, [System.IO.SeekOrigin]::Begin) } # Insert the start code $stream.Seek($flacDataStartPosition, [System.IO.SeekOrigin]::Begin) if (Test-Path "$FullPath.tmp") { Remove-Item "$FullPath.tmp" } $fixedStream = [System.IO.File]::Open("$FullPath.tmp", [System.IO.FileMode]::CreateNew) [byte[]]$startCode = [char[]](f, L, a, C); $fixedStream.Write($startCode, 0, $startCode.Length) $stream.CopyTo($fixedStream) $stream.Close() $fixedStream.Close() Move-Item -Force"$FullPath.tmp" $FullPath } 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.156.157.158.159.160.161.162.163.164.165.166.167.168.169.170.171.172.173.174.175.176.177.178.179.180.181.182.183.184.185.