convert-audio-rec-sndfile Module (convert-audio-rec-sndfile.py)


# conversion utlity front-end to sndfile-convert by castro lopo
# no mp3...
# with extra strings features (retrieve space, all small letters...)

import os
import sys
import Tkinter
import tkFileDialog
from normalize_filename import *


def get_rep(initrepload=None):
    rep = tkFileDialog.askdirectory(title="Choose the repertory to parse: ", initialdir=initrepload, mustexist="true")
    if rep == None or rep == "": rep = None
    return rep

def rec_rep(reppath, extsin, extout, erase):
    # get all folders and files in the repertory path given
    try: files_or_reps = os.listdir(reppath)
    except: return 0

    # sort by names
    files_or_reps.sort()

    # ----------------- loop on all matching files
    for file_or_rep in files_or_reps:
        file_or_rep_path = reppath + file_or_rep

        # =================================== TEST ON FILE ASPECT
        # it is a  FILE !!!
        if os.path.isfile(file_or_rep_path):
            file     = file_or_rep
            filepath = file_or_rep_path

            # NORMALIZE FILE
            filenamenormed = my_normal(file)
            # =================================== get its EXTENSION
            # let''s have a look to its extensions
            fileexts = filenamenormed.split('.')
            # get filename for sure without extension
            filenameout = fileexts[0]
            # get the first extension
            fileext  = fileexts[1].lower()

            # =================================== CONDITIONAL CONVERSION
            # we are not in Spain...
            if fileext in extsin:
                filepathin  = filepath
                filepathout = reppath + filenameout + '.' + extout
                # build the output file
                print 'sndfile-convert %s %s ' % (filepathin, filepathout)
                os.system('sndfile-convert "%s" "%s" '% (filepathin, filepathout))

                # ----------------- ERASE ??
                # bye bye sucker
                if erase:
                    os.remove(filepathin)

    # =================================== THE EXCITING RECURSION PART
    # get all folders and files in the repertory path given
    try: files_or_reps = os.listdir(reppath)
    except: return 0
    # ----------------- recurse directories
    for file_or_rep in files_or_reps:
        file_or_path = reppath + file_or_rep
        if not os.path.isfile(file_or_path):
            nextpath = reppath + file_or_rep + '/'
            rec_rep(nextpath, extsin, extout, erase)

if __name__ == '__main__':

    extsin  = ['aif', 'aifc', 'aiff']
    extout = 'wav'
    # ERASE SOUND IN ?
    erase = 1

    if len(sys.argv)>1: rootrep = sys.argv[1]
    else:
        initrepload = '/Volumes/data/'
        rootrep = get_rep(initrepload)

    if rootrep is not None:
        rec_rep(rootrep, extsin, extout, erase)



# Vincent Rioux, 2003.
# Python.org: Tutorial, Language Ref, Libraries.