Build videos using every Nth image from original video
Afficher commentaires plus anciens
I have a high speed video of a spinning object. The camera is triggered by the object position and it takes 20 pictures for each trigger. So I want to extract every 20th frame to a separate video as that will give me separate videos of the different positions around the object. I expect the input video to have around 4,000 frames.
What I want is to create one video with frames 1, 21, 41... another with frames 2, 22, 42... etc., up to frames 8, 28, 48...
Here is my current code. Is there a better way to do this?
% variables for extracting fixed position videos
numImagesPerTrigger = 20;
numLocsToOutput = 8;
%% Code to create a movie with every Nth image
% Get properties of the video
vidObjIn = VideoReader(InputFileName);
numFrames = vidObjIn.NumberOfFrames;
FrameRate = vidObjIn.FrameRate;
% Must reopen the video after getting its properties.
vidObjIn = VideoReader(InputFileName);
% Create separate videos for each part
for Offset = 0 : numLocsToOutput - 1
% Create the output file
OutputFileName = char(string(SelectedFilePath) + ...
string(InputFileBaseName)+ '-(' + Offset + ')' + ...
string(InputFileext));
vidObjOut = VideoWriter(OutputFileName, 'MPEG-4');
vidObjOut.FrameRate = 1;
open(vidObjOut);
% Select all frames at this location
for Frame = Offset : numImagesPerTrigger : numFrames - 1
vidObjIn.CurrentTime = (Frame) / FrameRate;
vidFrame = readFrame(vidObjIn);
writeVideo(vidObjOut, vidFrame);
end
% Clean up output file
close(vidObjOut)
end
It seems to me that the line:
vidObjIn.CurrentTime = (Frame) / FrameRate;
is cumbersome. I am concerned that it is possibly prone to error if there are a large number of frames. I would prefer to select the actual frame number directly.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Audio and Video Data dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!