Skip to content

Instantly share code, notes, and snippets.

@kampfgnu
Created February 24, 2017 21:25
Show Gist options
  • Save kampfgnu/f15999b95eb6abb5a524406212d054ad to your computer and use it in GitHub Desktop.
Save kampfgnu/f15999b95eb6abb5a524406212d054ad to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# batch conversion: convert all .wav files in sourceFolder and its subfolders to mp3 or aifc.
# e.g. python convertAudioFiles.py sourceFolder destinationFolder 0
import sys
import os
from glob import glob
from subprocess import call
import shlex
def main(argv):
if len(argv) < 2:
print "missing arguments (arg1: source folder path, arg2: destination folder path, optional arg3: create .aifc files)"
exit(1)
#parse arguments
sourceFolder = argv[0]
destinationFolder = argv[1]
useAFConvert = 0
if len(argv) == 3:
useAFConvert = argv[2]
# if os.listdir(destinationFolder):
# print "files exist in destination, exit."
# exit(1)
#get all filepaths
filePaths = [y for x in os.walk(sourceFolder) for y in glob(os.path.join(x[0], '*.wav'))]
if len(filePaths) == 0:
print "no files were found, exiting."
exit(1)
else:
print "number of files to convert: " + str(len(filePaths))
#file names
filenames = []
for filepath in filePaths:
filenames.append(os.path.basename(filepath))
#search for duplicates
duplicates = listDuplicates(filenames)
if len(duplicates) > 0:
print "early exit because duplicate files were found: " + str(duplicates)
exit(1)
#start conversion
for filepath in filePaths:
destinationFilename = os.path.splitext(os.path.basename(filepath))[0]
destinationFilepath = os.path.join(destinationFolder, destinationFilename)
command = ""
if useAFConvert:
command = "afconvert -v -f AIFC -d ima4 " + "'" + filepath + "'" + " -o " + "'" + destinationFilepath + ".aifc" + "'"
else:
command = "ffmpeg -i " + "'" + filepath + "'" + " -codec:a libmp3lame -qscale:a 2 -ar 48000 -ab 192k " + "'" + destinationFilepath + ".mp3" + "'"
args = shlex.split(command)
call(args)
# call(["ffmpeg", "-i", '""' + filepath + '""', "-codec:a libmp3lame", "-qscale:a 320k", '""' + destinationFilepath + '""'])
def listDuplicates(seq):
seen = set()
seen_add = seen.add
# adds all elements it doesn't know yet to seen and all other to seen_twice
seen_twice = set( x for x in seq if x in seen or seen_add(x) )
# turn the set into a list (as requested)
return list( seen_twice )
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment