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

조회 수: 5 (최근 30일)
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)

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 4월 26일
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
Akshaya Srinidhi Vijayareka
Akshaya Srinidhi Vijayareka 2021년 5월 5일
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!
Geoff Hayes
Geoff Hayes 2021년 5월 5일
Akshaya - please clarify what code you are using and what the error message is.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by