2
$\begingroup$

I have a set of about 100 .png images representing the evolution of the system I am studying: how do I create a video out of these images with Mathematica?

$\endgroup$
5
  • 3
    $\begingroup$ I would save myself some trouble and use ffmpeg or a similar tool instead of Mathematica. You will get much more control over the video quality, and it will be much faster, allowing you to experiment with quality settings without having to wait for a long time between runs. When I create a video with Mathematica, I usually export PNG frames, then combine them with a different program. $\endgroup$
    – Szabolcs
    Commented Oct 22, 2019 at 10:02
  • $\begingroup$ Not a video, but you can save animated gif from MMA using Export command. $\endgroup$
    – Alx
    Commented Oct 22, 2019 at 11:52
  • $\begingroup$ @Alx You can export it as a video in the same way, see my answer. $\endgroup$
    – C. E.
    Commented Oct 22, 2019 at 17:31
  • $\begingroup$ @C.E., I know of course about exporting in video, but I agree with Szabolics' comment that video support in MMA is not so good as in external applications: in your answer what is codec, compresion level, quality etc.? So, OP should check if avi (or mov) is sufficient for his needs, otherwise it is better to use ffmpeg or the like applications to convert series of images to video. $\endgroup$
    – Alx
    Commented Oct 23, 2019 at 3:11
  • $\begingroup$ @Alx I was simply reacting to your comment that said "not a video, but you can save animated gif" which by people who don't know about video exporting might be construed to say that video exporting is not possible. $\endgroup$
    – C. E.
    Commented Oct 23, 2019 at 4:30

1 Answer 1

3
$\begingroup$

Here is a small example:

(* Export images *)
frames = Table[Plot[Sin[x + delta], {x, 0, 2 Pi}], {delta, 0, Pi/2, 0.01}];

MapThread[
  Export["~/Desktop/video_frames/" <> ToString[#] <> ".png", #2] &,
  {Range@Length@frames, frames}
  ];

(* Import them again *)
sortedFilenames = SortBy[
   FileNames["~/Desktop/video_frames/*.png"],
   ToExpression@First@StringCases[#, "/" ~~ n : DigitCharacter .. ~~ ".png" :> n] &
   ];
frames = Import /@ sortedFilenames;

(* Export video *)
Export["~/Desktop/example_video.avi", frames]

It all comes down to the last line. I suspect that you didn't realize that it is this simple:

Export["~/Desktop/example_video.avi", frames]

where frames is a list of images or a list of graphics.

$\endgroup$
2
  • $\begingroup$ How do I change the quality of the resulting video? $\endgroup$
    – Daniel
    Commented Feb 27, 2022 at 15:49
  • $\begingroup$ @Daniel I don’t have practical experience with tuning the quality so the best I can do is point you to the Options section in the documentation for the file format: reference.wolfram.com/language/ref/format/AVI.html Perhaps try changing the compression level. $\endgroup$
    – C. E.
    Commented Feb 27, 2022 at 19:50

Your Answer

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

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