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:
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:
function ConvertToMp3(
\[switch\] $inputObject,
\[string\] $vlc = 'C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe')
{
PROCESS {
$codec = 'mp3';
$oldFile = $_;
$newFile = $oldFile.FullName.Replace($oldFile.Extension,
".$codec").Replace("'","");
&"$vlc" -I dummy "$oldFile" ":sout=#transcode{acodec=$codec,vcodec=dummy}:standard{access=file,mux=raw,dst=`'$newFile`'}" vlc://quit | out-null;
# delete the original file
Remove-Item $oldFile;
}
}
function ConvertAllToMp3(\[string\] $sourcePath) {
Get-ChildItem "$sourcePath\\*" -recurse -include *.m4a | ConvertToMp3;
}
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:
function DuplicateId3Tags
{
param (
[string]$sourceM4aTrack,
[string]$targetMp3Track
)
# load sharp library for working with tags
[Reflection.Assembly]::LoadFrom("C:\\Users\\andrew.RIVERCITY\\Documents\\Music Workshop\\taglib-sharp.dll")
#load files
$sourceM4a = [TagLib.File]::Create($sourceM4aTrack);
$targetMp3 = [TagLib.File]::Create($targetMp3Track);
# copy tags
[TagLib.Tag]::Duplicate($sourceM4a.Tag, $targetMp3.Tag, $true);
$targetMp3.Save();
}
And added a call to the function right before I delete the original file and voila… 100% MP3 again!
# update ID3 tags on target file
DuplicateId3Tags "$oldFile" "$newFile";