视频转化为帧处理
1. 使用opencv(python)
import cv2,os
vc = cv2.VideoCapture('你的文件名字') # 读入视频文件
c = 0 #抽帧名字0开始
if vc.isOpened(): # 判断是否正常打开
rval, frame = vc.read()
while rval: # 循环读取视频帧
cv2.imwrite(r'保存地址/' + str(c) + '.jpg', frame) # 存储为图像
cv2.waitKey(1)
c = c + 1
rval, frame = vc.read()
vc.release()
2. 使用scikit-video
注意, sk-video已经过时了, 自行卸载后pip下scikit-video, 此外需要FFmpeg的支持, 如果报下面错, 按照我代码写的设置目录即可, 设置目录时候注意在import skvideo后, skvideo.io之前setPath, 不然无效
Cannot find installation of real FFmpeg (which comes with ffprobe).
import skvideo
skvideo.setFFmpegPath(r'C:/ffmpeg/bin')
import skvideo.io
print(skvideo.getFFmpegPath())
videodata = skvideo.io.vread(r'苍老师.MP4')
# videodata就是一个读取了视频所有帧的numpy数组了, 视频太大也可以选一帧帧读的
3. 使用matlab中的VideoReader(mmreader过时)
obj = VideoReader('苍老师.MP4');
numFrames = obj.NumberOfFrames; % 读取视频的帧数
for i = 1 : numFrames
frame = read(obj,i); % 读取每一帧
%imshow(frame); %显示每一帧
imwrite(frame,strcat(num2str(i),'.jpg'),'jpg'); % 保存每一帧
end
4. 使用vidsr
一个新的库, 挂上来看看
>>> from vidsrc import VideoSource
>>> video = VideoSource('test.avi', grayscale=False)
>>> len(video) # number of frames in video
48
>>> video.duration # length in s
1.6016
>>> video.framerate # frames per second
29.970089850329373
>>> video.shape # frames, height, width, color channels
(48, 64, 64, 3)
>>> frame = video[0] # access first frame
>>> frame = video[-1] # access last frame
>>> for frame in video:
... pass # do_something_with(frame)
网友评论