Find mp3 files with low Bit Rate

Here’s a function to find all folders containing mp3 files (listed if ‘-details’ switch is used) with a bit rate under the threshold (128kbps by default)

function Get-LowMp3([string]$folder=".",[int]$minBitrate=128,[switch]$details=$false) {
$shellObject=New-Object -ComObject Shell.Application
$bitrateAttrib=0

$mp3s=Get-ChildItem $folder -Recurse -Filter '*.mp3'
ForEach($mp3 in $mp3s) {
    # Get a shell object to retrieve file metadata.
    $directoryObject=$shellObject.NameSpace($mp3.Directory.FullName)
    $fileObject=$directoryObject.ParseName($mp3.Name)

    # Find the index of the bit rate attribute.
    For($index=5; -Not $bitrateAttrib;++$index) {
        $name=$directoryObject.GetDetailsOf($directoryObject.Items,$index)
        if($name -eq 'Bit rate') { $bitrateAttrib=$index }
    }

    # Get the bit rate of the file.
    $bitrateString=$directoryObject.GetDetailsOf($fileObject,$bitrateAttrib)
    if($bitrateString -match '\d+') { [int]$bitrate=$matches[0] }
    else { $bitrate=-1 }

    # Include the file in the results if it has the desired bit rate.
    if($bitrate -le $minBitrate) { 
        if ($details) { $mp3 } else { $directoryObject }
        }
    }
}

Leave a Reply

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

Scroll to Top