#!/usr/bin/python import os,re,shutil,shlex,subprocess from string import Template debug=False currentPath=os.path.dirname(__file__) inDir = currentPath+'/IpadConverterIN/' outDir = currentPath+'/IpadConverterOUT/' ffmpegi= Template("ffmpeg -i '$i'") ffmpegc= Template("ffmpeg -i '$i' -r 30 -vcodec libx264 -acodec libfaac -threads 0 -ar 48000 -ab 128k -ac 2 -y -crf 21 -vpre fast $o '$c'") pushInItunes= Template(""" osascript -e 'tell application "iTunes" add POSIX file "$a" end tell'""") if debug: options='-t 30' else: options='' print """ _____ _ _ |_ _| | | | | | | _ __ __ _ __| | ___ ___ _ ____ _____ _ __| |_ ___ _ __ | | | '_ \ / _` |/ _` | / __/ _ \| '_ \ \ / / _ \ '__| __/ _ \ '__| _| |_| |_) | (_| | (_| | | (_| (_) | | | \ V / __/ | | || __/ | |_____| .__/ \__,_|\__,_| \___\___/|_| |_|\_/ \___|_| \__\___|_| | | |_| """ if not os.path.isdir(inDir): os.makedirs(inDir) print "IpadConverterIN directory created" if not os.path.isdir(outDir): os.makedirs(outDir) print "IpadConverterOUT directory created" while 1: nf=''; for f in os.listdir(inDir): if re.search('\.[a-zA-Z0-9]{3}$',f) : proc = subprocess.Popen(ffmpegi.substitute(i = inDir+f) ,shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) fbff= proc.communicate()[0] if debug: print repr(fbff) print "---------------------" movieSize=re.search('([0-9]+)x([0-9]+)', fbff) if not movieSize : print '"'+f+'" no movie screen size, is this a movie ?' shutil.move(inDir+f,outDir) else: nf=f if int(movieSize.group(1))>1024 or int(movieSize.group(2))>768: options+=' -s xga ' break elif not re.search('^\.',f): print '"'+f+'" not a valid file name' if nf=='': print "no more file in "+inDir; break print 'processing: "'+nf+'" ('+movieSize.group(1)+'x'+movieSize.group(2)+')' proc = subprocess.Popen(ffmpegc.substitute(i = inDir+nf, c=outDir+nf+'.mp4', o=options ),shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) fbff= proc.communicate()[0] if debug: print repr(fbff) print "---------------------" shutil.move(inDir+nf,outDir) convTime = re.findall(' time\=([0-9]+\.[0-9]+) bitrate',fbff) if not convTime: print "error during conversion" os.remove(outDir+nf+'.mp4') break print '"'+nf+'" converted in '+convTime.pop()+' sec' proc = subprocess.Popen(pushInItunes.substitute(a = outDir+nf+'.mp4'),shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) itff= proc.communicate()[0] if debug: print itff print "---------------------" print '"'+nf+'" is in Itunes' os.remove(outDir+nf+'.mp4') if debug: shutil.move(outDir+nf, inDir) break print "\nyou can close this terminal now\n\n"