R中高清动画视频的最佳实践

8

目标是制作一个视频,可以在1280 x 800分辨率的屏幕上全屏播放两个垂直排列的ggplot图表,使用grid.arrange()命令。例如:

library(ggplot2)
library(gridExtra)
library(animation)
saveVideo({
  for (i in 1:50) {
    data = data.frame(x=rnorm(1000),y=rnorm(1000))
    plot1 = ggplot(data, aes(x=x, y=y)) + geom_point()
    plot2 = ggplot(data, aes(x=y, y=x)) + geom_point()

    grid.arrange(arrangeGrob(plot1, plot2, heights=c(3/4, 1/4), ncol=1))

    ani.options(interval = 0.05, ani.dev="png", ani.height=800)
  }
},video.name = "test_png.mp4", other.opts = "-b 1000k")

视频的画质无法满足全屏观看的要求。我尝试增加 "-b 1000k",但似乎只会增加文件大小和输出的清晰度没有提高。
  • 我应该使用哪种设备?
  • 如何增加画布的高度(ani.height=800 似乎没有任何效果)?

编辑:我尝试了选项 other.opts = "-s 1280x800" 的脚本。虽然图像现在更宽了,但清晰度仍然很低。下面是从我的 1280x800 显示器上拍摄的截图(将菜单栏与视频进行比较):

enter image description here:


other.opts = "-s 1280x800"是什么意思? - lukeA
@lukeA。使用您的选项,框架的大小确实增加了,但是分辨率仍然太低......我还尝试了other.opts="-s 1280x800 -b 1000k",但没有效果。 - CptNemo
2个回答

6
你特别询问了动画包,但是如果你对我使用过并取得良好效果的另一种替代方法感兴趣,你可能想看看ffmpeg。
这是我使用igraph和ffmpeg组合制作的一个剪辑,并发布在YouTube上,支持最高2160p分辨率。

https://www.youtube.com/watch?v=Aga9UxMPuFA

这将是一个由PNG组成的剪辑,展示了两个ggplot()并排显示的图形:

https://www.youtube.com/watch?v=3A4qZdSf7bk

我使用的将捕获的PNG图像拼接在一起的ffmpeg命令是:
ffmpeg 
  -framerate 10     # input frame rate
  -i image%03d.png  # image names (image000.png, ..., image999.png)
  -s:v 1280x720     # video size
  -c:v libx264      # encoder
  -profile:v high   # H.264 profile for video
  -crf 20           # constant rate factor
  -pix_fmt yuv420p  # pixel format
  -r 30             # output frame rate
  clip.mp4   # clip file name

关于如何使用ffmpeg实现此目的的详细信息,您可以在此处找到: http://www.joyofdata.de/blog/hd-clips-with-ffmpeg-for-youtube-and-vimeo/


0

不要传递 ani.dev="png",而是可以传递 ani.dev = function(...){png(res=75*grain,...)},其中 grain 是某个大于 1 的数字。如果你将 ani.height(如果有的话还包括 ani.width)乘以相同的因子 grain,那么你就可以有效地将输出的像素分辨率增加这个因子。

注意:上面的默认分辨率 75 可能与机器有关,我没有看到它有文档记录。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接