fix an audio conversion script to work through multiple directories and convert mp3s to ogg vorbis

Chris Hill chris at
Mon May 9 02:38:01 UTC 2011

On Sun, 8 May 2011, Antonio Olivares wrote:

> On Sat, May 7, 2011 at 4:03 PM, Chris Hill <chris at> wrote:
>> On Sat, 7 May 2011, Antonio Olivares wrote:
>>> My question is the following:
>>> How can I run the script to recursively find all mp3's and convert 
>>> them to ogg vorbis(with ogg extension already in place/or rename them 
>>> in one step[instead of running two scripts] and deleting the mp3's) 
>>> all in one time?
>> I had a similar (but not identical) problem

  [ snip ]

>> My script is at - maybe you can adapt it 
>> to your needs.

> Thank you for your suggestion.  But I have gotten into a problem I get 
> errors and too many directories :(, Directories with spaces get 
> recreated and no ogg files are created :(

If your directory and/or file names have spaces, you will have to quote 
the filenames somehow: 'file name' vs. file_name. Maybe you could escape 
the spaces? None of my names have spaces, for exactly this reason.

[ Script mostly snipped ]

> =====================================================
> #!/bin/sh
> # From Steve Parker, only slightly modified:
  [ snip ]
> traverse()
> {
>  # Traverse a directory
>  ls "$1" | while read i
>  do
>    if [ -d "$1/$i" ]; then
>      THISDIR=$1/$i
>      # Calling this as a subshell means that when the called
>      # function changes directory, it will not affect our
>      # current working directory
>      if [ -d $OGGROOT/$THISDIR ]; then
>        # directory exists, leave it be
>        echo "$OGGROOT/$THISDIR already exists, not created."
>      else
>        mkdir $OGGROOT/$THISDIR
>        echo "Copying $THISDIR to $OGGROOT/$THISDIR"
>      fi
>      traverse "$1/$i" `expr $2 + 1`
>    else
  [ snip ]
>    fi
>  done
> }
> traverse . 0
> =====================================================
> I have modified to above script.  I don't get how the directory
> structure is copied?  I don't see a cp -r from_directory/ to
> _directory/ then mplayer -ao ....

There is no cp -R. What this is doing is replicating the directory 
structure, then copying each file. I missed it too, the first several 
times I looked at it. Almost the entire script is the definition of the 
traverse() function, which is called in the last line. The function then 
calls itself whenever it finds a directory, which makes it recurse. I 
thought it was pretty clever; wish I'd thought of it.

  [ snip ]

> Thanks for helping.  I am experimenting and trying not to shoot myself
> in the foot.

So was I; that's the main reason why there are all those `echo 
"something"` lines - I wanted to see what it would try to do, before 
actually turning it loose on my files. That and the fact that doing all 
the conversions takes a few hours.

Good luck.

Chris Hill               chris at
**                     [ Busy Expunging </> ]

More information about the freebsd-questions mailing list