| #!/usr/bin/python | |
| import Image | |
| import sys | |
| def itergif(gif): | |
| try: | |
| gif.seek(0) | |
| while True: | |
| yield gif | |
| gif.seek(gif.tell()+1) | |
| except EOFError: | |
| return | |
| def framerate(gif): | |
| frames = [] | |
| for frame in itergif(gif): | |
| frames.append(float(frame.info["duration"])) | |
| average = sum(frames) / len(frames) | |
| for num, frame in enumerate(frames): | |
| # if the difference between the average and the frame | |
| difference = abs(average - frame) | |
| # is more than 5% of the average | |
| if difference > (average * 0.20): | |
| raise Exception("frame %d has duration %f, which is more than 20%% different than average of %f" % (num, frame, average)) | |
| if difference > (average * 0.05): | |
| # then the frame will jitter noticeably as an avi and the user should know | |
| sys.stderr.write("warning: frame %d has duration %f! (average: %f)\n" % (num, frame, average)) | |
| sys.stderr.flush() | |
| try: | |
| return 1000.0/average | |
| except ZeroDivisionError: | |
| return 25.0 | |
| def file_rate(filename): | |
| return framerate(Image.open(filename)) | |
| if __name__ == "__main__": | |
| print file_rate(sys.argv[-1]) |
| import framerater | |
| import os | |
| import subprocess | |
| import tempfile | |
| import sys | |
| import shutil | |
| def command(quitter, *args): | |
| sys.stderr.write("command: %r\n" % (args,)) | |
| popen = subprocess.Popen(args, stdout=2, stderr=2) | |
| while True: | |
| try: | |
| popen.wait() | |
| break | |
| except KeyboardInterrupt: | |
| quitter.quit() | |
| continue | |
| class Quitter(object): | |
| def __init__(self): | |
| self.quitting = False | |
| def doquit(self): | |
| if self.quitting: | |
| raise KeyboardInterrupt | |
| def quit(self): | |
| print "quitting..." | |
| self.quitting = True | |
| def name2avi(input): | |
| output = input | |
| if output.endswith(".gif"): | |
| output = output[:-4] | |
| output = output + ".avi" | |
| return output | |
| def gif2avi(quitter, input, output=None): | |
| framerate = framerater.file_rate(input) | |
| if not output: | |
| output = name2avi(input) | |
| if os.path.exists(output): | |
| raise IOError("file exists: %s" % output) | |
| tempdir = tempfile.mkdtemp() | |
| frametemplate = tempdir + "/test%05d.png" | |
| command(quitter, "/usr/bin/convert", input, "-quality", "10", frametemplate) | |
| if os.path.exists(output): | |
| raise IOError("file exists: %s" % output) | |
| command(quitter, "/usr/bin/ffmpeg", "-r", str(int(framerate)), "-i", frametemplate, output) | |
| shutil.rmtree(tempdir) | |
| return output | |
| if __name__ == "__main__": | |
| print gif2avi(Quitter(), sys.argv[-2], sys.argv[-1]) |
| import os | |
| import glob | |
| import gif2avi | |
| import sys | |
| def gifs2avis(indir, outdir): | |
| if not os.path.exists(outdir): | |
| os.makedirs(outdir) | |
| infiles = glob.glob("%s/*.gif" % indir) | |
| quitter = gif2avi.Quitter() | |
| for infile in infiles: | |
| outfile = gif2avi.name2avi(outdir+"/"+os.path.basename(infile)) | |
| stat = os.stat(infile) | |
| times = (stat.st_atime, stat.st_mtime) | |
| try: | |
| gif2avi.gif2avi(quitter, infile, outfile) | |
| except IOError, e: | |
| import traceback | |
| traceback.print_exc() | |
| continue | |
| os.utime(outfile, times) | |
| print infile, outfile | |
| quitter.doquit() | |
| if __name__ == "__main__": | |
| gifs2avis(os.path.realpath(sys.argv[-2]), | |
| os.path.realpath(sys.argv[-1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment