8
$\begingroup$

In version 11.1.1 I used MathMF by Simon Woods to extract png images from large avi files (up to 100GB in size). His package was MUCH faster than using something like that:

nImages = Length@Import["D:\\movie.avi", "Frames"];

Do[
  image = Import["D:\\movie.avi", {"Frames", i}];

  strCounter = ToString@PaddedForm[i, 5, NumberPadding -> {"0", ""}];

  Export[StringJoin[outputPath, "image_", strCounter, ".png"], image, 
   "png"], {i, 1, nImages}
]; 

Already only reading the number of images of a large avi file of 6GB (containing 640*480 pixel images) takes a long time (45s). If somebody does not belive it, the file is available here: https://photos.app.goo.gl/1g2kffBySogQgtsS9).

The reading of a single image at a certain position i=1 takes also about 45 sec.

Why is this so slow? (I have 32 GB of RAM, processsor: i7-4940MX 3.1 GHz)

See this:

image = Import["D:\\movie.avi", {"Frames", 1}]; // AbsoluteTiming
{44.1149, Null}

FOR SMALL AVI FILES THIS PROBLEM IS "INVISIBLE"!

For comparison I took VirtualDub (for Windows) and opened the 6GB avi file. In less than a second it shows the first image and the total number of files (20159).

In versions 11.3 MathMF does not work any more (see this question and the comment of Simon).

Simon mentioned the following:

Looking at the MediaTools package it seems like it might do everything that MathMF can do anyway. If you do Needs["MediaTools`"] and then ?MediaTools`Private`$MF* there appear to be functions for frame-by-frame reading and writing.

If I execute ?MediaTools`Private`$MF* I get a list of fuctions. enter image description here

How can I substitute with these functions my upper code to increase the performance?

$\endgroup$
0

1 Answer 1

11
$\begingroup$

In version 11.3 for Windows you can do the following:

In[1]:= file = URLSave["http://mirrors.standaloneinstaller.com/video-sample/
Panasonic_HDC_TM_700_P_50i.avi", "c:\\tmp\\sample.avi"];

In[2]:= Needs["MediaTools`"]

In[3]:= MediaTools`Private`$MFInitReader[file]

Out[3]= True

In[4]:= AbsoluteTiming[i = MediaTools`Private`$MFReadNextFrame[];]

Out[4]= {0.0497098, Null}

In[5]:= MediaTools`Private`$MFFinalizeReader[]

Out[5]= True

In[6]:= ImageDimensions[i]

Out[6]= {1920, 1080}
$\endgroup$
8
  • $\begingroup$ I just now test the extaction with your code and it is very fast. In my upper code example I only modified the reading by: image = MediaToolsPrivate$MFReadNextFrame[];. With that about 10 images per second are extracted and saved. The only thing which I do not understand: when I want to know the contained total number of images in the avi I used: n = MediaToolsPrivate$MFReadVideoFrameCount[file];. The first time for my 6GB avi file it takes about 40sec (result: n = 20159). If I execute the same line a second time it is done in fraction of a second. Do you know why this is so? $\endgroup$
    – mrz
    Nov 30, 2018 at 10:52
  • $\begingroup$ When I replace Export[StringJoin[outputPath, "image_", strCounter, ".png"], image, "png"] by ImageImportExportDumpImageWritePNG[ StringJoin[outputPath, "image_", strCounter, ".png"], image]; in average 16 images per second are saved. $\endgroup$
    – mrz
    Nov 30, 2018 at 11:12
  • 2
    $\begingroup$ @mrz: MediaToolsPrivate$MFReadVideoFrameCount[file]; has to decode all frames when it is evaluated for the first time. The result is cached. Video file formats store only an approximate duration (= approximate number of frames) in the header. $\endgroup$ Nov 30, 2018 at 11:47
  • $\begingroup$ I understand. Without the help of Simon Woods and you, I would have never been able to solve my problem. If I search in the Mathematica help I do not find any documentation about the existence of the package MediaTools. Is there a possibility to list all of these „hidden“ functions? $\endgroup$
    – mrz
    Nov 30, 2018 at 12:08
  • 3
    $\begingroup$ We don't document internal paclets, but I will pass this idea. $\endgroup$ Nov 30, 2018 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.