查找视频中的帧数

时间:2015-01-14 13:27:32

标签: matlab

我试图逐帧显示视频,我需要知道它的总帧数。但它显示错误:“无法确定此文件中的帧数”。

obj = VideoReader('a.avi');
nof=obj.NumberOfFrames;
for i=1:nof
    img = read(obj,i);
    imshow(img);

end

3 个答案:

答案 0 :(得分:5)

视频可能采用可变帧速率进行编码,在这种情况下,MATLAB在读取最后一帧之前无法检测帧数(如文档here所示。

要找到答案,您应按照建议here阅读最后一帧。

Obj = VideoReader('varFrameRateFile.avi');
lastFrame = read(Obj, inf);
numFrames = Obj.NumberOfFrames;

答案 1 :(得分:0)

vidFrames = read(readerobj);
numFrames = get(readerobj, ‘numberOfFrames’);

答案 2 :(得分:0)

Matlab将在以后的版本中弃用read()和NumberOfFrames。下面是一些视频打开和查找帧数功能,我用它来完成Matlab版本更改。我已经在一些avi文件上测试了类似版本,并安装了所需的编解码器。

function [totalFrames] = findNumFrames(videoSrcPath, SelectedReader)
    % open video source
    if ( SelectedReader == 1 )
            % VideoReader currently has a read to random frame but will be
            % deprecated soon                
            videoSrc = VideoReader(videoSrcPath); 
    elseif ( SelectedReader == 0 )
            %vision.VideoFileReader does not have a read to random frame
            videoSrc = vision.VideoFileReader(videoSrcPath,...
               'ImageColorSpace', 'Intensity'); 
    end

    % get number of frames
    if ( SelectedReader == 1 )
        % The below read() and NumberOfFrames will be deprecated in
        % future versions and we will have to use SelectReader == 0
        % or readFrame 
        try                    
            lastFrame = read(videoSrc, inf); % need to read last frame to get the number of frames
            totalFrames = videoSrc.NumberOfFrames;                    
        catch
            warning('Problem using read - possibly deprecated. Using readFrame instead.'); 
        end

    elseif ( SelectedReader == 0 )

        totalFrames = 0;
        while( ~isDone(videoSrc) )
            step(videoSrc);
            totalFrames = totalFrames + 1;
        end
        reset(videoSrc);

    end

end