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

cleanup

Over the past couple of years I travel quite a lot.  As such, I tend to load up on 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 }
  }
}
Advertisements

Author: shauncassells

Shaun Cassells, Senior Solutions Engineer, 1E Shaun is a Microsoft MVP Windows Insider MVP and executive at Central Texas Systems Management User Group (CTSMUG). Shaun is a frequent speaker at conferences like MMS, Gartner, TechEd Europe, TechEd North America, IT Dev Connections, System Center Rallies, and Systems Management User Groups (SMUGs). Shaun is an award winning blogger syndicated from www.shauncassells.com. Shaun specializes in analysis, optimization, and design of Tactical Solutions to Strategic Business Goals. Prior to joining 1E in 2010, he worked for a Global 100 company as the Configuration Manager Service Owner and Architect. Recently as a Principle Consultant at 1E, he accomplished design, review, and improvements to a variety of Configuration Manager environments from the very small to sites with a half million seats. He provided leadership that created a patented automated application rationalization and usage based OSD mapping solution in use by multiple fortune 500 companies. These days you can find Shaun traveling to help discover, review, and improve business productivity across the world.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s