Batch files to convert mp3 podcasts to space-saving spx files

If you Google for something to convert mp3 to speex format, you won’t find much. I couldn’t find a method on the internet to automatically and easily convert them, so maybe my batch files below are a first of sorts. It has worked for me with Windows 2000 and Windows Vista, however it does have some issues with certain characters in podcast names, such as ®, which I can fix if there is any interest.

Why would you want to convert one lossy audio format to another lossy audio format, with the resulting noticeable loss of quality that results? Because podcasts aren’t really available in .wav format, and .spx format is more efficient than .mp3 for the spoken word and saves a little space on my Palm Tungsten T3’s SD card.

If you want to get this working yourself, you’ll need the Juice podcast receiver, ffmpeg Windows binaries and speexenc (look for the speexw installer):
I will assume you extract ffmpeg to C:\TOOLS, and install speex to the default C:\SPEEXW, and that you copy and paste the batch files into Notepad and save to C:\BAT. Make sure that you append ;C:\TOOLS;C:\SPEEXW;C:\BAT to your existing PATH environment variable. You will also have to make sure the folders C:\PODCASTS and C:\PDA2GO exist.

Look for ipodder.cfg. On Vista, it should be in C:\Users\YOUR LOGIN NAME\AppData\Roaming\iPodder. Change the download directory to C:\\PODCASTS (double backslashes intentional) which is used in the batch files below. (In Windows 2000 or XP, you can also change this in the GUI, but in Vista you can’t even open the GUI until you change it because the line refers to My Documents which in Vista has changed to simply Documents.)

From Juice, click File > Preferences, go to the Advanced tab, and check the box which says “Run this command after each download” and type the following in the text box below it:
podwork "%f" "c:\pda2go"

If you are running Windows Vista, you will need to go to properties of Juice.exe and set it to run in Windows XP SP2 compatibility mode (this is a requirement of being able to run Juice 2.2 on Vista, not a requirement of podwork.cmd):

The podwork.cmd script below converts a single file from mp3 to speex format. You will want to experiment with the quality variable (10 is the maximum, and at 7 the lesser quality is noticeable).


@echo off
rem podwork.cmd
rem thanks to 1CMDFAQ.TXT by Prof. Timo Salmi for
rem bringing my DOS skills into the 21st Century :)
rem See
setlocal enableextensions
set quality=7

rem set pod to your source folder (no trailing slash)
set pod=c:\podcasts

if [%2]==[] goto :EOF
if [%tmp%]==[] goto :EOF

set ptmp=%tmp%\
set ptmp=%ptmp:\\=\%
set ptmp=%ptmp:~0,-1%

if not exist "%ptmp%\*.*" goto :EOF

set pfol=%~dp1
set pfol=%pfol%\
set pfol=%pfol:\\=\%
set pfol=%pfol:~0,-1%
for %%a in ("%pfol%") do set pfol=%%~nxa

set tful=%~2
set tful=%tful%\
set tful=%tful:~0,-1%

if not exist "%tful%\*.*" goto :EOF
for %%a in ("%tful%") do set tfol=%%~nxa

rem Make necessary tree structures
xcopy "%pod%" "%ptmp%\podwork\" /t
xcopy "%pod%" "%tful%" /t

rem Skip if Speex file already exists
if exist "%tful%\%pfol%\%~n1.spx" goto :EOF

@echo on
ffmpeg -i "%~1" "%ptmp%\podwork\%pfol%\%~n1.wav"
if not exist "%ptmp%\podwork\%pfol%\%~n1.wav" goto :EOF
speexenc --quality %quality% "%ptmp%\podwork\%pfol%\%~n1.wav" "%tful%\%pfol%\%~n1.spx"
@echo off

IMPORTANT NOTE: gets confused by double hyphens: Line 44 should start off “speexenc –quality” not “speexenc —quality” or “speexenc -quality”.

That works for new podcast downloads, but since I had downloaded some podcasts already, I wrote a quick podsweep.cmd script to run podwork.cmd for each pre-existing mp3 file. Make sure Juice is closed before running it. It is hard-coded for what I use as my Juice top-level download folder and my output folder:


cd \podcasts
sweep.cmd for %%f in (*.mp3) do podwork "%%~ff" "c:\pda2go"

podsweep.cmd depends on sweep.cmd to run the command in every directory. With sweep.cmd, I mimic an ancient MS-DOS tool from the 80’s called SWEEP.COM:


@echo off
for /R %%D in (.) do (
  cd "%%D"
cd /d %cd%

Thanks to Chris Pirillo for the information on getting Juice to work in Windows Vista.