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

13 ビュー (過去 30 日間)
Life is Wonderful
Life is Wonderful 2023 年 5 月 24 日
コメント済み: Life is Wonderful 2023 年 5 月 25 日
Hi
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

採用された回答

Diwakar Diwakar
Diwakar Diwakar 2023 年 5 月 24 日
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']);
end
% Release the media player object
release(mediaPlayerObj);
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 件のコメント
Life is Wonderful
Life is Wonderful 2023 年 5 月 25 日
Thanks @diwakar diwakar !! Absolutely clean solution and nicely articulated. Your suggestion help me to get match media player time
Thank you

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by