Skip to content

Instantly share code, notes, and snippets.

@mistic100
Created August 25, 2026 16:11
Show Gist options
  • Select an option

  • Save mistic100/182ff5642c2e106265b93a0112be9a07 to your computer and use it in GitHub Desktop.

Select an option

Save mistic100/182ff5642c2e106265b93a0112be9a07 to your computer and use it in GitHub Desktop.
Re-encode every FLAC above 16 bits or 44100 hz
param(
[string]$Path = "."
)
Get-ChildItem -Path $Path -Recurse -Filter "*.flac" | ForEach-Object {
$file = $_.FullName
$sampleRate = [int](ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 "$file")
$bitsStr = ffprobe -v error -select_streams a:0 -show_entries stream=bits_per_raw_sample -of default=noprint_wrappers=1:nokey=1 "$file"
if ([string]::IsNullOrWhiteSpace($bitsStr) -or $bitsStr -eq "N/A") {
$bitsStr = ffprobe -v error -select_streams a:0 -show_entries stream=bits_per_sample -of default=noprint_wrappers=1:nokey=1 "$file"
}
$bits = [int]$bitsStr
if ($sampleRate -gt 44100 -or $bits -gt 16) {
Write-Host "Re-encoding: $file ($sampleRate Hz, ${bits}-bit)"
$tmpFile = [System.IO.Path]::ChangeExtension($file, "tmp.flac")
ffmpeg -v error -i "$file" -af "aformat=sample_fmts=s16:sample_rates=44100" -map 0 -c:a flac -c:v copy "$tmpFile"
if ($LASTEXITCODE -eq 0) {
Move-Item -Path $tmpFile -Destination $file -Force
} else {
Write-Error "Error processing $file"
Remove-Item -Path $tmpFile -ErrorAction SilentlyContinue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment