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?

  • 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 Oct 22 '19 at 10:02
  • $\begingroup$ Not a video, but you can save animated gif from MMA using Export command. $\endgroup$ – Alx Oct 22 '19 at 11:52
  • $\begingroup$ @Alx You can export it as a video in the same way, see my answer. $\endgroup$ – C. E. Oct 22 '19 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 Oct 23 '19 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. Oct 23 '19 at 4:30

Here is a small example:

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

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

(* Import them again *)
sortedFilenames = SortBy[
   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.


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.