2

I have 20 second .wav files which I need to combine to make 20 minute length files. I have them in date modified order but not named in a particular way (the files have come directly from AudioMoth recordings, can try and get them renamed if needed). I've looked into ways of combining them and I can using sox or ffmpeg however I have around 15000 files so would take a little too long manually. Hoping it may be possible with a loop? Is this possible through bash or maybe with python or R?

Jasmine815
  • 23
  • 3
  • hope this helps https://stackoverflow.com/questions/2890703/how-to-join-two-wav-files-using-python. and you could use glob to get the list of wav files in folder with glob.glob( '*/**.wav' ) – Quantum Dreamer Mar 30 '20 at 16:57

1 Answers1

2

Here's how I would approach this using R and ffmpeg. I'm sure you can do the same type of looping with bash, but this seemed pretty straightforward:

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))
  })
}

If you'd prefer to use sox, the loop's a little more straightforward:

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))
  })
}

In R, you would then run combiner(path_to_your_wav_files, 60) if you want to combine 60 files at a time.

Note that the combined files will be in the working directory that you run the script from (use getwd() to verify where that is).

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485