I have a sequence of graphs that are being created within Mathematica which I'm exporting to create an animated plot of the graph, as follows.
startTime = 0;
endTime = 5;
secondsPerFrame = 1/30;
sampleData = {#, #^3 - 5*#^2 + #} & /@ Range[startTime, endTime, secondsPerFrame];
{tList, xList} = sampleData\[Transpose];
timeInformationLocation = {0.2 + Min@tList, 0.9*Max@xList};
timeInformation =
Graphics[Text[
"Time: " <> ToString[NumberForm[N[#1], {10, 2}]] <> " s",
timeInformationLocation, {-1, 0}, BaseStyle -> {Black, 16}]] &;
evaluationInitialTime = AbsoluteTime[];
graphListForExport =
ParallelTable[
Show[ListPlot[Select[Transpose[{tList, xList}], #[[1]] < tdummy &],
PlotRange -> {1.1*{0, Max@tList}, 1.1*{Min@xList, Max@xList}},
AxesOrigin -> {0, 0}, AxesLabel -> {"time / s", "position / m"},
LabelStyle -> {Black, 16}, ImageSize -> 440,
PlotMarkers -> {Automatic, 6}],
timeInformation[tdummy]], {tdummy, startTime + secondsPerFrame,
endTime, secondsPerFrame}];
Print[Row[{"List of graphs completed at ", AbsoluteTime[] - evaluationInitialTime}]];
SetDirectory[NotebookDirectory[]];
Export["sample.mov", graphListForExport];
Print[Row[{"Export completed at ", AbsoluteTime[] - evaluationInitialTime}]];
Unfortunately, exporting the images is very slow; for the sample code above to export 150 frames, my system takes ~9s to generate the list of graphs, but another ~90s to export the list into the .mov format.
The question is, how can I optimize exporting a list of
Graphics
as a video?
There are several things which possibly could be done to improve the code. Firstly, when making the list of graphs, I could have chosen to use Graphics
instead, which is likely to be faster. However, as most of the time seems to be taken up in the export step, I would like to focus on that first.
Since I would be processing the video in other software afterwards, I considered encoding the video in another format. However, while saving the video as .avi
leads to some performance improvements (~60s for export versus ~90s), it is still rather slow. Other formats seem to be even slower than .mov, although I don't have the hard numbers for this specific example.
I also attempted to export the graphs immediately as a list of images that I could string together again via ffmpeg
. Again, there was no significant performance improvement, and considering the additional work that has to be done to produce the final video, I'm not sure whether it is worth it.
Halirutan suggested in chat to rasterize the images first, and YvesKlett added to that by pointing out that the process of rasterization could in fact be parallelized. However, when I attempt to rasterize the images in my system my system crashed. The two approaches I used to try to rasterize the image were inserting a Rasterize
within my ParallelTable
and inserting the code graphListForExport=Rasterize\@graphListForExport
.
For my final application, I will be exporting about 30s worth of graphs at 60fps, which means a total of 1800s. Each export takes about an hour, meaning that modifying elements in the video is very time-consuming.
I'm running Mathematica 9.0 on a system with a Dual Core 1.86 Ghz CPU, and 2.50 GB of RAM.
$HistoryLength=0
? That will help you RAM-wise. $\endgroup$