Wednesday, June 17, 2015

PowerShell Script to Number mp3 Files

I downloaded a free audiobook that came in 43 separate mp3 files. The problem was that the first file "preface" was not included in the track numbering which left the files out of order.

Below is a quick powershell script to update their track numbers. This script relies on taglib-sharp:

$taglib = (Resolve-Path .\taglib-sharp.dll).Path
[system.reflection.assembly]::loadfile($tablib)
Get-ChildItem . | % { 
    @{id= ($_.name -split "-")[2]; name=$_.fullname } } | % {
  $media = [taglib.file]::create($_.name)
  $media.tag.Track = $_.id; $media.tag.TrackCount = 43;
  $media.save()
} 
 
As a bonus if you need to rename an entire directory of files, say to remove a prefix from a file name:

Get-ChildItem -filter 2011*.* | % {
    Rename-Item $_.Name $($_.Name -replace '2011','') }

No comments: