import argparse, sys, os, glob, subprocess supported = ['ogg', 'mp3'] def sanity_check_system(): r = subprocess.call("ffmpeg -version", shell=True) == 0 if not r: sys.exit("ffmpeg not installed. Aborting...") def convert(i, o, f): global supported if f not in supported: print("warning: format " + f + "is not supported") return if f == 'mp3': codec = 'libmp3lame' elif f == 'ogg': codec = 'libvorbis' subprocess.call(['ffmpeg', '-i', i, '-acodec', codec, o]) if __name__ == '__main__': p = argparse.ArgumentParser(description='Converts .wav files to other formats (using ffmpeg)'); p.add_argument('-i', '--input', help='input directory'); p.add_argument('-f', '--format', help='output formats (ogg and/or mp3)', nargs="+"); sanity_check_system() args = p.parse_args() if not args.format: sys.exit("Nor formats specified. Aborting.") formats = [] for f in args.format: if f not in supported: print("warning: format " + f + "is not supported") else: formats.append(f) if not formats: sys.exit("None of the specified formats are supported. Aborting.") input_dir = "." if args.input: input_dir = args.input wav_files = glob.glob(os.path.join(input_dir, "*.wav")) for w in wav_files: for f in formats: fn = os.path.join(input_dir, os.path.basename(w).replace('.wav', '.' + f)) convert(w, fn, f)