Assert-FfprobeIsPresent.ps1

function Assert-FfprobeIsPresent
{
  <#
      .SYNOPSIS
      Returns the path to the ffprobe.exe executable. If the executable is not present, it will be automatically downloaded (on Windows), or a message appears with instructions on how to install it (on other OS).
 
      .DESCRIPTION
      ffprobe.exe is a free open-source tool to analyze the format and encoding of audio file content.
 
      .PARAMETER ExecutablePath
      Optional. When specified, the executable is expected at this path, and when missing, will be downloaded to this path.
      When not specified, the default location is $env:temp\ffprobe\ffprobe.exe
 
      .PARAMETER Force
      Downloads the tool even if it exists already (i.e. to ensure the latest version)
 
      .EXAMPLE
      $path = Assert-FfprobeIsPresent
      returns the path to ffprobe.exe so it can then be called.
  #>



  [CmdletBinding()]
  param
  (
    [String]
    $ExecutablePath = (Join-Path $env:temp -ChildPath ffprobe | Join-Path -ChildPath ffprobe.exe),
    
    [switch]
    $Force
  )
  
  $exists = Test-Path -Path $ExecutablePath
  if ($exists -and (!$Force)) { return $ExecutablePath }
  
  $windows = Test-IsWindows
  if ($windows)
  {

    $url = 'https://github.com/ffbinaries/ffbinaries-prebuilt/releases/download/v6.1/ffprobe-6.1-win-64.zip'
    $archive = Join-Path -Path $env:temp -ChildPath ffprobe.zip
    $archiveUnpacked = $ExecutablePath | Split-Path 
    Write-Host "ffprobe.exe not present, downloading (53KB)..."
    Invoke-WebRequest -Uri $url -OutFile $archive -UseBasicParsing
    Unblock-File -Path $archive
    Write-Host "Unpacking downloaded archive..." -NoNewline
    Expand-Archive -Path $archive -DestinationPath $archiveUnpacked -Force
    Remove-Item -Path $archive 
    $exists = Test-Path -Path $ExecutablePath
    if ($exists)
    {
      Write-Host "success."
    }
    else
    {
      Write-Host "failed: unable to write to $ExecutablePath"
    }
  }
  
  if ($exists)
  {
    return $ExecutablePath
  }
  else
  {
    Write-Host @'
You need to manually download "ffprobe.exe" from here: https://ffbinaries.com/downloads
After you have downloaded the archive, unblock and unpack it, and move ffprobe.exe (not ffmpeg.exe!) to this location:
'@

    Write-Host $ExecutablePath -ForegroundColor Black -BackgroundColor White
    throw "ffprobe.exe not found: $ExecutablePath does not exist."
  }
  
}