Creating and extracting time-stamps from an MP4 video.

41 views (last 30 days)
I am trying to correlate 2 sets of data obtained from the frames of a MP4 video. Issue is that each of these data sets end at a different frame number - i.e. one continues till the last frame of the video whereas the other terminates at frame 1000. Because of this, I cannot use frame count to align the data. Instead, it seems best I extract time-stamps for reference when comparing the different length data sets. See the code below that I have written for extracting the time-stamps (I have also included it as an m.file). However I am getting this error returned: 'Error using extract_timestamps, Could not extract total duration from video'.
function timestamps = extract_timestamps(video_path)
% extract_timestamps extracts timestamps of each frame from an MP4 video
% INPUT
% video_path - string, path to the MP4 video
% OUTPUT
% timestamps - vector of timestamps (in seconds) for each frame
if ~ischar(video_path)
error("video_path must be a text scalar (i.e. a string)");
end
% Extract the video information using FFmpeg
[~. video_info] = system(['ffmpeg -i ' video_path ' 2>&1 | grep "Duration"']);
fprintf("video_info: %s\n", video_info);
% Extract the total duration of the video
total_duration = sscanf(video_info, 'Duration: %d:%d%f');
if isempty(total_duration)
error("Could not extract total duration from video");
end
total_duration = total_duration(1) * 3600 + total_duration(2) * 60 + total_duration(3);
% Extract the number of frames in the video
[~, video_info] = system(['ffmpeg -i ' video_path ' 2>&1 | grep "Video"']);
num_frames = sscanf(video_info, 'Video: ., %d');
% Calculate the duration of each frame
frame_duration = total_duration / num_frames;
% Generate the timestamps for each frame
timestamps = (0:num_frames-1) * frame_duration;
end

Accepted Answer

Sachin
Sachin on 14 Feb 2023
Edited: Sachin on 14 Feb 2023
Hi David,
I understand that you are trying to create and extract time-stamp from an MP4 video. Referring the following information might be of good help to you.
  1. Install the “FFmpeg” in your machine.
  2. After installation “FFmpeg binary directory to" the path.
  3. After this you can use “FFmpeg” in your machine.
  4. To install and check if "FFmpeg" is installed or not in your machine, you can refer the following page.
  5. https://www.wikihow.com/Install-FFmpeg-on-Windows
  6. If you are using windows you need to change "grep" to “findstr” because cmd doesn’t cover "grep" syntax as it works on Linux. You can refer the following page for more information about "grep".
[~, video_info] = system(['ffmpeg -i ' video_path ' 2>&1 | findstr "Duration"']);
7. If are using linux system then you don’t need to change the code. You can refer this page for more information about grep.
8. Since you are using “sscanf” make sure second argument is correct as video_info contains. Because in video_info there are some blank spaces before Duration so we have to take care of this also. See below -
video_info: Duration: 00:00:30.80, start: 0.000000, bitrate: 141 kb/s
total_duration = sscanf(video_info, ' Duration: %d:%d:%f')
9. If you still face any error in sscanf then you can use split function in Matlab to split the video_info as shown below.
duration = split(video_info,",");
duration = string(duration(1))
total_duration = sscanf(duration, ' Duration: %d:%d:%f')
Regards
Sachin

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!