# Setting currentTime yields wrong frame

8 views (last 30 days)
Tom Rix on 23 Nov 2019
Commented: Tom Rix on 4 Dec 2019
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.CurrentTime
ans =
0
>> video.CurrentTime
ans =
0.0419
>> video.CurrentTime
ans =
0.0752
>> video.CurrentTime = 0.05
video =
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'
>> isequal(A,B)
ans =
logical
0
>> isequal(A,C)
ans =
logical
1
>> isequal(A,B)
ans =
logical
0
>> video.CurrentTime = 0.6
video =
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'
>> 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?

Jyotsna Talluri on 4 Dec 2019
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.CurrentTime
ans =
0
>> video.CurrentTime
ans =
0.0333
>> video.CurrentTime
ans =
0.0667
>> video.CurrentTime = 0.04;
>> 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
Tom Rix on 4 Dec 2019
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.

### Categories

Find more on Startup and Shutdown in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!