目录
av读取mp4
pip install av
import os
import sys
import cv2
from av import open
video = open("d:/2.mp4")
stream = next(s for s in video.streams if s.type == 'video')
for packet in video.demux(stream):
print(packet)
for frame in packet.decode():
# some other formats gray16be, bgr24, rgb24
img = frame.to_nd_array(format='bgr24')
cv2.imshow("Test", img)
if cv2.waitKey(1) == 27:
break
cv2.destroyAllWindows()
torchvision 读取mp4 用到了pyav
linux安装方法:
pip install av
pyav
frames, _, _ = torchvision.io.read_video(video_path, pts_unit="sec")
# Convert frames to tensor and move to GPU
frames = frames.permute(0, 3, 1, 2).cuda() # Shape: (num_frames, C, H, W)
# Process video frames in batches
num_frames = frames.shape[0]
print("frames shape: ", frames.shape, num_frames)