From GRASS-Wiki
Jump to navigation Jump to search

Q: How to construct high-quality MPEG-4 movies (animations) from a series of still frames?

Maris wrote:

Encode all .png files in directory to out.avi with 15 frames per second:

mencoder "mf:///path/to/files/*.png" -mf fps=15 -o out.avi # put -ovc here, see next lines
## for DivX - libavcodec MPEG 4 (DivX5), use:
-ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=1800 -ffourcc MP4S
## for XviD, use:
-ovc xvid -xvidencopts bitrate=1024 -xvidencopts pass=2 -xvidencopts cartoon
## for DivX4, use:
-ovc divx4 -divx4opts q=5
## for x264 use:
-ovc x264 -x264encopts pass=3 -x264encopts preset=veryslow -x264encopts tune=animation

Uses mplayer's encoder. Choose one of encoding formats and append to end of "mencoder" line. Unfortunately only way to know quality settings is by encoding, watching result and re-encoding with different params.

Notice - by default encoding with lavc will set video fourcc to FMP4, that can be decoded with ffdshow. Option -ffourcc MP4S will change it to MPS4 ("official" MS fourcc for mpeg4) and video will be playable on Windows by standard MS mpeg4 decoder. It may lead to side effects, if MS mpeg4 decoder is buggy. More info:

Other guides:

Q: How to construct high-quality WebM movies?

A: WebM is the container for the VP8 and VP9 codecs. VP8 approaches H.264 in quality but differs from H.264 in that it is both patent + license free to use. Likewise, VP9 is an alternative to HEVC/H.265. The audio channel, if used, is encoded using the Ogg Vorbis codec for VP8 or Opus codec for VP9. Google has been promoting WebM as the standard fallback video format for HTML5 and is moving YouTube content to it. Firefox. Opera, and Google Chrome web browsers support WebM playback out of the box without need for extra plugins.


First convert PNG or JPEG images to PPM format using the NetPBM tools.

This step is pretty fast, I wouldn't worry about trying to parallelize it.
for file in *.png ; do
   pngtopnm $file > `basename $file .png`.ppm
for file in *.jpg ; do
   jpegtopnm $file > `basename $file .jpg`.ppm

Next create a Y4M stream containing a concatenation of the raw PPM frames:

cat *.ppm | ppmtoy4m -F 2:1 > all_frames.y4m
  • The "-F 2:1" option above sets the frame rate to 2 frames per second.
  • The ppmtoy4m program comes from the mjpegtools package.
  • For a very high number of frames you may wish to skip this step and create a named FIFO or stdin pipe directly into vpxenc, as the raw Y4M file can become extremely large.

Finally, run the vpxenc encoder to create a high quality WebM video:

vpxenc all_frames.y4m -o my_movie.webm \
  --passes=2 --threads=4 \
  --best --target-bitrate=2000 --end-usage=vbr \
  --auto-alt-ref=1 --verbose \
  --minsection-pct=5 --maxsection-pct=800 \
  --lag-in-frames=16 --kf-min-dist=0 --kf-max-dist=360 \
  --static-thresh=0 --drop-frame=0 \
  --min-q=0 --max-q=60
  • Set "--threads=" in the above command to the number of cores on your CPU. (or one less than the number of cores if you have many)
  • If you are encoding to VP8 (and not VP9) consider adding --token-parts=2
  • If vpxenc complains about "Invalid image format" either add "-S 420jpeg" to the ppmtoy4m options or add "--profile=1" to the vpxenc options.
  • The vpxenc program comes from the vpx-tools package.
  • Encoder parameters help page

Q: How to generate an animated GIF, FLI, or MNG?

A: for animations of 300 frames or less, animated GIF, FLI, or MNG formats are smaller files and better quality (i.e., frames stored as PNG not JPEG). Create with gifsicle, ppm2fli, or for MNG use ImageMagick's 'convert'. With more than 300 frames the players usually have memory issues. Animated GIFs are playable in any web browser of course and also in OpenOffice.

# with convert, many GIFs into one animated GIF:
convert -verbose -delay 20 -loop 0 snapshot*.png animation.gif

Notes: '-delay 20' means 20 hundreds of a second delay between each frame. The '-loop 0' flag lets it loop indefinitely. To loop only 3 times use '-loop 3' etc.

Sample script to export PNGs in GRASS (generates tmean_001.png .. tmean_708.png):

   export i=0
   for year in `seq 1950 2008` ; do
       for m in `seq 1 12` ; do
           i=`expr $i + 1`
           # awk trick to generate wildcard-correct file names:
           i=`echo $i | awk '{printf "%03d\n", $1}'`
           r.out.png tmean_europe.$year.$m.avg out=tmean_$i.png
    convert -verbose -delay 20 -loop 0 tmean_*.png tmean_animation.gif

For presentations using a web browser, you can center the image on a blank white page, turn off any toolbars and go into full screen mode (F11 for Firefox or Chrome). Then Alt-Tab your way to the animation at the appropriate time and nobody knows you are using a web browser.

Example HTML for centering image:

  <IMG SRC="animation.gif">

Q: How to create dynamic surface movies in NVIZ?

A: See slides from the FOSS4G 2006 workshop:

See also the example at the bottom of the NVIZ keyframe animator panel help page.

Q: How to animate a time series of raster maps?

A: Use

  • to view in a GRASS xmon (see d.mon)
  • r.out.mpeg to save to a MPEG-1 movie.
  • GRASS GIS 6: xganim to view on screen in X-windows (with live start/stop/speed controls)
  • GRASS GIS 7: g.gui.animation to view on screen in wxGUI (with live start/stop/speed controls)
