giovedì 13 gennaio 2022

POWERSHELL - Script di estrazione audio da MKV (con uso di FFMPEG)

Questo script permette di estrarre la traccia audio (in MP3) da un file MKV:
# caricamento e controllo parametri
param([string]$filePath)

# variabili di appoggio
# NOTA: questo va aggiornato con il path effettivo della posizione di FFMPEG (vedi nota sotto nel comando)
$ffmpegPath = "[PATH_DI_FFMPEG]"

try
{
    if (($args.length -eq 0) -and ($filePath.length -eq 0))
    {
        Write-Host "ERRORE: il path del file è obbligatorio." -ForegroundColor Red

        Return
    }
    else
    {
        if ($filePath.length -eq 0)
        {
            if ($args.length -eq 1)
            {
                # stampa dell'help
                if (($args[0] -eq "-h") -or ($args[0] -eq "-help"))
                {
                    Write-Host "DESCRIPTION"
                    Write-Host "    Estrae un file audio in formato MP3 da un file MKV tramite FFMPEG."
                    Write-Host ""
                    Write-Host "PARAMETERS"
                    Write-Host "    -fp, -filePath <String>"
                    Write-Host "        Percorso del file da elaborare."
                    Write-Host ""
                    Write-Host "    -h, -help"
                    Write-Host "        Stampa la guida all'utilizzo."
                }
                else
                {
                    Write-Host "ERRORE: parametro '$($args[0])' senza un valore specificato." -ForegroundColor Red
                }

                Return
            }
            elseif (($args.length -eq 2) -and ($args[0] -ne "-fp"))
            {
                Write-Host "ERRORE: parametro '$($args[0])' non riconosciuto." -ForegroundColor Red

                Return

            }
            elseif (($args.length -eq 2) -and ($args[0] -eq "-fp"))
            {
                $filePath = $args[1]
            }
            else
            {
                Write-Host "ERRORE: troppi parametri specificati." -ForegroundColor Red

                Return
            }
        }

        # controllo presenza di FFMPEG
	$ffmpegExists = Test-Path -Path "$($ffmpegPath)\ffmpeg.exe" -PathType Leaf

        # controllo la correttezza del path passato come parametro e se esiste il file
        $fileExists = Test-Path -Path $filePath -PathType Leaf

        # errore
	if ($ffmpegExists -ne $true)
	{
            Write-Host "ERRORE: FFMPEG non trovato. Controllare il path dell'eseguibile." -ForegroundColor Red

            Return
	}
        elseif ($fileExists -ne $true)
        {
            Write-Host "ERRORE: il file '$($filePath)' non esiste." -ForegroundColor Red

            Return
        }
        else
        {
            # recupero le informazioni sul file (basename + extension)
            $inputFile = Get-ChildItem $filePath

            # controllo l'estensione MKV
            if ($inputFile.Extension -ne "mkv" - $inputFile.Extension -ne ".mkv")
            {
                Write-Host "ERRORE: il file '$($inputFile.Name)' non ha estensione MKV." -ForegroundColor Red
            }
            else
            {
                # conversione del file
                Write-Host "Conversione del file '$($inputFile.Name)' in corso..."

                $cmd = "$($ffmpegPath)\ffmpeg.exe -i ""$($inputFile.Name)"" -ac 2 -ab 192000 -ar 44100 ""$($inputFile.BaseName).mp3"""

                Write-Host "$($cmd)" -ForegroundColor Green

                Invoke-Expression $cmd

                Write-Host ".. conversione del file completata."
            }
        }
    }
}
catch
{
    Write-Host "CATCH: errore generico." -ForegroundColor Red
}

Nessun commento:

Posta un commento