Getting MKV Stream Data Information with PowerShell and the MKVToolnix Toolkit

I often want to get information about an MKV file, usually to find out if it has one or more subtitle tracks. MKVToolNix is my toolset of choice for this. Automation of this process turned out to be relatively straight forward with PowerShell (naturally!) and one of their tools, mkvinfo.

Before we go into the cmdlet details, you will need to download and install the MKVToolNix toolset if you do not already have it already You can get this by visiting the site of the author, Moritz Bunkus, at https://www.bunkus.org/videotools/mkvtoolnix/

A word of warning. We’re using ‘Prayer Based Parsing’. If a future revision of mkvinfo changes the format of output, there’s a good chance our script will cease to work. I’m pretty certain more RegEx aware gurus will be able to tighten the parsing a bit to lessen the chance of this, but it’s still something to think about.

Looking at the code, ‘FullName’ is defined as an alias for Path in the cmdlet, to allow the use of pipeline output from cmdlets such as Get-ChildItem. That way, track information from multiple files can be obtained quite simply.

Also remember to change the path in the code below to where your mkvinfo.exe file exists.

Once you’ve loaded the function into memory, it can be used simply the following way :

PS C:\temp> Get-MKVTrackInfo -Path C:\temp\movie.mkv

An example, also showing how we can combine it with Get-ChildItem is below.

Get-MKVInfoChildItemIn the next post, we’ll make use of another MKVToolnix tool and PowerShell to allow us to extract subtitle files from MKV files.

Any feedback, comments, errata always welcome. 🙂

#requires -Version 2

function Get-MKVTrackInfo
{
    [CmdletBinding()]
    
    Param
    (
        [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)][Alias('FullName')] [string] $Path
    )
    
    Process
    {
        $info = & 'C:\Program Files\MKVToolNix\mkvinfo.exe' $Path  | Where-Object -FilterScript {
            ($_ -like '*Track number*') -or ($_ -like '*Track type*')
        }
        $info = $info | ForEach-Object -Process {
            $_.replace('|  + ','')
        }
        $info = $info | ForEach-Object -Process {
            $_.replace('(track ID for mkvmerge & mkvextract: ',':')
        } 
        $info = $info | ForEach-Object -Process {
            $_.replace(')','')
        }
        $info = $info | ForEach-Object -Process {
            $_.replace(': ',':')
        }
        $info = $info | ForEach-Object -Process {
            $_.replace(' :',':')
        }
        $info = $info | ForEach-Object -Process {
            $_.replace('Track number:','')
        }
        $info = $info | ForEach-Object -Process {
            $_.replace('Track type:','')
        }
        For ($index = 0;$index -lt $info.count;$index = $index + 2) 
        {
            $tmpArray = $info[$index].Split(':')

            $hash = @{
                Track     = $tmpArray[1]
                TrackType = $info[$index+1]
                Path      = (Get-ChildItem -Path $Path).FullName
            }
        
            New-Object -TypeName PsObject -Property $hash
        }
    }
}
Advertisements

2 thoughts on “Getting MKV Stream Data Information with PowerShell and the MKVToolnix Toolkit

  1. Pingback: Extracting Subtitles from MKV Files | PowerShellize

  2. Pingback: Extracting Subtitles from MKV Files | powershell.amsterdam

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s