-
Notifications
You must be signed in to change notification settings - Fork 37
/
ffmpeg_convert.R
42 lines (42 loc) · 1.18 KB
/
ffmpeg_convert.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#' Convert Files using FFMPEG
#'
#' @param file Video/PNG file to convert
#' @param outfile output file
#' @param overwrite should output file be overwritten?
#' @param args arguments to pass to \code{\link{system2}} to pass to
#' \code{ffmpeg}
#'
#' @return A character string of the output file with different attributes
#' @export
#'
#' @examples
#' pngfile <- tempfile(fileext = ".png")
#' png(pngfile)
#' plot(0, 0)
#' dev.off()
#' if (have_ffmpeg_exec()) {
#' res <- ffmpeg_convert(pngfile)
#' }
ffmpeg_convert <- function(file,
outfile = tempfile(
fileext = paste0(".", tools::file_ext(file))
),
overwrite = TRUE,
args = NULL) {
ffmpeg <- ffmpeg_exec(quote = FALSE)
file <- normalizePath(file)
add_y <- NULL
if (file.exists(outfile)) {
if (!overwrite) {
stop("outfile already exists, overwrite = FALSE")
} else {
add_y <- "-y"
}
}
args <- c(add_y, "-i", file, args, outfile)
suppressWarnings({
res <- system2(ffmpeg, args = args, stdout = TRUE, stderr = TRUE)
})
attr(outfile, "result") <- res
return(outfile)
}