Setting currentTime yields wrong frame

조회 수: 3 (최근 30일)
Tom Rix
Tom Rix 2019년 11월 23일
댓글: Tom Rix 2019년 12월 4일
I try to access specific frames from a video file. After opening it with a VideoReader and setting a time via currentTime in seconds readFrame will return the wrong frame. Interestingly, when increasing the currentTime from 0 to 0.1 to 0.2 and so on nothing changes, readFrame will always return the first frame. Only after reaching second 1 it will return a new frame, however this is the 30th frame (as my video has slightly more than 30 fps). Why did it skip all the other 29 frames?
Here an example set of instructions to show what the problem is:
video = VideoReader('videos/myvideo.mp4');
>> video.CurrentTime
ans =
0
>> A = readFrame(video);
>> video.CurrentTime
ans =
0.0419
>> B = readFrame(video);
>> video.CurrentTime
ans =
0.0752
>> video.CurrentTime = 0.05
video =
VideoReader with properties:
General Properties:
Name: 'myvideo.mp4'
Path: 'path/to/videos'
Duration: 69.1200
CurrentTime: 0.0500
NumFrames: 2068
Video Properties:
Width: 2160
Height: 3840
FrameRate: 30.0171
BitsPerPixel: 24
VideoFormat: 'RGB24'
>> C = readFrame(video);
>> isequal(A,B)
ans =
logical
0
>> isequal(A,C)
ans =
logical
1
>> isequal(A,B)
ans =
logical
0
>> video.CurrentTime = 0.6
video =
VideoReader with properties:
General Properties:
Name: 'myvideo.mp4'
Path: 'path/to/videos'
Duration: 69.1200
CurrentTime: 0.6000
NumFrames: 2068
Video Properties:
Width: 2160
Height: 3840
FrameRate: 30.0171
BitsPerPixel: 24
VideoFormat: 'RGB24'
>> D = readFrame(video);
>> isequal(A,D)
ans =
logical
1
I'm saving the first frame in A and the second one in B. They are different. After setting currentTime to 0.05 which is after the time of A and before the time of B I retrieve another frame C. Surprisingly this frame is identical with A and not with B, although the video object stored the currenttime as 0.0500 correctly. The same happens with currentTime at 0.6 which is a lot after frame B. The retrieved frame D is identical with A.
Only after setting current time to 1 or 1.1 or so and retrieving a frame it yields a frame that is not identical to frame A. However, it is also not identical to frame B, it is identical to the frame with the number of the frame rate.
Is this a problem with my video file or is it a problem with the currentTime or readFrame function in MATLAB? Has anyone experienced the same issue before?

답변 (1개)

Jyotsna Talluri
Jyotsna Talluri 2019년 12월 4일
Each frame of the video is read in a specific time depending on the video Frequency. When we set the 'CurrentTime' to a certain value readFrame reads a frame from the starting time of a frame in which this time is included .From your question frames B,C and not the frames A and C .I tried with an example video and it worked fine.
video = VideoReader('PIL.mp4');
>> video.CurrentTime
ans =
0
>> A = readFrame(video);
>> video.CurrentTime
ans =
0.0333
>> B = readFrame(video);
>> video.CurrentTime
ans =
0.0667
>> video.CurrentTime = 0.04;
>> C = readFrame(video);
>> isequal(A,B)
ans =
logical
0
isequal(A,C)
ans =
logical
0
>> isequal(B,C)
ans =
logical
1
Try with another video and sometimes it depends on the Operating System
  댓글 수: 1
Tom Rix
Tom Rix 2019년 12월 4일
You're right, it's a problem with the video file and not with Matlab. Other video files work fine, somehow this one doesn't.
There might be a connection to the tip provided on the Documentation page of VideoReader: https://uk.mathworks.com/help/matlab/ref/videoreader.html#d117e1573060
It says that there are issues on MacOS and the solution is to resave the file in QuickTime and then is should work.
My solution was to read in all the frames and resave it in Matlab and than open the resaved version which then works perfectly.
However, I still don't know what the underlying problem is.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for IP Cameras에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by