def read_video(v_name):
"""A workaround function for reading video.
Apparently precompiled OpenCV couldn't read AVI videos on Mac OS X
and Linux,
therefore I use PyAV, a ffmpeg binding to extract video frames
Parameters
----------
v_name : string
absolute path to video
Returns
-------
frames : list
An ordered list for storing frames
num_frames : int
number of frames in the video
"""
container = av.open(v_name)
video = next(s for s in container.streams if s.type == b'video')
frames = []
for packet in container.demux(video):
for frame in packet.decode():
frame_t = np.array(frame.to_image())
frames.append(cv2.cvtColor(frame_t, cv2.COLOR_RGB2BGR))
return frames, len(frames)
评论列表
文章目录