4
$\begingroup$

I am trying to work with large numbers of frames from a video (30000+). I see there are a number of answers out there, but I find none of them satisfying - see below for details.

I understand from painful experience that loading all frames from a video into memory via import is a bad idea. Instead I am trying to first export all individual frames of the video via an import/crop/export for-loop and will work on individual frames.

I find that import times of individual frames behave very different depending on what frames I want to import:

Table[{x, AbsoluteTiming[Import[vidName, {"Frames", x}]][[1]]}, {x, 1, 30000, 1000}]

enter image description here

I tried using the ffmpeg plugin as suggested here, but can not get it to run.

Likewise the pattern of how import times scale with number of simultaneously imported frames does take on an exponential shape rather than what Karolis observed:

LoadNFrames[n_] := (Import[vid, {"Frames", Range[1, 1 + n]}]; n);
times = Table[AbsoluteTiming[LoadNFrames[n]], {n, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 50, 
75, 100, 150}}]

enter image description here

My question hence is: With importing behaving erratic like this, what is the single best way to extract images from videos to work on them? Do I really have to export frames in badges of 3 in a loop?

Thanks,

mondo

EDIT: ran my suggested single-frame import/crop/export loop and found some interesting pattern in the timing for each import:

enter image description here

Does this immediately make sense for anyone?

EDIT 2: I used the same method on another video. This time i get shuffled frame numbers - i.e., my exported frame number 5 fits after frame number 6, and before frame number 7 etc.

Any thoughts, anyone?

$\endgroup$
  • $\begingroup$ Wouldn't it be better to do the video to frame image conversion with a free external software such as VirtualDub? $\endgroup$ – rhermans Aug 18 '15 at 11:35
  • $\begingroup$ @rhermans Quite possible. Will look it up. Tested method as mentioned above overnight, see edit. $\endgroup$ – mondo Aug 19 '15 at 2:15
  • $\begingroup$ Added another edit. Exported frames seem to jump in sequence... $\endgroup$ – mondo Aug 19 '15 at 13:14
  • $\begingroup$ I think that this may be related to the compression algorithms that uses a sparse representation that depends on the data on previous frames. In order to extract 1 frame, you still need to read all the frames before. I bet an uncompressed video format would not have this behaviour. $\endgroup$ – rhermans Aug 19 '15 at 13:18
  • $\begingroup$ Thanks for the thought @rhermans. Currently using .mov format - can you suggest a format in which this should not present a problem? Alternatively, should external software like VirtualDub (unfortunately not available for Mac, but I am sure there are alternatives) circumvent this problem? $\endgroup$ – mondo Aug 19 '15 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.