I am trying to use external ffmpeg.exe to convert list of image files into mp4

a = Table[Plot[Sin[ω t], {t, 0, 10}, PlotRange -> {-1, 1}, ImageMargins -> .4], 
         {ω, .1, 2, .1}];

Export["movie01.png", a, "VideoFrames"];
Run["ffmpeg -f image2 -framerate 12 -i movie%02d.png -c:v libx264  movie.mp4"]

But the conversion is not done. If I run the exactly same ffmpeg code in windows CMD, it works. What is wrong with the above Run command?


It turns out that after adding new exe to the windows environment(in this case, ffmpeg.exe), we have to restart mathematica, or this new exe will not be recognized by mathematica.

  • 1
    $\begingroup$ Do your paths / $Paths match? $\endgroup$ – Yves Klett Nov 15 '15 at 16:17
  • $\begingroup$ @Yves Klett yeah. Run["del file"] works well $\endgroup$ – matheorem Nov 15 '15 at 16:27
  • 3
    $\begingroup$ ass @YvesKlett points out, you need to make sure that you reference the absolute path. $\endgroup$ – elbOlita Nov 15 '15 at 19:39
  • $\begingroup$ Why do you refer to the movie file as "movie01.png" in Export and as "movie%02d.png" in Run? $\endgroup$ – m_goldberg Nov 15 '15 at 23:02
  • $\begingroup$ @m_goldberg This is the command of ffmpeg. see "ffmpeg.org/ffmpeg.html#Options" search "image2" you will find this. $\endgroup$ – matheorem Nov 16 '15 at 0:33

Let's override the Run so it stops acting up.

Run[x___] := Module[{t, p},
   t = NETNew["System.Diagnostics.Process"];
   t@StartInfo@FileName = "C:\\Windows\\System32\\cmd.exe";
   t@StartInfo@Arguments = "/c " <> StringJoin@Riffle[{x}, " "];
Run["ffmpeg -f image2 -framerate 12 -i movie%02d.png -c:v libx264 movie.mp4"]

Someone is welcome to convert this to .NET link but as it stands the following works for me. This is the old code.

using System;
using System.Diagnostics;

public class Run{
 public static void Main(String[] arg) {
    string command = @"C:\Windows\System32\cmd.exe";
    Process process = new Process(); 
    process.StartInfo.FileName = command; 
    process.StartInfo.Arguments = "/c " + string.Join(" ",arg);

Compile it like so.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Run.cs

and run it like so in Mathematica

Run["Run.exe ffmpeg -f image2 -framerate 12 -i movie%02d.png -c:v libx264  movie.mp4"]
| improve this answer | |
  • $\begingroup$ compile shows cannot find “Wolfram.NETLink.dll”? $\endgroup$ – matheorem Nov 16 '15 at 2:37
  • $\begingroup$ @matheorem Now try it. Sorry it is how I compile .NET links when starting a kernel from .net $\endgroup$ – William Nov 16 '15 at 2:50
  • $\begingroup$ This is great ! Thank you! upvoted. But still wish mma could fix this annoying bug $\endgroup$ – matheorem Nov 16 '15 at 2:53
  • $\begingroup$ @matheorem I think it is a speed trade of(notice mathematica doesn't display a cmd window) maybe or at least they way they currently have it. I'm not really sure though. If I have time I'll try to convert it to a .NET link. $\endgroup$ – William Nov 16 '15 at 2:54
  • $\begingroup$ I don't understand. You mean it has something to do the disappearance of the cmd window? $\endgroup$ – matheorem Nov 16 '15 at 2:56

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