-
-
Save SuperShinyEyes/028e673695e366e5b40aac4ce6dc8c28 to your computer and use it in GitHub Desktop.
Mass Convert MP4/AVI clips to GIF with a multithreaded Python function, using a variable to set number of threads required.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import imageio | |
import os, sys | |
import glob | |
import threading | |
import time | |
class TargetFormat(object): | |
GIF = ".gif" | |
MP4 = ".mp4" | |
AVI = ".avi" | |
def filenameFromPath(inputpath): | |
return inputpath.split("\\")[-1:][0] | |
def convertFile(inputpath, targetFormat): | |
"""Reference: http://imageio.readthedocs.io/en/latest/examples.html#convert-a-movie""" | |
filename = filenameFromPath(inputpath) | |
outputpath = "outputfiles\\" + filename.split(".")[0] + targetFormat | |
reader = imageio.get_reader(inputpath) | |
fps = reader.get_meta_data()['fps'] | |
writer = imageio.get_writer(outputpath, fps=fps) | |
for i,im in enumerate(reader): | |
writer.append_data(im) | |
writer.close() | |
print("\t\tFinished: \t{0}".format(filenameFromPath(inputpath))) | |
def createThread(threads, inputpath): | |
t = threading.Thread(name = filenameFromPath(inputpath), target=convertFile, args=(inputpath,TargetFormat.GIF)) | |
threads.append(t) | |
t.start() | |
def main(): | |
maxThreads = 9 | |
index = 0 | |
conv_list = glob.glob("convertfiles\\*.mp4") | |
threads = [] | |
while (len(threads)<len(conv_list)): | |
if (len(threads) - len([t for t in threads if not t.is_alive()]) < maxThreads): | |
inputpath = conv_list[index]; | |
print("File {0} of {1}\tStarted: \t{2}".format(index+1, len(conv_list),filenameFromPath(inputpath))) | |
createThread(threads, conv_list[index] ) | |
index+=1 | |
if __name__ == '__main__':main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment