Microsoft MVP Logo

Last weekend I was poking around my music collection on my server and noticed a bunch of artists and albums not showing up in my library (I use Zune to listen to my music and copy it to my phone). Diving in I noticed one folder was full of M4A files... arg! Damn iTunes format... I don't want Apple or Microsoft format, I want standard plain old MP3! Using the following PowerShell command, I found there were a ton of these types of files on my server:

   1: Get-ChildItem "\RIVERCITY-NAS1\Music" -recurse -include *.m4a

Yikes... over 800 files! OK, that's enough to look for a way to automate this conversion process. After a bit of research, I found the freely available VLC player had the ability to convert these types of files... for free. Sweet! So after a little work, I got this PowerShell script working:

   1: function ConvertToMp3(
   2:     [switch] $inputObject, 
   3:     [string] $vlc = 'C:\Program Files (x86)\VideoLAN\VLC\vlc.exe')
   4: {
   5:   PROCESS {
   6:     $codec = 'mp3';
   7:     $oldFile = $_;
   8:     $newFile = $oldFile.FullName.Replace($oldFile.Extension, 
   9: ".$codec").Replace("'","");
  10:     
  11:     &"$vlc" -I dummy "$oldFile" ":sout=#transcode{acodec=$codec,
  12: vcodec=dummy}:standard{access=file,mux=raw,dst=`'$newFile`'}" vlc://quit | out-null;
  13:     
  14:     # delete the original file
  15:     Remove-Item $oldFile;
  16:   }
  17: }
  18:  
  19: function ConvertAllToMp3([string] $sourcePath) {    
  20:   Get-ChildItem "$sourcePath\*" -recurse -include *.m4a | ConvertToMp3;
  21: }
  22:  
  23: ConvertAllToMp3 '\\RIVERCITY-NAS1\Music';

Sweet... until I noticed all the MP3 files didn't have any ID3 tags. So I needed to add a step. I found an old copy of the TagLib# library. that would read the tags from one file and copy them to another. So I added the following function to the script:

   1: function DuplicateId3Tags
   2: {    
   3:     param (
   4:     [string]$sourceM4aTrack,
   5:     [string]$targetMp3Track
   6:   )
   7:  
   8:   # load sharp library for working with tags
   9:     [Reflection.Assembly]::LoadFrom("C:\Users\andrew.RIVERCITY\Documents\Music Workshop\taglib-sharp.dll")      
  10:   
  11:   #load files
  12:   $sourceM4a = [TagLib.File]::Create($sourceM4aTrack);
  13:   $targetMp3 = [TagLib.File]::Create($targetMp3Track);
  14:   
  15:   # copy tags
  16:   [TagLib.Tag]::Duplicate($sourceM4a.Tag, $targetMp3.Tag, $true);
  17:   
  18:   $targetMp3.Save();
  19: }

And added a call to the function right before I delete the original file and voila... 100% MP3 again!

   1: # update ID3 tags on target file
   2: DuplicateId3Tags "$oldFile" "$newFile";
Comments powered by Disqus