Skip to content

Instantly share code, notes, and snippets.

@mikkohei13
Created February 26, 2025 15:10
Show Gist options
  • Save mikkohei13/4314677a039bed402537d0991e7de74a to your computer and use it in GitHub Desktop.
Save mikkohei13/4314677a039bed402537d0991e7de74a to your computer and use it in GitHub Desktop.
PowerShell script that converts all wav files in a folder (and subfolders) into flac format.
# Check if FFmpeg is installed
try {
ffmpeg -version | Out-Null
} catch {
Write-Error "FFmpeg is not installed or not in PATH. Please install FFmpeg first."
exit 1
}
# Allow user to specify the root directory or use current directory as default
$rootDirectory = Read-Host "Enter the root directory path (press Enter to use current directory)"
if ([string]::IsNullOrWhiteSpace($rootDirectory)) {
$rootDirectory = Get-Location
}
# Verify directory exists
if (-not (Test-Path $rootDirectory)) {
Write-Error "Directory not found: $rootDirectory"
exit 1
}
# Find all .wav files in all subdirectories
$wavFiles = Get-ChildItem -Path $rootDirectory -Filter *.wav -Recurse -File
$totalFiles = $wavFiles.Count
if ($totalFiles -eq 0) {
Write-Host "No WAV files found in $rootDirectory"
exit 0
}
Write-Host "Found $totalFiles WAV files to convert"
$currentFile = 0
foreach ($wav in $wavFiles) {
$currentFile++
# Define the output file with the same name but .flac extension
$flacFile = $wav.FullName -replace "\.wav$", ".flac"
Write-Progress -Activity "Converting WAV to FLAC" -Status "Processing $($wav.Name)" -PercentComplete (($currentFile / $totalFiles) * 100)
try {
# Convert using FFmpeg (with reduced output verbosity)
$ffmpegCommand = "ffmpeg -i `"$($wav.FullName)`" `"$flacFile`" -y -loglevel error"
Invoke-Expression $ffmpegCommand
if (Test-Path $flacFile) {
Write-Host "Successfully converted: $($wav.Name)"
# Uncomment the following line to delete original WAV files
Remove-Item $wav.FullName -Force
} else {
Write-Warning "Failed to convert: $($wav.Name)"
}
} catch {
Write-Error "Error converting $($wav.Name): $_"
}
}
Write-Progress -Activity "Converting WAV to FLAC" -Completed
Write-Host "Conversion complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment