Convert-AudioWavFile.ps1

function Convert-AudioWavFile 
{
  <#
      .SYNOPSIS
      Converts WAV audio file in audio formats that can be played back by simple MP3 Players such as DFPlayer Mini
 
      .DESCRIPTION
      Uses ffmpeg.exe to convert the audio file format of WAV audio files either to supported WAV files or MP3.
      On Windows, if ffmpeg.exe is not present, it will be downloaded automatically and stored in a temp folder.
      On other operating systems, ffmpeg.exe needs to be manually downloaded (i.e. from https://ffbinaries.com/downloads)
      and placed here: $env:temp\ffmpeg\ffmpeg.exe
 
      .PARAMETER Path
      Path to original WAV audio file
 
      .PARAMETER OutPath
      Path to new audio file to be created
 
      .PARAMETER CreateMp3
      When specified, the new audio file is a mp3 file. Else, it remains a wav file.
 
      .PARAMETER Force
      Overwrites target file when it exists.
 
      .EXAMPLE
      Convert-AudioWavFile -Path c:\test\test.wav -OutPath c:\test\test.mp3 -Force
      Converts wav file to wav encoding that can be played back by DFPLayer Mini. If the target file exists, it will be overwritten.
 
      .EXAMPLE
      Convert-AudioWavFile -Path c:\test\test.wav -OutPath c:\test\new.wav
      Converts wav file to a new wav file using encoding that can be played back by DFPlayer Mini. If the target file exists, an exception is raised.
  #>



  param
  (
    [String]
    [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
    [Alias('FullName')]
    $Path,
    
    [String]
    [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
    $OutPath,
    
    [switch]
    $CreateMp3, 
    
    [switch]
    $Force
  )
  
  begin
  {    
    $executablePath = Assert-FfmpegIsPresent
  }
  process
  {
    $file = Get-Item -Path $Path
    $extension = $file.Extension.ToLower()
    if ($extension -ne '.wav')
    {
      throw "No WAV Audio File: $Path"
    }
    
    if ($CreateMp3)
    {
      $OutPath = [system.io.Path]::ChangeExtension($OutPath, 'mp3')
    }
     
    $exists = Test-Path -LiteralPath $OutPath -PathType Leaf
    if ($exists -and $Force)
    {
      Remove-Item -LiteralPath $OutPath -Force
    }
        
    $exists = Test-Path -LiteralPath $OutPath -PathType Leaf
    if (!$exists)
    {
      if ($CreateMp3)
      {
        
        $null = & $executablePath -n -i $Path -ar 44100 -ac 1 -b:a 128k $OutPath *>&1
      }
      else
      {
        $null = & $executablePath -n -i $Path -c:a adpcm_ima_wav -ar 44100 -ac 1 $OutPath *>&1
      }
    }
    else
    {
      Write-Warning "Target file '$OutPath' exists. Use -Force to overwrite."
    }
      
     
    
  }
}