Clear Filters
Clear Filters

how to convert video object current time with media player time.

17 views (last 30 days)
I'm reading video time with VideoReader() videoobject current time, but when I see media player time shown in seconds, minutes, and hours, they don't seem to match.
Could someone kindly explain what I'm missing or why my comprehension is different even if they both represent the same value?
Thank you

Accepted Answer

Diwakar Diwakar
Diwakar Diwakar on 24 May 2023
Try this code. May be this code will help you.
% Specify the path to the video file
videoPath = 'path_to_video_file';
% Create a VideoReader object
videoObj = VideoReader(videoPath);
% Read the first frame to get the frame rate
frame = readFrame(videoObj);
frameRate = videoObj.FrameRate;
% Get the current time in seconds using VideoReader
currentTimeFrames = videoObj.CurrentTime;
currentTimeSeconds = currentTimeFrames / frameRate;
% Display the current time in seconds using VideoReader
disp(['Current Time (VideoReader): ' num2str(currentTimeSeconds) ' seconds']);
% Open the video file in a media player
mediaPlayer = VideoReader(videoPath);
mediaPlayerObj = vision.VideoPlayer;
% Play the video and observe the media player time
while hasFrame(mediaPlayer)
frame = readFrame(mediaPlayer);
step(mediaPlayerObj, frame);
currentTimeMedia = mediaPlayer.CurrentTime;
% Display the current time in seconds using the media player
disp(['Current Time (Media Player): ' num2str(currentTimeMedia) ' seconds']);
% Release the media player object
In this code, you first create a VideoReader object using the path to the video file. Then, you read the first frame to obtain the frame rate of the video. The CurrentTime property of the VideoReader object provides the current time in frames. By dividing this value by the frame rate, you can calculate the current time in seconds.
Next, the video file is opened in a media player using the vision.VideoPlayer object. The while loop reads frames from the media player and displays them using the step function. The CurrentTime property of the media player object gives you the current time in seconds.
Both the VideoReader and media player time values are displayed using the disp function for comparison.
  1 Comment
Life is Wonderful
Life is Wonderful on 25 May 2023
Thanks @diwakar diwakar !! Absolutely clean solution and nicely articulated. Your suggestion help me to get match media player time
Thank you

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!