See also the wiki page for the wxGUI Animation Tool:
YouTube video introduction to the WxGUI Animation Tool

Q: How to animate a series of maps, including decorations?

A: Use the xmon drivers (d.mon) to render the displays to a window via a shell script loop, then switch from the x0 to PNG driver to save as a series of PNG or PPM images. Use a method answered in a FAQ above to combine those into a single animation.

Q: How to create a screencast (for video tutorials etc)?

  • Easist way: recordmydesktop (user interfaces are 'qt-recordmydesktop' and 'gtk-recordmydesktop')
  • when uploading videos to Youtube created by recordmydesktop you need to encode the file eg. using mencoder
- No sound, just video screencast, as AVI file:
mencoder out.ogv -nosound -ovc lavc -o out.avi
- With audio track, as AVI file:
mencoder out.ogv -ovc xvid -oac mp3lame -xvidencopts pass=1 -o out.avi
- With audio track, as WebM file:
ffmpeg -i out.ogv -b:a 24k out.webm
- With audio track, as MP4 file:
ffmpeg -i out.ogv -ar 44100 -ab 96k -coder ac -strict experimental -vcodec libx264 out.mp4
MS Windows


Using the Spearfish sample dataset

Animate on screen

Using a shell script

  d.mon start=x0

g.region rast=elevation.dem n=4927830 s=4912980

for NUM in `seq 1 0.5 10` ; do
   d.rast elevation.dem --quiet
   d.vect roads
   d.vect bugsites icon=basic/circle col=black fcol=green size=`echo "21-($NUM * 2)" | bc`

   echo "symbol basic/arrow2 25 80 `echo "10*$NUM" | bc` 80:80:80 125:125:255" | d.graph

   echo "Spearfish, South Dakota" | d.text size=4.5 at=1,2.25
   d.barscale at=59.8,93.6 tcol=grey
Animate to files

Using GRASS's PNG driver

# set up base map
g.region rast=elevation.dem n=4927830 s=4912980
export GRASS_WIDTH=640
export GRASS_HEIGHT=500
export GRASS_PNGFILE=base.ppm
d.mon start=PNG
  d.rast elevation.dem
  d.vect roads
  echo "Spearfish, South Dakota" | d.text size=4.5 at=1,2.25
  d.barscale at=59.8,93.6 tcol=grey
d.mon stop=PNG

# loop to draw variable parts as new frames
for NUM in `seq 1 0.5 10` ; do
   i=`expr $i + 1`
   FRAMENUM=`echo $i | awk '{printf("%03d", $1)}'`
   echo "Processing frame $FRAMENUM ..."
   export GRASS_PNGFILE="ganim_${FRAMENUM}.ppm"
   cp base.ppm "$GRASS_PNGFILE"
   d.mon start=PNG --quiet
   d.vect bugsites icon=basic/circle col=black fcol=green \
      size=`echo "21-($NUM * 2)" | bc`

   echo "symbol basic/arrow2 25 80 `echo "10*$NUM" | bc` 80:80:80 125:125:255" | d.graph
   d.mon stop=PNG --quiet

# repeat the last frame a few times
for EXTRA in 1 2 3 ; do
 i=`expr $i + 1`
 FRAMENUM=`echo $i | awk '{printf("%03d", $1)}'`
 cp base.ppm "ganim_${FRAMENUM}.ppm"
\rm base.ppm
Encode as animated GIF

A) Using gifsicle:

Advantage: the delay is coded in the animated GIF file and rendered properly e.g. in a Web browser.

# Imagemagick's <tt>convert</tt> program may do a better job at 
# 256 color quantization than NetPBM's <tt>ppmquant</tt>.
for IMG in ganim*.ppm ; do   # convert each frame from PPM to GIF
   #ppmquant 256 $IMG | ppmtogif > "`basename $IMG .ppm`.gif"
   convert "$IMG" "`basename $IMG .ppm`.gif"

gifsicle -O2 --delay 20 --no-loopcount --colors 256 ganim_*.gif > ganim.gif

\rm ganim_0*.gif

B) Using ImageMagick's convert and animate:

# Generate "film" (create animated GIF):
convert lst_monthly_avg_film.00*.png lst_monthly_avg_2002_2012_zoom_film.gif

# play animation (delay 50millisec between frames, pause for 5 seconds at the end)
animate -delay 50 -pause 5 lst_monthly_avg_2002_2012_zoom_film.gif
Encode as animated PNG (MNG)
  • Using the ImageMagick "convert" encoder
  • View with ImageMagick's "display"
convert -delay 20 ganim_*.ppm ganim.mng
Encode as FLI
ls ganim_*.ppm > frames.txt
ppm2fli -g"${GRASS_WIDTH}x${GRASS_HEIGHT}" -O -s 15 frames.txt ganim.fli
\rm frames.txt
Encode as Flash
png2swf -o outfile.swf *.png -r 1
Encode as MPEG-4 Xvid
# doesn't like GRASS's PPMs, convert to PNG
for IMG in ganim*.ppm ; do
  pnmtopng $IMG > "`basename $IMG .ppm`.png"
mencoder "mf://ganim_*.png" -mf "type=png:fps=5" -o ganim.avi \
   -ovc "xvid" -xvidencopts "bitrate=1024"
\rm ganim_0*.png

Publish your cool videos in this Wiki

We have installed a Mediawiki widget extension and created a special template for including Youtube videos (Template:YouTube). See also wxGUI Modeler page for the usage.

The overall goal is to have many as possible GRASS GIS videos on YouTube