I've got something working that converts a series of pngs to mp4 and then embeds it using the media9 package. It plays back fine in Adobe Reader on Windows, but for some reason doesn't work on Okular running on Linux Mate. Having said that, nor do any other pdfs I've tried with embedded movies, so it seems to be an issue on the reader side.
Here's the .Rnw:
\documentclass{article}
\usepackage{media9}
\usepackage{graphicx, xcolor}
<< label = setup, include = FALSE>>=
library("rgl")
library("car")
library("knitr")
hook_rgl_spin_mp4 <- function(before, options, envir) {
if (!before) {
par3d(windowRect = 100 + options$dpi *
c(0, 0, options$fig.width,
options$fig.height))
if (!is.null(options$spin3d.axis)) {
spin3d.axis <- options$spin3d.axis
} else {
spin3d.axis <- c(0, 0, 1)
}
if (!is.null(options$spin3d.rpm)) {
spin3d.rpm <- options$spin3d.rpm
} else {
spin3d.rpm <- c(0, 0, 1)
}
spinFunc <- spin3d(axis = spin3d.axis, rpm = spin3d.rpm)
for(i in 1:options$fig.num) {
par3d(spinFunc(i * options$interval))
Sys.sleep(0.05)
rgl.snapshot(fig_path(".png", number = i), fmt = "png")
}
system(paste0('ffmpeg -y -r 10 -i "', sub("-1\\.", "-%d\\.", fig_path(".png", number = 1)),
'" -pix_fmt yuv420p "', vid_path <- fig_path(".mp4", number = 1), '"'))
options$fig.num <- 0
options$fig.show <- "hide"
paste0("\\includemedia[width=", options$out.width, ",height=", options$out.height,
",activate=pageopen,addresource=",
vid_path, ",flashvars={source=", vid_path, "}]{}{VPlayer.swf}")
}
}
knit_hooks$set(rgl = hook_rgl_spin_mp4)
@
\begin{document}
<< label=rgl1, rgl=TRUE, fig.show='animate', fig.width=5, fig.height=5, out.width='.6\\linewidth', out.height='.6\\linewidth', dev='png', fig.num = 40, interval=0.1, spin3d.axis=c(0, 0, 1), spin3d.rpm=20, results='asis'>>=
scatter3d(prestige ~ income + education, data=Duncan)
@
\end{document}
Note it has to have the results='asis'
to work, but otherwise everything is done in the hook.