Music Library Cleaning with PowerShell – Identifying Old MP3 Files with Low Bitrates

Over the past couple of years I travel quite a lot.  As such, I tend to load up on CDs in digital format. However, some of my older files have some nasty names or bad quality.

This has led to me to start grooming my 15,000+ song catalog.  That number does not include another 15,000+ audio book files.  I love to listen to books while running.

As I was going through the folders of CD rips, iTunes purchases, and other sundry file droppings… I was feeling a bit overwhelmed.

Trying to scope the issue I went for the following categories

  • ITunes Music
  • Files below a certain Bitrate Threshold
  • Artists I did not enjoy
  • CD Tagging

Today I am going to talk about how I used PowerShell to identify files below a certain bitrate threshold.   Example, I want to get all MP3 files with a bit rate less than or equal to 100.  Then I want to delete them… as they sound even worse on digital players (like the iPhone)

  1. Run the PowerShell Function Get-Mp3FilesLessThan with up to three inputs
    1. Inputs
      1. Directory
      2. BitRate
      3. File Extension
    2. Example
      1. Get-Mp3FilesLessThan \\Server\Music 100 *.mp3 | Format-Wide
    3. Output
      1. List of all files where criteria are matched
  2. Delete result site
    1. Get-Mp3FilesLessThan \\Server\Music100 *.mp3 | Remove-Item -WhatIf
      1. I like using –WhatIf because Remove-Item by default does not show results
      2. Remove the –WhatIf to commit the change

Enjoy!

function Get-Mp3FilesLessThan( [string]$directory = "$pwd", [int]$minimumBitrate = 32, [string]$FileTypeExt = "*.mp3" ) {
  $shellObject = New-Object -ComObject Shell.Application
  $bitrateAttribute = 0

  # Find all mp3 files under the given directory
  $mp3Files = Get-ChildItem $directory -recurse -filter $FileTypeExt
  foreach( $file in $mp3Files ) {
    # Get a shell object to retrieve file metadata.
    $directoryObject = $shellObject.NameSpace( $file.Directory.FullName )
    $fileObject = $directoryObject.ParseName( $file.Name )

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

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

    # If the file has less than or equal to the desired bit rate, include it in the results.
    if( $bitrate -le $minimumBitrate ) { $file }
  }
}
email

Written by , Posted .