I’m currently working on a python project which has to deal with many videos on the web. Let a list of these videos url_list, i.e.
url_list = ['sample_url1.mp4', ..., 'sample_url_n.mp4']
For a performance reason, I decided to use ThreadPool to get videos asynchronously. The code is like this:
from concurrent.futures import ThreadPoolExecutor import cv2 def get_video(url): cap = cv2.VideoCapture(url) if hasattr(cap, 'isOpened') and cap.isOpened(): return cap with ThreadPoolExecutor() as executor: caps = executor.map(get_video, url_list) # video processing section for cap in caps: while cap.isOpened(): ret, frame = cap.read() if ret: do_something_with(frame)
And here is my questions.
How exactly does
cv2.VideoCapturework when using url as a parameter? Does it download(in memory) the whole video at once from url at the
cap = cv2.VideoCapture(url)part? Or does it work like a video stream from url?
In other words, if a network gets disconnected at video processing section, will the rest of the code work?
(I tested it several times[disconnecting the internet at the video processing part], and it works. But I’m not 100% sure it is safe to use.)
cv2.VideoCapturesafe for using with multithreading and multiprocessing?
Thanks in advance!
Edit I’m using ffmpeg as a backend.
I tested with a big size mp4 file and it failed the connectivity test. So I assume that VideoCapture doesn’t read the whole video but just some parts of it. It will read the rest of it in
cap.read(). If this is wrong, any correction will welcome.
videoio module use multi-platform libraries such as
ffmpeg, gstreamer, Media SDK, platform specific such as
WINRT, AVFOUNDATION, MSMF/DSHOW, V4L/V4L2 as backends to operate with files or urls. When open a file/url(
VideoCapture::open), it will loop over all registered backends until open successfully. Take FFMPEG as example, it can read from url、hls if it implement the responding protocols(such as RTMP ).
cv2.VideoCapture is same to use multithreading, but take care to show the frames only in main thread. here is an example: How to capture Multi camera with Opencv Multi threading (python)