Composite animations using mogrify and ffmpeg#

For this how-to we use an example of two rotating globes showing the projected 2m temperature anomalies from CMIP5 simulations, more specifically of the optimistic scenario RCP2.6 side by side with the pessimistic “business as usual” scenario RCP8.5 simulated with MPI-ESM. The composite and the video encoding can be done using the command-line tools mogrify and ffmpeg, which are readily available on Mistral.

image0

Figure 1: Composite image of 2m temperature anomaly data for the optimistic (RCP2.6) and pessimistic (RCP8.5) scenario from CMIP5 simulations.

Tools Used:

  • ParaView to render series of image files (PNG or JPEG)

  • mogrify to crop figures

  • ffmpeg to compose figures and generate animation (mp4 or gif)

Path to image series on Mistral: /work/kv0653/u241288/Compose-two-rotating-earth

Subdirectories:

  • rcp26

  • rcp85

  • rcp26_85_basic

Steps to take:#

  1. Copy & create folders

  • For this how-to, we suggest that you create a new directory in a folder where you have write access.

  • Copy folder rcp26_85_basic recursively to that directorycp –r rcp26_85_basic /<path_to_your_directory>/

  • Create empty directories: rcp26_cropped and rcp85_cropped: mkdir rcp26_cropped ;   mkdir rcp85_cropped

  1. Crop images to the required size

In this step we crop the sub-images for RCP2.6 (Fig. 2) and RCP8.5 (Fig. 4) from the original figures to the ones shown in Figs. 3 and 5.

image1

Figure 2: Sub-image for RCP2.6 before cropping

../../../../../../_images/rcp26.0040_cropped.png

Figure 3: Cropped sub-image for RCP2.6

../../../../../../_images/rcp85.0040.png

Figure 4: Sub-image for RCP8.5 before cropping

image2

Figure 5: Cropped sub-image for RCP8.5

Note that we have used distinct horizontal image cutouts for the RCP2.6 and RCP8.5 images. The globe is positioned further to the left in Figure 3 and further to the right in Figure 5.

Cropping can be done for single image files using the convert command and for an image file series using the command mogrify.:

mogrify -crop xSizexySize+xOffset+yOffset -path outputDirectory inputFiles

with xOffset and yOffset being the number of pixels relative to the most upper left corner.

To crop only a single file, change the command to convert::

convert -crop xSizexySize+xOffset+yOffset inputfile outputfile

The steps to take in practice:

  • Go to data directory rcp85

  • mogrify -crop 480x460+310+110 -path <your_path>/rcp85_cropped/ *.png

  • Go to directory rcp26

  • mogrify -crop 480x460+360+110 -path <your_path>/rcp26_cropped/ *.png

Please note that the cropped images will have been generated in the respective folders (rcp26_cropped and rcp85_cropped) with the same file names as the uncropped ones.

  1. Create animation from cropped images and background image series

Figure 6 shows our background image series on which the cropped images will be overlaid. All images of this series are available in the directory rcp26_85_basic. The relevant features from this background image series that we would like to see in the final animation are the colorbar, the colorbar label (2 meter Temperature) and the animted time annotation (here: 1991) that progresses with simulation time. The globe itself is not needed and will be covered properly by the cropped images; however, if the globe representation had been switched off while rendering the animation in ParaView, the colorbar would have been hidden as well.

image3

Figure 6: Background image series

Before running the following commands, make sure that your current directory is the one containing the three data folders rcp26_86_basic, rcp26_cropped and rcp85_cropped. The batch commands for creating an entire video are as follows: mp4 video::

/sw/rhel6-x64/graphics/ffmpeg-3.4.1-gcc64/bin/ffmpeg -i ./rcp26_85_basic/rcp26_ensm_LR_dtemp2_ym_rm10_1985-2095_n7_v_overlay.%04d.png -i ./rcp26_cropped/rcp26_ensm_LR_dtemp2_ym_rm10_1985-2095_n7_v_overlay.%04d.png -i ./rcp85_cropped/rcp85_ensm_LR_dtemp2_ym_rm10_1985-2095_n7_v_overlay.%04d.png -filter_complex "[0:v][1:v] overlay=110:110 [temp]; [temp][2:v] overlay=540:110" -q:v 0 rcp26_85.mp4

Animated gif::

/sw/rhel6-x64/graphics/ffmpeg-3.4.1-gcc64/bin/ffmpeg -i ./rcp26_85_basic/rcp26_ensm_LR_dtemp2_ym_rm10_1985-2095_n7_v_overlay.%04d.png -i ./rcp26_cropped/rcp26_ensm_LR_dtemp2_ym_rm10_1985-2095_n7_v_overlay.%04d.png -i ./rcp85_cropped/rcp85_ensm_LR_dtemp2_ym_rm10_1985-2095_n7_v_overlay.%04d.png -filter_complex "[0:v][1:v] overlay=110:110 [temp]; [temp][2:v] overlay=540:110" rcp26_85.gif

To generate a single output image, e.g. in the gif format, you would use the same command but replace all entries of %04d by the actual 4-digit consecutive number of the image files; for our example images this would be 0040. If you have cropped your images differently and would like to modify the ffmpeg command accordingly, the following explanations may of interest for you:

The command ‘ffmpeg’::

ffmpeg -i filename1.%04d.png -i filename2.%04d.png -i filename3.%04d.png -filter_complex "[0:v][1:v] overlay=110:110 [temp]; [temp][2:v] overlay=540:110" -q:v 0 rcp26_85_temp.mp4

Details:

-filter_complex: an option to do overlapping, following its parameters within “”
overlay=xOffset:yOffset indicate the overlapped position from the upper left.
[0:v][1:v]overlay[temp] : to overlay the second input on the first input and generate a temporal map named ‘temp’
[temp][2:v] : to overlay the third input on the former ‘temp’ map and generate the final map
-q:v 0: preserve the figure quality, the fixed quality scale (see details in ffmpeg official guide) for the video

More information on cropping images using the above ImageMagick commands:

https://deparkes.co.uk/2015/04/30/batch-crop-images-with-imagemagick/

ffmpeg documentation:

https://ffmpeg.org/ffmpeg.html