Home > Back-end >  python opencv load consecutive videos from list
python opencv load consecutive videos from list

Time:07-04

I'm trying to play a list of video files from a directory in opencv and be able to navigate the list with hotkeys.

I manage to open the second video and go back by one but afterwards it bugs and closes.

This is what I have so far.

import glob
import cv2
import numpy as np

#importing file list
d = r"L:\Projects\vids"
fl = glob.glob(d   "/*.mp4")

#file list counter
counter = 0

img = cv2.VideoCapture(fl[counter])


while True:
    ret, frame = img.read()
    cv2.imshow('viewer', frame)

    #checking if key is p(next) u(previous) or q
    k = cv2.waitKey(1) 
    
    if k == ord('q'):
        break
    elif k == 112:
        img = cv2.VideoCapture(fl[counter 1])
    elif k == 117:
        img = cv2.VideoCapture(fl[counter-1])


img.release()
cv2.destroyAllWindows()

The end goal is to make a video file viewer by directory

  • scrub through the video
  • select in's and outs
  • List item
  • add keywords to the selections
  • get video start and end movement direction
  • send the data over to ffmpeg to convert for further editing.

Thanks in advance for any input,

CodePudding user response:

you have to care about under / overflow here. use a modulo to make it "wrap around" the array borders:

elif k == 112:
    counter = (counter   1) % len(fl) # go to first ob overflow
    img = cv2.VideoCapture(fl[counter])
elif k == 117:
    counter = (counter - 1   len(fl)) % len(fl) # go to last on underflow
    img = cv2.VideoCapture(fl[counter])
  • Related