# If we want to access frame at index 10, frame = vr[9] print(frame.shape) ################################################################ # For deep learning, usually we want to get multiple frames at once. Now you can use ``get_batch`` function, # Suppose we want to get a 32-frame video clip by skipping one frame in between, frame_id_list = range(0, 64, 2) frames = vr.get_batch(frame_id_list).asnumpy() print(frames.shape) ################################################################ # There is another advanced functionality, you can get all the key frames as below, key_indices = vr.get_key_indices() key_frames = vr.get_batch(key_indices) print(key_frames.shape) ################################################################ # Pretty flexible, right? Try it on your videos. ################################################################ # Speed comparison # ---------------- ################################################################ # Now we want to compare its speed with Opencv VideoCapture to demonstrate its efficiency. # Let's load the same video and get all the frames randomly using both decoders to compare their performance. # We will run the loading for 11 times: use the first one as warming up, and average the rest 10 runs as the average speed.
def video_legal(video_path, remove_illegal=True): # 检查切出来的视频是否可读 try: cv_wrong = False cap = cv2.VideoCapture(str(video_path)) flag, f = cap.read() if not flag: cv_wrong = True cap.release() if cv_wrong: # opencv检测出视频不正常,删掉 print("opencv remove", video_path, flush=True) if remove_illegal: os.remove(str(video_path)) return False vr = VideoReader(str(video_path)) last_timestamp = [-100, -100] video_wrong = False if (len(vr) > 1 and len(vr.get_key_indices()) > 1) else True if video_wrong: print("len vr {} and len(vr.get_key_indices()) {}".format( len(vr), len(vr.get_key_indices())), video_path, flush=True) if remove_illegal: os.remove(str(video_path)) return False for i in range(len(vr)): # 解决卡住 tmp = vr.get_frame_timestamp(i) if last_timestamp[0] == tmp[0] and last_timestamp[1] <= tmp[1]: print("last_timestamp", i, video_path, last_timestamp[0], tmp[0], last_timestamp[1], tmp[1], flush=True) video_wrong = True break last_timestamp = tmp if video_wrong: print("timestamp error", video_path, flush=True) if remove_illegal: os.remove(str(video_path)) return False for i in range(len(vr)): # 解决decord._ffi.base.DECORDError: [18:09:55] /io/decord/src/video/ffmpeg -loglevel quiet/threaded_decoder.cc:288: [18:08:29] /io/decord/src/video/ffmpeg -loglevel quiet/threaded_decoder.cc:216: Check failed: avcodec_send_packet(dec_ctx_.get(), pkt.get()) >= 0 (-1094995529 vs. 0) Thread worker: Error sending packet. try: _t = vr[i] except Exception as e: print("DECORDError", e, i, video_path, flush=True) if remove_illegal: os.remove(str(video_path)) return False except: print("remove video format exception", video_path, flush=True) if remove_illegal: os.remove(str(video_path)) return False return True