我有几个视频文件,我需要修剪/切割(即,削减00:05:00 - 00:10:00之间2小时长的视频)。我可以用ffmpeg剪掉每一段视频。然而,由于我有+100视频文件需要修剪,我想使用R循环功能来做它。
我发现有几个R包是人们用来处理视频的,比如成像仪或magick,但是我找不到用R来修剪视频的方法。
你能帮帮我吗?谢谢!
发布于 2020-07-09 15:42:58
使用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包,但我一直倾向于在终端使用循环或创建使用sprintf和system()运行的命令。
https://stackoverflow.com/questions/62804755
复制相似问题