Remove additional audio track from .MKV files using PowerShell and FFMPEG

Problem:

I have lots of files that have multiple audio tracks and I only want to keep one of the tracks from each file.

Solution:

Run the below script and enter the location of your files and the audio track you would like to keep.  The new files will all be copied to the output destination.

Add-Type -AssemblyName Microsoft.VisualBasic

$Filepath = [Microsoft.VisualBasic.Interaction]::InputBox('Enter folder path', 'File Path')
$audiotrack = [Microsoft.VisualBasic.Interaction]::InputBox('Which track do you want to keep', 'track')
$sources = Get-ChildItem -path "$Filepath" -Recurse -Include *.mkv
$ffpath = "C:\ProgramData\chocolatey\lib\ffmpeg\tools\ffmpeg\bin\ffmpeg.exe"
$acodec = "0:" + $audiotrack

foreach ($source in $sources)
	
{$DirOut = "D:\Temp"
$output = Join-Path $DirOut -ChildPath ((Get-Item $source).basename + ".mkv")

 
& $ffpath -i $source -map 0:0 -map $acodec -acodec copy -vcodec copy $output -loglevel 8 -stats}

Leave a Reply

Your email address will not be published. Required fields are marked *