How to lower frame rate of a video without changing the duration of the video

5 views (last 30 days)
i want to lower the frame rate of a video without actually changing the duration of the video, i realise that this means i am losing data in the process but i need this for a specifc task, i tried doing on my own but it kept resulting in a lower frame rate however it added to the duration of the vid, my code:
function [] = tests(frameRate)
vid = VideoReader('anyVideo.mp4', 'tag', 'myreader1');
newVid = VideoWriter('NewVid.avi');
newVid.FrameRate = frameRate;
open(newVid);
numFrames = vid.NumberOfFrames;
vidTime = numFrames/Vid.FrameRate;
newNum = vidTime * frameRate;
steps = numFrames / (numFrames - newNum); % Max Count
disp(steps)
ceil(steps);
count = 1;
for frame = 1 : numFrames
% Extract the frame from the movie structure.
thisFrame = read(vid, frame);
if count < steps
writeVideo(newVid,thisFrame);
count = count + 1;
else
count = 1;
end
end
close(newVid);
disp(newVid.FrameCount)

Accepted Answer

Geoff Hayes
Geoff Hayes on 26 Apr 2020
Ahmed - since you are downsampling, can't you use the sampling frequencies to determine which frames to extract from your original video file? For example,
% assume frameRate < vid.FrameRate
frameToExtract = ceil(vid.FrameRate/frameRate);
for frame = 1 : numFrames
% Extract the frame from the movie structure.
thisFrame = read(vid, frame);
if frame == 1 || mod(frame - 1, frameToExtract) == 0
writeVideo(newVid,thisFrame);
end
end
We extract the first frame and then every frameToExtract after. I haven't tested the above but I think that it should work (or produce a reasonable extraction). This differs from your code in that we extract every nth sample rather than consecutive samples (if I'm reading your code correctly).
  5 Comments
Akshaya Srinidhi Vijayareka
Hi !
Thank for this code.
I'm trying to reeduce the frame rate of a video. I'm trying out your code. But i'm not able to read the output video file saved in the path. Can you help me with this ?
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!