Tuesday, August 29, 2006

Shrinking MP3's for Use on a Mobile MP3 Player - MP3Shrink.vbs

I recently purchased a MobiBlu DAH-1500i MP3 player. It is a pretty nice little unit (and I do mean little.) It is the smallest MP3 player on the market today.

The sound quality is great, the battery life is passable at around 6-8 hours, and the display is top notch. Having a built in FM tuner is really the selling point for me on this little device. It is nice to be able to use it at the gym to tune the different TV stations when I’m running or biking or using some of the other cardio equipment.

The only part about this little device that can be a bit of a hassle is that it only has 1 GB of storage space. This can make for a more limited selection of music that can be stored on this thing at one time.

The thought occurred to me that it would be nice to just down-sample the music using LAME before I copied it over since I don’t need stellar quality, just something passable. I could strip the stereo and just output the stream to a mono channel and then use LAME’s VBR encoding at it’s highest compression so that I could get the smallest possible file while still having quality good enough to listen to.

This was all pretty easy but is a tedious process if I did it on every file I wanted to copy to my MobiBlu. So, as you’d expect. I wrote a script to streamline the process.

All you have to do with this particular script is drag a file, or folder onto the “.vbs” file and it will do the conversion for you. You just have to adjust some presets that are defined in the options section of the script. Then after the conversion it automatically copies the file, or folder to the mobile device. When it’s finished converting it displays a status screen showing the folder it copied the files you created to, how many files and folders it created, and how long the whole process took.

Make sure to download LAME and put this script is in the same directory as the lame executable.

Note this will work with any MP3 files. You don't have to have a MobiBlue to use this script!

'MP3Shrink.vbs 1.0
'Drag and drop your MP3s or a folder of MP3's onto this vbs file and
'it will automatically downsample (shrink) and copy those files to
'your MP3 device.
'
'You'll need the LAME executable in the same directory as this vbs
'file in order for the script to work properly.
'
'Be sure to change the sDestination to the location of your MP3 device
'and any other options in the options section below.
'
'This script is provided under the Creative Commons license located
'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
'be used for commercial purposes with out the expressed written consent
'of NateRice.com


'----------------OPTIONS-------------------
'This is the location of your MP3 device
sDestination = "C:\Temp\"

'Singles directory. If you drag and drop
'single files, this is the default
'directory that they are copied to.

sSinglesDirectory = "Singles\"

'You can choose if you want your file to
'remain stereo or if you'd like to convert
'it to mono in order to save space
bStereo = False

'Select the ammount of compression you'd
'like. 0=Better quality, Bigger files.
'9=Worse quality, smaller files.
sQuality = "9"
'-----------------------------------------

'Display Warning
Set WshShell = WScript.CreateObject("WScript.Shell")
vContinueYN = WshShell.Popup("You are about to convert files!" & vbLF & _
              "Destination: " & sDestination, 0, "Attention!", 1)

If Not vContinueYN = 2 Then

