我有一些工作可以将一系列 png 转换为 mp4,然后使用 media9 包将其嵌入。它在 Windows 上的 Adobe Reader 中可以正常播放,但由于某种原因在 Linux Mate 上运行的 Okular 上不起作用。话虽如此,我也没有尝试过嵌入电影的任何其他 pdf,所以这似乎是读者方面的一个问题。
这是.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}
注意它必须有results='asis' 才能工作,否则一切都在挂钩中完成。