At rOpenSci we are developing on a suite of packages that expose powerful graphics and imaging libraries in R. Our latest addition is av – a new for working with audio/ based on the FFmpeg libraries. This ambitious new project will become the video counterpart of the magick package which we use for working with images.


The package can be installed directly from CRAN and includes a test function av_demo() which generates a demo video from random histograms.

Why AV in R?

One popular application is animating graphics by combining a sequence of graphics into a video. The animation and gganimate packages have many great examples. However up till now these packages would have to shell out to external software (such as the ffmpeg command line program) to generate the video. This process is inefficient and error prone and requires that the correct version of the external software is installed on the user/server machines, which is often not the case.

The av package takes away this technical burden. It uses the same libraries as FFmpeg, however because we interface directly to the C API, there is no need to shell out or install utilities. Everything we need is linked into the R package, which means that if the package is installed, it always works.

FFmpeg provides a full-featured video editing library, and now that the core package is in place, we can take things a step further. For example you can already enhance an animation with an audio track (to narrate what is going on or show off your karaoke skills) or apply one of the 100+ built-in video filters. In future versions we also want to add things like screen capturing and reading raw video frames and audio samples for analysis in R.

Create video from images

The av_encode_video() function converts a set of images (png, jpeg, etc) into a video file with custom container format, codec, fps, and filters. The video format is determined from the file extension (mp4, mkv, flv, gif). Av supports all popular codecs and muxers (codecs compress the raw audio/video and a muxer is the container format which interleaves one or more audio and video streams into a file).

# Create some PNG images
png("input%03d.png", width = 1280, height = 720, res = 108)
for(i in 1:10){

Create video from graphics

The example above illustrates how to generate encode a set of png images into a HD video. For generating a video from the R graphics, av includes av_capture_graphics() – a convenient wrapper which automatically opens and closes the graphics device and then encodes the video:


Using av with gganimate

The latest version of the super cool gganimate package already has built-in support for rendering video with av by using the av_renderer() output function. It is not yet on CRAN so you need to install from GitHub:

# Install gganimate
devtools::install_github("thomasp85/gganimate", "thomasp85/transformr")

Try this example animation:


# Create the gganimate plot

Check the gganimate repo for many cool examples.


AV also allows for adding a custom video filter chain. For example this will use the same animation as above, then negate the colors, and apply an orange fade-in effect to the first 15 frames.

# Continue on the example above

Note that filters can affect the number of frames and final framerate of the video. Below the same example as we had earlier, however now we added a vfilter which increases the framerate of the video from 1 to 10 by interpolating the intermediate frames, which results in a smoother transition between the frames:

makeplot offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook

Source link


Please enter your comment!
Please enter your name here