2

I have thousands of 30sec/20fps/.avi videos (so 600 frames total per video). I need to automate subsampling these videos in order to save every 100th frame (every 5 seconds). Any picture format is fine.

Is there an easy way to do this in either Matlab (R2015b) or Python+libraries?

Austin
  • 6,921
  • 12
  • 73
  • 138

2 Answers2

3

In python, you could use scikit-video combined with numpy to do this:

import skvideo.io

def subsample_and_write(filename, out_filename, n_steps):
    video_mat = skvideo.io.vread(filename)  # returns a NumPy array
    video_mat = video_mat[::n_steps]  # subsample
    skvideo.io.vwrite(out_filename, video_mat)
Geoffrey Negiar
  • 809
  • 7
  • 28
1

In MATLAB:

you can use VideoWriter object or imwrite, depends on the desired output format:

vin = VideoReader('vid1.mp4');
vout = VideoWriter('vid-out.mp4');
framenum = 0;
everyNframe = 100;
vout.open();
while vin.hasFrame
    frame = vin.readFrame;
    if rem(framenum,everyNframe) == 0
        vout.writeVideo(frame);
        % OR
        imwrite(frame, [num2str(framenum,'%04i') '.jpg']);
        disp(framenum)
    end
    framenum = framenum + 1;
end
vout.close();

another option the ffwd the input video to the next desired frame is by setting vin.CurrentTime, but for some reason it is slower than simply read 100 frames.

user2999345
  • 4,195
  • 1
  • 13
  • 20