Hi,
I have a recording setup with several different webcams (the number of which may change from day to day). I am trying to write a program in Python that allows me passing a list of input videos from a txt file, open all videos and process the video feeds SIMULTANEOUSLY, but save them SEPARATELY into different video files.
The code I have works great so far, but I am getting stuck in one particular question:
Is there a way of creating a list of VideoWriter objects and write to them in turns, calling them via an index?
Opening several videos via iteration works like a charm (‘cam_name’ is the list of my camera IDs, i.e. 0, 1, 2, … ):
cap = [cv2.VideoCapture(i) for i in cam_name]
Even creating a list of VideoWriter objects works without problems (and all videos start appearing under the file paths passed by the list ‘outnames’):
outfile = [cv2.VideoWriter(i,cv2.VideoWriter_fourcc(‘M’, ‘J’, ‘P’, ‘G’),30,(640, 480)) for i in [item for item in outnames]]
However, within my while loop, I seem to not be able to call the ‘.write’ function of these VideoWriter objects from my list using an iterator (here, ‘i’ is an integer indicating the position in ‘outfile’ I want to address, and ‘frame’ is the image I’d like to stich to the video):
outfile[i].write(frame)
I double-checked each step in the code and the problem definitely appears in ‘outfile[i].write’. Notably, the code runs through without error! It just seems like ‘write’ isn’t doing anything. The video files created with ‘cv2.VideoWriter’ remain unmodified while my loop is running through.
If I use an ‘imwrite’ workaround, I manage to save the single frames and later stich them together into videos with e.g. ffmpeg, but I’d like to avoid that and call the VideoWriter objects in my loop.
Hope someone can help me! Thanks!
PS: If someone knows the solution in C++, it would also help