def encode(input): size = os.path.getsize(input) try: rotation = get_set_rotation(input) except: return #if(time.time() - os.path.getctime(file) < 2592000.0): # return print 'File: ' + input print 'Size: %s' % size print 'Rotatation: %s' % rotation print 'Time: %s' % time() print 'ctime: %s' % os.path.getctime(file) print 'Time Diff: %s' % (time() - os.path.getctime(file)) parts = os.path.splitext(input) output = input tmp = parts[0] + '.out' + parts[1] backup = parts[0] + '.bak' + parts[1] if(os.path.exists(backup)): print 'Skipping already encoded file: ' + input return #do we need to rotate? filters = "" if rotation == 90: filters = "transpose=1" elif rotation == 180: filters = "vflip,hflip" elif rotation != 0: print 'Skipping file with rotation: %s' % rotation return if filters != "": filters = "-vf " + filters #do the encoding cmd = './ffmpeg -i \'{input}\' {filters} \'{output}\''.format(input=input,filters=filters,output=tmp) print "Command: " + cmd p = Popen(shlex.split(cmd), stdout=FNULL, stderr=PIPE) stdout.write("Encoding " + input + " ") stdout.flush() code = None while True: stdout.write(".") stdout.flush() sleep(.1) code = p.poll() if code is not None: break print "" print "Result: %s" % p.returncode if code == 0: #fix the rotation if rotation != 0: print "Adusting rotation to 0" get_set_rotation(tmp, 0) os.rename(input, backup) #save a backup just in case os.rename(tmp, input) else: #UNDO! UNDO! for line in p.stderr: print line os.remove(tmp)
url_links = "http://54.249.245.7/notebook/_design/media/_view/quicktime"; import urllib import json import qtrotate import os #fetch the list urllib.urlretrieve (url_links, "links.json") with open("links.json") as json_file: json_data = json.load(json_file) for item in json_data["rows"]: for file in item["value"]["files"]: urllib.urlretrieve (file["url"], file["file_name"]) movie_rotation = qtrotate.get_set_rotation(file["file_name"]) print "Downloading:" + file["url"] + " rotates " + str(movie_rotation) rotate_index = None if(movie_rotation > 180): movie_rotation = movie_rotation - 180 if (movie_rotation == 90): rotate_index = "transpose=1"; if (movie_rotation == -90): rotate_index = "transpose=0,transpose=0"; if (movie_rotation == 180): rotate_index = "transpose=2,transpose=2"; if (movie_rotation == 270): rotate_index = "transpose=3"; if (rotate_index != None): #cmd = "mencoder "+file["file_name"]+" -o rot_"+file["file_name"]+" -vf rotate="+str(rotate_index)+" -oac mp3lame -ovc lavc" #http://stackoverflow.com/questions/3937387/rotating-videos-with-ffmpeg # http://ffmpegmac.net/ # 0 = 90CounterCLockwise and Vertical Flip (default) # 1 = 90Clockwise # 2 = 90CounterClockwise # 3 = 90Clockwise and Vertical Flip