Thursday, April 7, 2011

This Was The Droid I Was Looking For...

I purchased an HTC HD2 ([Leo] TMobile US version) when it first came out last spring.  A Snapdragon gigahertz processor, HTC's UI, a 4.3 inch screen, ...and all the kings horses and all the king's men couldn't keep WindowsMobile 6.x from ruining that phone for me.  WinMo was an ok OS 10 years ago, but hasn't done much for non-corporate-sphere users for quite a while.

And I thought I'd be fine with that until Windows Phone 7 came out and could be flashed onto the phone...that is, until Microsoft announced that the HD2 "didn't have the correct hardware button configuration", or some such.  Even with that news hitting after I purchased the phone,  I knew that bootleg versions would probably be available pretty quick after wp7's release so didn't panic.  And the place to get them would be on this fantastic site xda-developers.com which used to be pretty much a WinMo site, and has grown to embrace most of the phones out (though they favor Android and wp7 now).

So just before SXSW I began the install of a bootloader and an Android OS on the SD card of the phone (no flashing of ROMs, nothing permanent so I could turn back if I needed to).  It was a bit of a risk as I'd need my phone for email, txt'ing, web access, and even calls while at the conference...but I was willing to take the chance.  This Android stuff was looking better and better to me.  The install went fairly smooth (I didn't read the fine print and missed the part about the long initial startup due it being from the SD card, after which its fairly snappy every time).  Since I did that I haven't run WinMo once, and I won't be looking back.  The community and wealth of applications, both good and bad, are astounding and worth it alone to switch.  Take into account the OS is modern, solid, and so well suited to running on a device, and the decision to switch is overwhelmingly pointed towards Android.  If you have an HD2 (or a whole slew of other phones), I'd recommend checking the xda-developers site out, specifically this post to get started.

My phone before...
...and now much improved.

Tuesday, April 5, 2011

Converting From WMA to MP3

I have a fairly decent sized library of music ripped from CD or flat out bought digital.  The main problem is, as I move towards Android on the phone and because I use my phone as a portable media player, a lot of non-MS software just don't like the ol' WMA.  So, I set out to find a solution.

I found a fine tool ffmpeg.exe and a great script to drive it with PowerShell lifted from this site (my thanks go to Paul for creating the script).  However, I of course have one wrinkle in my folder configuration that causes the script to error and not function:  I have a space within the path to my music folder (probably most of us do).  So I began to read about escaping and dereferencing of PowerShell code within a script.  It turns out its not all that easy, but it is a bit of a touchy issue no matter what you're coding in (e.g. at searching a SQL text field and a name like O'Reilly comes up and screws your SQL because an apostrophe is the end of enclosed text).  After a lot of reading it turns out that putting a single ampersand at the beginning of the text fed to the Invoke-Expression call is all it took.  So here is my version of the script:

(The script assumes a default install location for ffmpeg, and needs a change to the path of Get-ChildItem in the For loop per specific machine config.)
# wma2mp3 conversion powershell script... since my >€600 car stereo does not understand wma....
# dec 2010 version 1.1 Servercare, Paul Weterings

$tool = '"C:\Program Files (x86)\WinFF\ffmpeg.exe"'
$succescounter = $failurecounter = 0

# we simply find all the file objects that and with .wma, and iterate through the folders recursively
# note that we are converting everything in the M: drive here, another example might be:
# (Get-ChildItem "f:\music\" -include *.wma -recurse)

foreach ($child in $(Get-ChildItem "C:\Users\UserName\Music\" -include *.wma -recurse))
{
    $wmaname = $child.fullname
    
    # Since we convert the file, lets give it the appropiate extension, note that the function is case-sensitive
    # so we handle that first
    $wmaname = $wmaname.Replace("WMA","wma")
    $mp3name = $wmaname.Replace("wma","mp3")

    # The argument string that tells ffmpeg what to do...
    $arguments = '-i "' + $wmaname + '" -y -acodec libmp3lame -ab 160k -ac 2 -ar 44100 "' + $mp3name + '"'
    echo "-----------------------------------------------------------------------------------------------------"
    echo "----- Processing: $mp3name"
    #echo "$tool $arguments"
    Invoke-Expression "& $tool $arguments"

    # Lets see what we just converted, did everything go OK?
    $mp3file = get-item $mp3name

    # if conversion went well the mp3 file is larger than 0 bytes, so remove the wma file,
    # otherwise leave the wma file & remove the (zero length) mp3 file
    if ($mp3file.Length -gt 0)
    {
        echo "----- removing $wmaname"
        Remove-Item $wmaname
        $succescounter++
    }
    else
    {
        echo "----- removing $mp3name"
        Remove-Item $mp3name        
        $failurecounter++
    }
}

# We are done, so lets inform the user what the succesrate was.
Echo "Processing completed, $succescounter conversions were succesfull and $failurecounter were not."

The magic line that makes it work with a space in the path is:
Invoke-Expression "& $tool $arguments"

This ran on a fairly beefy desktop for over 14 hours to convert about 5.5k files occupying 34GB.  But chug away it did.  There were some WMA's that couldn't be deleted but wasn't an issue to clean up.

Also, thanks to Darshan for his easy-to-follow post on how to install and use SyntaxHighlighter 3.