我有20秒的.wav文件,我需要将它们组合起来以制作20分钟长的文件。我将它们按日期修改的顺序排列,但没有以特定的方式命名(这些文件直接来自AudioMoth录音,可以根据需要尝试将其重命名)。我已经研究了将它们组合的方法,可以使用sox或ffmpeg,但是我大约有15000个文件,因此手动操作会花费一些时间。希望可能有一个循环吗?是否可以通过bash或使用python或R?
这是我将如何使用R和ffmpeg
来解决这个问题。我确定您可以使用bash进行相同类型的循环,但这看起来非常简单:
combiner <- function(path, segments_per_file) {
## Get a list of the wav files
files <- list.files(path = path, pattern = ".wav", full.names = TRUE)
## Split the list of wav files according to the number of files you want to combine at a time
groups <- cumsum(seq_along(files) %% segments_per_file == 1)
file_list <- split(files, groups)
## Loop through the list and use the concat protocol for ffmpeg to combine the files
lapply(seq_along(file_list), function(x) {
a <- tempfile(fileext = ".txt")
writeLines(sprintf("file '%s'", file_list[[x]]), a)
system(sprintf('ffmpeg -f concat -safe 0 -i %s -c copy Group_%s.wav', a, x))
})
}
如果您想使用sox
,则循环会更简单一些:
combiner <- function(path, segments_per_file) {
files <- list.files(path = path, pattern = ".wav", full.names = TRUE)
groups <- cumsum(seq_along(files) %% segments_per_file == 1)
file_list <- split(files, groups)
lapply(seq_along(file_list), function(x) {
system(sprintf("sox %s Group_%s.wav", paste(file_list[[x]], collapse = " "), x))
})
}
在R中,如果要一次合并60个文件,则将运行combiner(path_to_your_wav_files, 60)
。
请注意,合并的文件将位于您运行脚本的工作目录中(使用getwd()
验证该位置)。