4
$\begingroup$

I want to extract images from an avi file with MathMF from https://github.com/SimonWoods/MathMF.

(please see here the answer of Simon)

A test avi file (named "20181122_test.avi") can be downloaded here: https://drive.google.com/open?id=1ek87v3mLXRHAv51jW4967vQLIO-Z-Any

Let's consider only two lines:

Line 1. Needs["MathMF"];` (to load the package)

This works and the package is loaded.

Now I want to read the number of images in the file:

Line 2. numberImages = Length@Import["20181122_test.avi", "Frames"]

That produces an error:

enter image description here

If I don't load the MathMF package BEFORE, then I get the correct result:

numberImages = 
 Length@Import["20181122_test.avi", "Frames"]

5

The funny thing is NOW I can again execute Needs["MathMF"]` and AFTER that I can read the number of images in the file WITHOUT error.

Can somebody reproduce the error? What is different between Mathematica 11.3 and 11.1.1 and how can I execute with Mathematica 11.3 first line 1 and then line 2 without error?

My OS is Windows 10 Pro 64 bit and I am working with Mathematica 11.3.0.0

This error does not occur with Mathematica 11.1.1

$\endgroup$
  • $\begingroup$ @Simon Woods: do you have an idea where the problem might be? $\endgroup$ – mrz Nov 22 '18 at 17:14
  • 1
    $\begingroup$ I would try to find contact details outside of StackExchange. If you cannot, try to comment on one of his old answers and link to here. $\endgroup$ – Szabolcs Nov 28 '18 at 12:24
  • 1
    $\begingroup$ Judging from the error message it looks like Mathematica 11.3 is using Media Foundation (via MediaTools.dll) for the avi import (earlier versions didn't) and this is conflicting with the use of Media Foundation in my library. Most likely there is a bug in my code. I don't know C++ or COM well, so MathMF is probably not doing things quite the way they should be. $\endgroup$ – Simon Woods Nov 28 '18 at 22:02
  • 1
    $\begingroup$ 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. Unfortunately I don't have spare time to look into it any further right now. $\endgroup$ – Simon Woods Nov 28 '18 at 22:03
  • 1
    $\begingroup$ @mrz, see my answer in mathematica.stackexchange.com/a/186995/9239 $\endgroup$ – Piotr Wendykier Nov 29 '18 at 19:04
3
+25
$\begingroup$

Judging from the error message it looks like Mathematica 11.3 is using Media Foundation (via MediaTools.dll) for the avi import (earlier versions didn't) and this is conflicting with the use of Media Foundation in my library. Most likely there is a bug in my code. I don't know C++ or COM well, so MathMF is probably not doing things quite the way they should be.

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.

Update

Piotr Wendykier has provided an answer here showing how to use MediaTools for frame-by-frame reading.

$\endgroup$
1
$\begingroup$

Since the error is caused by loading MathMF, so I suggest modify the MathMF package file mannually further while executing Needs["MathMF'"], in order to do this, the package main file should be modified properly.

SystemOpen@FileNameJoin[{$UserBaseDirectory, "Applications"}]

then find the MathMF.m, open it with Notepad or other text editor enter image description here the content surrounded by red rectangle should be packed as a function following the package programming rules, see bellow likely

MFLoad[]:=Module[{},
    MathMFlib=FindLibrary["MathMF"];

    If[MathMFlib===$Failed,
	    Print["No MathMF.DLL found.\nMathMF will attempt to build the library from source."];
	    Module[{codefile,code},
	    codefile=FileNameJoin[ReplacePart[FileNameSplit[$InputFileName],-1->"MathMF.cpp"]];
    If[!FileExistsQ[codefile],
    Print["No source file found at "<>codefile<>"\nAborting MFMath."];Abort[]];
    code=Import[codefile,"Text"];
    Needs["CCompilerDriver`"];
    MathMFlib=CreateLibrary[code,"MathMF",
    "Language"->"C++",
    "CleanIntermediate"->True,
    "Libraries"->{"ole32.lib","mfreadwrite.lib","mfplat.lib","mfuuid.lib"}];
    If[!FileExistsQ[MathMFlib],
    Print["Library creation failed.\nAborting MFMath."];Abort[],
    Print["MathMF.DLL successfully created at "<>MathMFlib]]]];

    MFISR=LibraryFunctionLoad[MathMFlib,"InitSourceReader",{"UTF8String"},{Real,1}];
    MFGF=LibraryFunctionLoad[MathMFlib,"GrabFrame",{},{Integer,3}];
    MFSourceTime=LibraryFunctionLoad[MathMFlib,"SourceTime",{},Real];
    MFISW=LibraryFunctionLoad[MathMFlib,"InitSinkWriter",{"UTF8String",Integer,Integer,Integer,Real,Integer},"Void"];
    MFSF=LibraryFunctionLoad[MathMFlib,"SendFrame",{{Integer,3,"Constant"}},Integer];

Return[True];]

and add declaration in the usage section as

MFLoad::usage= "MFLoad[] load MathMF.dll and do preparation";

so that when Needs["MathMF'"] only loads packages functions but does not make initialization. Everytime you are going to use MathMF, you do MFLoad[] to call functions of MathMF or MFUnload[] to call the MMA meta-functions.

It's hard to use TracePrint to track the Import execution routing to find the error, so turn to MathMF author for help might be better.

$\endgroup$

Your Answer

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

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