Skip to content

Instantly share code, notes, and snippets.

@rsnk96
Last active September 9, 2020 08:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsnk96/cab4d282ec347c4fc61f0b33e4b75316 to your computer and use it in GitHub Desktop.
Save rsnk96/cab4d282ec347c4fc61f0b33e4b75316 to your computer and use it in GitHub Desktop.
Outline of a parallelized video processing code
def process_video(group_number):
cap = cv2.VideoCapture("input_file.mp4")
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_jump * group_number)
proc_frames = 0
out = cv2.VideoWriter("output_{}.avi".format(group_number), ...)
while proc_frames < frame_jump:
ret, frame = cap.read()
# ... DO SOME STUFF TO frame ... #
proc_frames += 1
out.write(frame)
return None
# Process the video by splitting it into as many fragments as the number of virtual cores you have
import multiprocessing as mp
frame_jump = cv2.VideoCapture("input_file.mp4").get(cv2.CAP_PROP_FRAME_COUNT) // mp.cpu_count()
mp.Pool(num_processes).map(process_video, range(num_processes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment