Skip to content

Instantly share code, notes, and snippets.

@kampfgnu
Last active February 24, 2017 21:23
Show Gist options
  • Save kampfgnu/16229588e87ee7cc640974817b807f39 to your computer and use it in GitHub Desktop.
Save kampfgnu/16229588e87ee7cc640974817b807f39 to your computer and use it in GitHub Desktop.
extract audio from mp4 movie and save as mp3 (using ffmpeg)
#!/usr/bin/python
# batch conversion: extract audio from all mp4's in sourceFolder and its subfolders and save as mp3.
# e.g. python extractAudioFromMp4.py sourceFolder destinationFolder
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)"
exit(1)
#parse arguments
sourceFolder = argv[0]
destinationFolder = argv[1]
# 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], '*.mp4'))]
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 = "ffmpeg -i " + "'" + filepath + "'" + " -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