0

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?

solution

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.

matheorem
  • 17,132
  • 8
  • 45
  • 115
  • 1
    Do your paths / $Paths match? – Yves Klett Nov 15 '15 at 16:17
  • @Yves Klett yeah. Run["del file"] works well – matheorem Nov 15 '15 at 16:27
  • 3
    ass @YvesKlett points out, you need to make sure that you reference the absolute path. – elbOlita Nov 15 '15 at 19:39
  • Why do you refer to the movie file as "movie01.png" in Export and as "movie%02d.png" in Run? – m_goldberg Nov 15 '15 at 23:02
  • @m_goldberg This is the command of ffmpeg. see "https://ffmpeg.org/ffmpeg.html#Options" search "image2" you will find this. – matheorem Nov 16 '15 at 00:33
  • @elbOlita I tried not working. and I don't think this is the point. Since Run["del file"] operate in the right directory. BTW, command like Run["ffmpeg -i movie.mov -c:v libx264 goo.mp4"] will convert correctly. – matheorem Nov 16 '15 at 00:41
  • I believe this is some type of bug. If you try running Import["!node -p console.log('test')", "Text"] you get a similar non response. You could probably work around this with Java. – William Nov 16 '15 at 01:07
  • @William I think "%" is suspicious, but I don't know how to solve this – matheorem Nov 16 '15 at 01:36
  • @matheorem Ignoring %, if it works in cmd but not in Mathematica then I think it fair to say this is some type of bug. I'm unlikely to post a fix but it is probably likely Java or .NET will fix this. – William Nov 16 '15 at 01:40
  • @William I have no idea about java, Would you like to post an answer if you are convenient? – matheorem Nov 16 '15 at 01:46
  • @matheorem This has been a pet peeve of mine so a hacked together a partial solution. – William Nov 16 '15 at 02:14
  • It appears we are having different issues apologizes if hope my posted solution doesn't work appropriately. http://mathematica.stackexchange.com/questions/99564/run-command-correct-output – William Nov 16 '15 at 04:53
  • Perhaps the second section of this answer can help to rewrite your Run argument in the correct form. – Alexey Popkov Nov 16 '15 at 12:46

1 Answers1

2

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

Needs["NETLink`"];
InstallNET[];
Unprotect[Run];
Run[x___] := Module[{t, p},
   LoadNETType["System.IO.Directory"];
   Directory`SetCurrentDirectory[Directory[]];
   t = NETNew["System.Diagnostics.Process"];
   t@StartInfo@FileName = "C:\\Windows\\System32\\cmd.exe";
   t@StartInfo@Arguments = "/c " <> StringJoin@Riffle[{x}, " "];
   t@Start[];
   t@WaitForExit[];
   t@ExitCode];
Protect[Run];
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);
    process.Start(); 
  }
}

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"]
William
  • 7,595
  • 2
  • 22
  • 70
  • compile shows cannot find “Wolfram.NETLink.dll”? – matheorem Nov 16 '15 at 02:37
  • @matheorem Now try it. Sorry it is how I compile .NET links when starting a kernel from .net – William Nov 16 '15 at 02:50
  • This is great ! Thank you! upvoted. But still wish mma could fix this annoying bug – matheorem Nov 16 '15 at 02:53
  • @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. – William Nov 16 '15 at 02:54
  • I don't understand. You mean it has something to do the disappearance of the cmd window? – matheorem Nov 16 '15 at 02:56
  • @matheorem Ignore what I said, honestly I'm not sure how it is implemented. I plan to send them a ticket/bug report at some point because this is an annoying bug especially because it gives no error and just doesn't run. Import["!echo test", "Text"] doesn't display a command prompt window so I believe that gestures that it is implemented differently then the way .NET is implemented above, but surprisingly Run["echo test"] does display a command propmt window so simply put I'm not sure. – William Nov 16 '15 at 02:59
  • again, I have no idea about .Net link, seems it is much better, really hopes you have time :) – matheorem Nov 16 '15 at 03:00
  • @matheorem It is about as good as its going to get. I added the bug tag to your question. Also filled a bug. – William Nov 16 '15 at 03:31
  • Does this fix your issue or not it still isn't clear to me. Do you mind uploading a video somewhere so I can try to reproduce the bug? – William Nov 16 '15 at 04:47
  • You know what, I am really confused right now. Since you asked me to provide an example to show the bug, it is actually shown in my question, so I run it again. But surprisingly, it runs without any problem now. I don't know what is going on. It didn't run properly before. And finally what I am sure is that your both methods in this answer works. Thanks for your answer, maybe I can catch this bug again later. – matheorem Nov 16 '15 at 05:50
  • and for your problem. I run Import["!echo test", "Text"], it didn't prompt a cmd window, but it did import the string. I am not sure if this is what you want – matheorem Nov 16 '15 at 05:51
  • I got another case, it seems that your Run.exe is not working this time. first, download http://www.7-zip.org/a/7za920.zip it is 7-zip command line. After adding it to the windows environment, I tried this Run["7z a -tgzip d:\\calc\\test.m.gz d:\\calc\\test.m"] not working. While in cmd, this is working. Then I tried your method Run["Run.exe 7z a -tgzip d:\\calc\\test.m.gz d:\\calc\\test.m"] still no .gz generated. – matheorem Nov 16 '15 at 06:31
  • Oh, my god. I finally understand what is going on. After I add new exe to windows environment. I have to restart mathematica, if not, it just doesn't recognize the new command. That is why I failed at first, but now it is OK after restart mma – matheorem Nov 16 '15 at 06:41