我可以像使用ffmpeg一样使用R来修剪视频吗



我有几个视频文件需要修剪/剪切(即在2小时的视频中剪切00:05:00-00:10:00(。我可以使用ffmpeg来修剪/剪切每个视频。然而,由于我有+100个视频文件需要修剪,我想使用R循环功能来完成

我发现有几个R包是人们用于视频处理的,比如imager或magik,但我找不到使用R 修剪视频的方法

你能帮我吗?谢谢

ffmpeg修剪视频的基本方法如下:

ffmpeg -i input.mp4 -ss 00:05:00 -to 00:10:00 -c copy output.mp4

要创建批处理文件,您可以将以下内容放在文本文件中,并将其保存为类似";trimvideo.bat";并在相关文件夹中运行它。

@echo off
:: loops across all the mp4s in the folder
for %%A in (*.mp4) do ffmpeg -i "%%A"^
:: the commands you would use for processing one file
-ss 00:05:00 -to 00:10:00 -c copy ^
:: the new file (original_trimmed.mp4)
"%%~nA_trimmed.mp4"
pause

如果你想通过R做到这一点,你可以做一些类似的事情:

# get a list of the files you're working with
x <- list.files(pattern = "*.mp4")
for (i in seq_along(x)) {
cmd <- sprintf("ffmpeg -i %s -ss 00:05:00 -to 00:10:00 -c copy %_trimmed.mp4",
x[i], sub(".mp4$", "", x[i]))
system(cmd)
}

过去,当我想从一个文件或多个文件中剪切特定零件时,我也使用过类似的方法。在这些情况下,我从类似于以下内容的data.frame开始:

df <- data.frame(file = c("file_A.mp4", "file_B.mp4", "file_A.mp4"),
start = c("00:01:00", "00:05:00", "00:02:30"),
end = c("00:02:20", "00:07:00", "00:04:00"),
output = c("segment_1.mp4", "segment_2.mp4", "segment_3.mp4"))
df
#         file    start      end        output
# 1 file_A.mp4 00:01:00 00:02:20 segment_1.mp4
# 2 file_B.mp4 00:05:00 00:07:00 segment_2.mp4
# 3 file_A.mp4 00:02:30 00:04:00 segment_3.mp4

我使用sprintf创建要运行的ffmpeg命令:

cmds <- with(df, sprintf("ffmpeg -i %s -ss %s -to %s -c copy %s", 
file, start, end, output)) 
cmds
# [1] "ffmpeg -i file_A.mp4 -ss 00:01:00 -to 00:02:20 -c copy segment_1.mp4"
# [2] "ffmpeg -i file_B.mp4 -ss 00:05:00 -to 00:07:00 -c copy segment_2.mp4"
# [3] "ffmpeg -i file_A.mp4 -ss 00:02:30 -to 00:04:00 -c copy segment_3.mp4"

我用lapply(..., system):运行它

lapply(cmds, system)

您也可以查看av包,但我一直倾向于在终端使用循环,或者使用sprintfsystem()创建要运行的命令。

最新更新