Second Stanza

November 2, 2008

Powershell Script for Shrinking MP3 Files

Filed under: Powershell — Tags: — dfbaskin @ 10:45 pm

I needed to shrink a bunch of lectures in MP3 format so that they would fit on a DVD. I compiled SoX to include MP3 support and then used the following Powershell script to compress the files:


$srcPaths = 'E:\MP3s\Year One', 'E:\MP3s\Year Two';
$dstPath = 'E:\MP3s\DVD';
$pathPrefixLen = 8;

Write-Host;
Write-Host '--------------------------------------------------------------------';
Write-Host 'MP3 Audio Update Utility';
Write-Host '--------------------------------------------------------------------';

foreach( $srcPath in $srcPaths )
{
    Write-Host 'Source: ' $srcPath;
}
Write-Host '  Dest: ' $dstPath;
Write-Host;

foreach( $srcPath in $srcPaths )
{
    Write-Host 'Reading ' $srcPath;
    $fileList = Get-ChildItem -path $srcPath -Recurse *.mp3

    foreach( $srcFile in $fileList )
    {
        $srcFullName = $srcFile.FullName;
        $dstFullName = [System.IO.Path]::Combine( $dstPath, $srcFile.FullName.Substring( $pathPrefixLen ) );
        $tmpFullName = [Regex]::Replace( $dstFullName, '\.mp3$', '.__temp__.mp3', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase );

        if( ! [System.IO.File]::Exists( $dstFullName ) )
        {
            Write-Host $dstFullName;
    
            & 'sox.exe' $srcFullName '-r' '12k' '-c' '1' '-S' $tmpFullName 'dither' | out-null
            if( $lastExitCode -ne 0 )
            {
                Write-Host 'Stopped on error.'
                exit;
            }
    
            [System.IO.File]::Move( $tmpFullName, $dstFullName );
        }
    }
}

A file included in the SoX source code (‘INSTALL’) has instructions for building SoX with MP3 support under Windows. Here are the requirements (excerpted from this file):

Appendix: How To Compile SoX with MP3 Support on Win32
------------------------------------------------------

The following text, untested by the SoX team, was originally written by `Enter
AG' and released to the public domain.

= How To Compile Sox with MP3 Support =

== Requirements ==

The following requirements have been tested. Nevertheless, other versions may
work as well.

o Microsoft Windows (Win32, Win2k, Win2003).

o CMake (Cross Platform Make) version 2.4
  available on [http://www.cmake.org] 

o Microsoft Visual Stuio 2008 (also earlier Versions)

o SoX source distribution version 14.0.1
  available on [http://sox.sourceforge.net] 

o LAME source distribution version 3.97
  available on [http://lame.sourceforge.net] 

o MAD source distribution version 0.15.1b
  available on [http://www.underbit.com/products/mad/] 

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: