Check language in Media Files

I have a largish media collection (mainly ripped DVDs) but noticed that somehow, I had managed to rip foreign audio instead of English. A little script can help identify these easily with the help of the PowerShell module “Get-MediaInfo” (The script will install this for you providing the first run is from an elevated PowerShell window)

The PowerShell script below will prompt you for the folder containing your video files, and will recurse through them.

It will then prompt you for the language you want to check (defaults to English) and will parse through them. If it detects any non-English versions, it will display these at the end of the script, with the option to save a CSV of all parsed files with info such as path, language, codec, audio format etc.

Please note, it will skip files where no language is detected! if you think you have these, check the CSV output, the language column will be blank.

#This script requires the powershell module "Get-mediainfo"
# 
# Lets check for it and install if we can...
if (Get-Module -ListAvailable -Name Get-mediainfo) {
    Write-Host "Get-mediainfo Module exists, loading" -ForegroundColor Green
	Import-Module Get-mediainfo 
	} 
else {
    #no module, does user have admin rights?
    Write-Host "Get-mediainfo Module does not exist please install`r`n with install-module Get-mediainfo" -ForegroundColor Red
	
		if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
		[Security.Principal.WindowsBuiltInRole] "Administrator")) {
			Write-Host "Insufficient permissions to install module. Please run as an administrator and try again." -ForegroundColor DarkYellow
            return(0)
		    }
		else {
		    Write-Host "Attempting to install Get-mediainfo module" -ForegroundColor Cyan
		    Install-Module Get-mediainfo -Confirm:$False -Force
        }
	
}
Add-Type -AssemblyName System.Windows.Forms #required for file dialog
Function Get-FileName($initialDirectory)
{   
  [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
  Out-Null
    $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
  $SaveFileDialog.initialDirectory = $initialDirectory
  $SaveFileDialog.filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"
  $SaveFileDialog.ShowDialog() | Out-Null
  $SaveFileDialog.filename
}
clear-host
# Lets define some filettypes we dont care about
$ignore = "*.jpg,*.png,*.db"
# prompt for a path...
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
    RootFolder            = "MyComputer"
    Description           = "$Env:ComputerName - Select a folder"
}
$Null = $FolderBrowser.ShowDialog()
$path = $FolderBrowser.SelectedPath
$start = Get-Date
[nullable[double]]$secondsRemaining = $null
$languagetocheck = Read-host "Please enter the langauge you wish to validate (default = English)"
if($languagetocheck.lentgh -le 3){$languagetocheck = "English"}
$items = Get-ChildItem $path -Recurse -exclude $ignore
$baditems = @()
$allfiles = @()
$progress = 1

foreach($item in $items)
{
    $secondsElapsed = (Get-Date) – $start
    $percentComplete  = [math]::Round((100/$items.count) * $progress,2)
    $progressParameters = @{
        Activity = "Working on files [$($progress)/$($items.Count)] $($secondsElapsed.ToString('hh\:mm\:ss'))"
        Status = 'Processing'
        CurrentOperation = "Checking " + $item.Name
        PercentComplete = $percentComplete
    }
        if ($secondsRemaining) {
        $progressParameters.SecondsRemaining = $secondsRemaining
    }
                
    Write-Progress  @progressParameters
    $tempitem = @{}
    try{
        #check its not a directory first!
        if ($item.VersionInfo.FileName -ne $null){
            
            $result = Get-MediaInfoValue $item.VersionInfo.FileName -Kind Audio -Index 1 -Parameter 'Language/String' 
        }
    }
    catch
    {
        write-host an error occored on $item.VersionInfo.FileName

    }
    if($result -ne $languagetocheck -and $result -ne $null -and $result.length -ge 3)
    {
        write-host "Foreign langauage detectedon " $item.name " Language detected is " $result -ForegroundColor red
        $fullinfo = Get-MediaInfo $item.FullName
        $tempitem.Name = $item.FullName
        $tempitem.langauage = $result
        $tempitem.audio = $fullinfo.format
        $tempitem.framerate = $fullinfo.framerate
        $tempitem.extension = $fullinfo.ext
        $tempitem.audiocodec = $fullinfo.AudioCodec
        $baditems += New-Object psobject -Property $tempitem
        $allfiles += New-Object psobject -Property $tempitem
    }else
    {
        $fullinfo = Get-MediaInfo $item.FullName
        $tempitem.Name = $item.FullName
        $tempitem.langauage = $result
        $tempitem.audio = $fullinfo.format
        $tempitem.framerate = $fullinfo.framerate
        $tempitem.extension = $fullinfo.ext
        $tempitem.audiocodec = $fullinfo.AudioCodec
        $allfiles += New-Object psobject -Property $tempitem
    }
    $progress++
    
    $secondsRemaining = ($secondsElapsed.TotalSeconds / $progress) * ($items.Count – $progress)

}
$progressParameters = @{
        Activity = "Working on files [$($progress)/$($items.Count)] $($secondsElapsed.ToString('hh\:mm\:ss'))"
        Status = 'Ready'
        Completed = $true
    }
    Write-Progress  @progressParameters
if($baditems.count -eq 0){
    Write-host "No Foreign Languages detected" -ForegroundColor Green
}
$baditems | FT
Write-host "Save a CSV of all items? (Y/N)" -ForegroundColor Cyan
$answer = ""
while("y","n" -notcontains $answer)
{
    $answer = read-host
} 
switch ($answer) {
 "y" {
 $outfile = Get-Filename -initialdirectory $env:UserProfile\Desktop

    if("" -eq $outfile){
        Write-host "No File selected, aborting" -ForegroundColor red
        start-sleep -s 5
        exit
    }
   $allfiles | export-csv $outfile -NoTypeInformation

Write-host "
 _______ _____ __   _ _____ _______ _     _ _______ ______ 
 |______   |   | \  |   |   |______ |_____| |______ |     \
 |       __|__ |  \_| __|__ ______| |     | |______ |_____/
                                                           
" -ForegroundColor Green
 
 }
 "n" {
    Write-host "All done, goodbye!" -ForegroundColor Green
    }
}


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.