我正在研究使用ffprobe
和data.table
包在R中提取胶片持续时间的最快方法。
wget https://ia801403.us.archive.org/13/items/AboutBan1935/AboutBan1935_512kb.mp4
mv AboutBan1935_512kb.mp4 one.mp4
for file in two.mp4 three.mp4 four.mp4 five.mp4 ; do cp one.mp4 "$file" ; done
library(data.table)
library(parallel)
# Get locations
executables <- Sys.which(c('ffprobe', 'ffmpeg'))
# Duration Function
get_duration_parallel <- function(files){
mclapply(X = files, FUN = function(file){
ffprobe_duration <- paste(executables['ffprobe'],
" -v quiet -print_format compact=print_section=0:nokey=1:escape=csv -show_entries format=duration ",
'"', file, '"', sep = "")
file_duration <- as.numeric(system(command = ffprobe_duration, intern = TRUE))
return(file_duration)
}, mc.cores = detectCores())
}
get_duration <- function(files){
sapply(X = files, FUN = function(file){
ffprobe_duration <- paste(executables['ffprobe'],
" -v quiet -print_format compact=print_section=0:nokey=1:escape=csv -show_entries format=duration ",
'"', file, '"', sep = "")
file_duration <- as.numeric(system(command = ffprobe_duration, intern = TRUE))
return(file_duration)
})
}
# Example table
dt <- data.table(Path = list.files(path = ".", pattern = "*.mp4$"))
system.time(
dt[, Seconds := get_duration_parallel(Path)]
)
# 9.667 seconds
system.time(
dt[, Seconds := get_duration(Path)]
)
# 0.078 seconds
我错过了任何明显的加速吗?扫描500个档案的ffprobe统计档案需要大约5分钟的测试时间。