video2pic
is an R-package that allows you to extract still frames from
a video. For efficiency purposes, the frame-extraction-algorithm has
been implemented in python.
Because the algorithm uses python in the background, you first need to install miniconda on your system. Please follow the instructions here to do so. Once you have installed miniconda, you can install the development version of video2pic from GitHub with:
# install.packages("devtools")
devtools::install_github("DavidDHofmann/video2pic")
Finally, you will also need to ensure that all necessary python packages
are installed. This can be done using the video2pic_install()
function.
library(video2pic)
video2pic_install()
This will create a virtual python environment called “video2pic” containing the required packages. You may need to restart your R-session after this!
The function to extract frames from a video is called video2pic()
. The
function allows you to specify an output directory and the number of
frames per second that should be extracted. Note that you could also
provide multiple videos at once, so that you don’t need to loop through
multiple files.
# Load required packages
library(video2pic)
library(magick)
# Initalize python environment
video2pic_initialize()
# Path to the example video
video <- system.file("extdata", "Earth.mp4", package = "video2pic")
# Extract one frame per second (and store them to a temporary directory)
video2pic(video, outdir = tempdir(), fps = 1)
#> Extracting frames...
#> | | | 0% | |=======================================================| 100%
#> Extracting done...
# The extracted images can now be found here
imgs <- dir(tempdir(), pattern = ".JPG", full.names = T)
# We can visualize some of them if we want
image_read(sample(imgs, 4))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.