Skip to content

Instantly share code, notes, and snippets.

@saschanaz
Last active September 29, 2018 06:24
Show Gist options
  • Save saschanaz/9f168b7f1971f2e11800 to your computer and use it in GitHub Desktop.
Save saschanaz/9f168b7f1971f2e11800 to your computer and use it in GitHub Desktop.
Convert FLAC to WMA Lossless
# Partly from http://kneppscript.wordpress.com/2011/03/25/encoding-wma-files-using-powershell-and-microsoft-expression-encoder/
# Depends on FLAC for Windows from Xiph; Expression Encoder 4 SP1 from Microsoft
if ([Environment]::Is64BitProcess)
{
write-warning 'Launching x86 PowerShell'
&"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -executionpolicy remotesigned -noninteractive -noprofile -file $myinvocation.Mycommand.path
exit
}
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Expression.Encoder") | Out-Null
$decoder = ".\flac.exe"
$encoderPreset = [Microsoft.Expression.Encoder.Preset]::FromFile(".\wmalpreset.xml")
Function Generate-WAV ($flacfile) {
$output = $(GetDisplayName($flacfile)) + ".wav"
&($decoder) -d $flacfile | Wait-Process;
Return $output
}
Function Generate-WMAL ($wavfiles) {
$job = New-Object Microsoft.Expression.Encoder.Job
$wavfiles | Foreach-Object {
$mediaItem = New-Object Microsoft.Expression.Encoder.MediaItem($_)
$job.MediaItems.Add($mediaItem)
}
$job.CreateSubfolder = $false
$job.OutputDirectory = Resolve-Path(".\")
$job.ApplyPreset($encoderPreset)
$onProgress_EventHandler = [ScriptBlock]{
param ( [PSObject]$Sender,
[Microsoft.Expression.Encoder.EncodeProgressEventArgs]$e
)
$activity = "Encoding '{0}'" -f $e.CurrentItem.ActualOutputFileName
$status = "{0}% Pass {1} of {2}" -f [int]$e.Progress, $e.CurrentPass, $e.TotalPasses
Write-Progress -Activity $activity -Status $status -PercentComplete ([int]($e.Progress))
}
# Register onProgress Event Handler with Encoder Job object
$job.add_EncodeProgress($onProgress_EventHandler)
# Start Encoding Job
$job.Encode()
}
Function GetDisplayName ($file) {
Return $file.Substring(0, $file.LastIndexOf('.'))
}
Function Convert ($flacfile) {
Generate-WMAL(Generate-WAV($flacfile))
}
$wavfiles = Get-Item *.flac | Foreach-Object { Generate-WAV($_.name) }
Generate-WMAL($wavfiles)
<?xml version="1.0" encoding="utf-16"?>
<!--Created with Expression Encoder version 4.0.4276.0-->
<Preset
Version="4.0">
<Job />
<MediaFile>
<OutputFormat>
<WindowsMediaOutputFormat>
<AudioProfile>
<WmaAudioProfile
Codec="WmaLossless"
Channels="2"
BitsPerSample="16"
SamplesPerSecond="44100">
<Bitrate>
<VariableQualityBitrate
Quality="100" />
</Bitrate>
</WmaAudioProfile>
</AudioProfile>
</WindowsMediaOutputFormat>
</OutputFormat>
</MediaFile>
</Preset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment