Recurse through directories and convert sound files

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
larryh
Posts: 32
Joined: Sat Mar 22, 2008 6:02 pm

Recurse through directories and convert sound files

Post by larryh » Tue Apr 28, 2009 6:17 pm

I have a couple of thousand wave files that are neatly organized in folders and subfolders. I would like to create a Rev script that will recurse through each of the files and apply a shell command to it. I want to apply a LAME MP3 conversion to each file and output to the same directory structure (although with a different root - see below). Any pointers as to how to recurse through the directory structure and apply the command to each file?
  • Eg.
    SOURCE
    • SoundfilesWAV
      • A
        • ant.wav
          apple.wav
        B
        • banana.wav
        Etc.
  • DESTINATION
    • SoundfilesMP3
      • A
        • ant.mp3
          apple.mp3
        B
        • banana.mp3
        Etc.
Thanks!

Larry

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4036
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Post by bn » Tue Apr 28, 2009 8:04 pm

larry,

are there subfolders of /soundfilesWAV/A/ ?

in other words do you know the structure of your folders? Are there only wav-files in these folders or other files also?

this example gives you the files of the subfolders one level deep with paths

Code: Select all

on mouseUp
   answer folder "please choose the WAV folder"
   if it is empty then exit mouseUp
   put the defaultfolder into origDefaultFolder
   put it into tWAVFolder
   set the defaultfolder to tWAVFolder 
   put the folders into tWAVSubFolders
   
   -- filter out operating system folders on mac (like "..", or starting with "." to make it invisible)
   filter tWAVSubFolders without ".*"
   -- filter without application folders, on the mac application may be packages that look like folders to Rev
   filter tWAVSubFolders without "*.app"
   
   
   put "" into tmyWAVListWithPath
   repeat for each line aFolder in tWAVSubFolders
      put tWAVFolder & "/" & aFolder & "/" into tActualFolder
      set the defaultfolder to tActualFolder
      put the files into tallFiles
      -- filter for macStyle ".xyz" files
      filter tallFiles without ".*"
      if tallFiles <> "" then
         
         repeat for each line aSingleFile in tallFiles
            put tActualFolder & aSingleFile & return after tmyWAVListWithPath
         end repeat
      end if
   end repeat
   delete last char of tmyWAVListWithPath -- the last return
   
   put tmyWAVListWithPath  -- goes to the message box now, could go into a field
   set the defaultfolder to origDefaultFolder -- be nice and restore the original defaultfolder
end mouseUp
regards
Bernd

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Location: London, UK
Contact:

Post by Mark Smith » Tue Apr 28, 2009 11:26 pm

The first thing I'd do is build a list of all the wav files. There are lots of 'directory walker' scripts about, but here's mine:

Code: Select all

function getAllFiles foName
  put the defaultfolder into oldFolder
  
  set the defaultfolder to foName
  repeat for each line L in the files
    if char 1 of L is "." then next repeat
    put foName & "/" & L & cr after theFiles
  end repeat
  
  repeat for each line L in the folders
    if char 1 of L is "." then next repeat
    put getAllFiles(foName & "/" & L) & cr after theFiles
  end repeat
  
  filter theFiles without empty
  set the defaultfolder to oldFolder
  return theFiles
end getAllFiles
You'll also need to create the necessary directory structure in the target folder, so this command is useful

Code: Select all

on createPath pPath
  set the itemDelimiter to "/"
  repeat with n = 1 to the number of items in pPath - 1
   if there is no folder (item 1 to n of pPath) then
     create folder (item 1 to n of pPath)
   end if
  end repeat
end createPath
it's also useful for shell commands to have Ken Ray's "q" quoting function available:

Code: Select all

function q pString
  return quote & pString & quote
end q
so a basic function for doing the work would be:

Code: Select all

on makeMp3s pSourceFolder, pTargetFolder

 put getAllFiles(pSourceFolder) into tWavList
 filter tWavList with "*.wav" -- remove all the non-wav files

 repeat for each line tWav in tWavList
  -- build the target file path
  put tWav into tMp3
  replace pSourceFolder with pTargetFolder in tMp3
  replace ".wav" with ".mp3" in tMp3

  createPath tMp3
  get shell("flac" && q(tWav) && "-o" && q(tMp3))
 end repeat

end makeMp3s

Best,

Mark Smith

larryh
Posts: 32
Joined: Sat Mar 22, 2008 6:02 pm

Post by larryh » Wed Apr 29, 2009 6:48 pm

Thanks Mark and Bernd, very helpful!

Post Reply

Return to “Multimedia”