saveVideo: Convert a sequence of images to a video by FFmpeg

Description Usage Arguments Details Value Note Author(s) References See Also

View source: R/saveVideo.R


This function opens a graphics device to record the images produced in the code expr, then uses FFmpeg to convert these images to a video.


saveVideo(expr, = "animation.mp4", = "Rplot", 
    ffmpeg = ani.options("ffmpeg"), other.opts = if (grepl("[.]mp4$", "-pix_fmt yuv420p", ...)



the R code to draw (several) plots

the file name of the output video (e.g. ‘animation.mp4’ or ‘animation.avi’)

the file name of the sequence of images to be generated


the command to call FFmpeg (e.g. 'C:/Software/ffmpeg/bin/ffmpeg.exe' under Windows or 'avconv' on some linux machines); note the full path of FFmpeg can be pre-specified in ani.options('ffmpeg')


other options to be passed to ffmpeg, e.g. we can specify the bitrate as other.opts = '-b 400k' (The default "-pix_fmt yuv420p" is a work-around for a bug in some versions of ffmpeg.)


other arguments to be passed to ani.options


This function uses system to call FFmpeg to convert the images to a single video. The command line used in this function is: ffmpeg -y -r <1/interval> -i <>%d.<ani.type> other.opts

where interval comes from ani.options('interval'), and ani.type is from ani.options('ani.type'). For more details on the numerous options of FFmpeg, please see the reference.

Some linux systems may use the alternate software 'avconv' instead of 'ffmpeg'. The package will attempt to determine which command is present and set ani.options('ffmpeg') to an appropriate default value. This can be overridden by passing in the ffmpeg argument.


An integer indicating failure (-1) or success (0) of the converting (refer to system).


There are a lot of possibilities in optimizing the video. My knowledge on FFmpeg is very limited, hence the default output by this function could be of low quality or too large. The file ‘presets.xml’ of WinFF might be a good guide:


Yihui Xie, based on an inital version by Thomas Julou [email protected]


See Also

Other utilities: im.convert, saveGIF, saveHTML, saveLatex, saveSWF

animation documentation built on May 29, 2017, 10:59 a.m.