'Find script directory
aScriptFilename = Split(Wscript.ScriptFullName, "\")
sScriptFilename = aScriptFileName(Ubound(aScriptFilename))
sWorkingDirectory = Replace(Wscript.ScriptFullName, sScriptFilename, "")

Dim oFSO
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

'------------------Stats------------------
vTotalFilesConverted = 0
vTotalFoldersCreated = 0
vStartTime = date & " " & time
'-----------------------------------------

For Each sArg In wscript.arguments
  'Check to see if the file passed is a file or a folder
  If oFSO.FolderExists(sArg) Then
    'get folder name by parsing argument (folder path)
    aFolderList = Split(sArg, "\")
    sFolder = aFolderList(Ubound(aFolderList))
    'getting root folder by truncating off the working folder name
    sRootFolder = left(sArg, len(sArg)-len(sFolder))

    RecursiveFolderCheck(sArg)
  Else
    aFilename = Split(sArg, "\")
    sFileName = aFilename(Ubound(aFilename))
    sEncodedFileDest = sDestination & sSinglesDirectory & sFileName

    If Not oFSO.FolderExists(sDestination & sSinglesDirectory) Then
      oFSO.CreateFolder(sDestination & sSinglesDirectory)
      '--------Stats------
      vTotalFoldersCreated = vTotalFoldersCreated + 1
      '-------------------
    End If

    If UCase(Right(sFileName, 3)) = "MP3" or _
    UCase(Right(sFileName, 3)) = "WAV" Then   
      vSuccess = LAMEShrink(sQuality, bStereo, sArg, sEncodedFileDest)
    End If
  End If

  'sArguments = sArguments & sArg & vbLF
Next

wscript.echo "Conversion Complete!" & vbLF & _
vbLF & _
sDestination & vbLF & _
vbLF & _
"Files Converted: " & vTotalFilesConverted & vbLF & _
"Folders Created: " & vTotalFoldersCreated & vbLF & _
"Time Taken: " & ReadableTime(datediff("S", vStartTime, _
date & " " & time)) & vbLF & _
"Time Per File: " & ReadableTime(Round(datediff("S", vStartTime, _
date & " " & time)/vTotalFilesConverted))


End IF

Function LAMEShrink(sQuality, bStereo, sSourceFile, sDestination)
  Dim oShell
  Set oShell = WScript.CreateObject("Wscript.Shell")

  If Not bStereo Then
    sMono = "-m m "
  End If

  oShell.Run """" & sWorkingDirectory & "lame.exe"" " & sMono & "-V " & _
  sQuality & " -h --silent """ & sSourceFile & """ """ & sDestination & """", 0, True
  LAMEShrink = True

  '---------Stats---------
  vTotalFilesConverted = vTotalFilesConverted + 1
  '-----------------------
End Function

Function RecursiveFolderCheck(sFolder)
  Dim oFSO, oFolder
  Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
  Set oFolder = oFSO.GetFolder(sFolder)

  '--------Create folder on destination--------
  sDestinationWorkingFolder = sDestination & Replace(sFolder, sRootFolder, "")
  If Not oFSO.FolderExists(sDestinationWorkingFolder) Then
    oFSO.CreateFolder(sDestinationWorkingFolder)
   
    '--------Stats------
    vTotalFoldersCreated = vTotalFoldersCreated + 1
    '-------------------
  End If
  '--------------------------------------------

  '-Convert the files in the current directory-
  For Each fFile in oFolder.Files
    aFilename = Split(fFile, "\")
    sFileName = aFilename(Ubound(aFilename))
    sEncodedFileDest = sDestinationWorkingFolder & "\" & sFileName

    If UCase(Right(sFileName, 3)) = "MP3" or UCase(Right(sFileName, 3)) = "WAV" Then
      vSuccess = LAMEShrink(sQuality, bStereo, fFile, sEncodedFileDest)
    End If
  Next
  '--------------------------------------------

  '--------------Recurse Folders---------------
  Set oSubFolders = oFolder.SubFolders
  For Each sSubFolder In oSubFolders
    RecursiveFolderCheck(sSubFolder)
  Next
  '--------------------------------------------
End Function

Function ReadableTime(vSeconds)
  'This function creates readable time in Years:Days:Hours:Minutes:Seconds
  'From the a value (vSeconds) passed in seconds.
  '
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com


  If Not IsNumeric(vSeconds) Then
    ReadableTime = -1
    Exit Function
  End If

  vScale = "seconds"

  If vSeconds >= 60 Then
    vSeconds1 = vSeconds Mod 60
    aMinutes = Split((vSeconds / 60), ".")
    vMinutes = aMinutes(0)
    vSeconds = "." & vSeconds1
    vScale = "mm.ss"
  End If

  If vMinutes >= 60 Then
    vMinutes1 = vMinutes Mod 60
    aHours = Split((vMinutes / 60), ".")
    vHours = aHours(0)
    vMinutes = ":" & vMinutes1
    vScale = "hh:mm.ss"
  End If

  If vHours >= 24 Then
    vHours1 = vHours Mod 24
    aDays = Split((vHours / 24), ".")
    vDays = aDays(0)
    vHours = ":" & vHours1
    vScale = "dd:hh:mm.ss"
  End If

  If vDays >= 365 Then
    vDays1 = vDays Mod 365
    aYears = Split((vDays / 365), ".")
    vYears = aYears(0)
    vDays = ":" & vDays1
    vScale = "yyyy:dd:hh:mm.ss"
  End If

  ReadableTime = vYears & vDays & vHours & vMinutes & vSeconds & " (" & vScale & ")"
End Function
Send this to:                          

Comments

ahmet said...

helpmeyardımınıza ıhtıyacım var eskı 1.19.1 surumlu firmware le format atabılıyodum 1.23.3 ü gorunce bı format atayım dedım (((atmaz olaydım))) formet ekranı gelıyo yalnız hata verıyo ‘’do not disconnect device during update’’ ‘’code:0x0’’ ya site ya bı link yada baksa bısey nasıl olursa olsun yardıma ıyhtıyacım var mp3 suz yapamıyorum sımdıden tesekkur. Muvo tx fm

9/1/2006 4:16:05 AM

bhavesh said...

hi

9/4/2006 12:21:25 AM

kevin said...

greattttttt

12/19/2007 6:07:05 PM

said...

i wish this worked on linux! :-(

10/24/2008 10:50:25 AM

Name
URL
Email
Email address is not published
Remember Me
Comments

CAPTCHA
Write the characters in the image above