Trim silence in audio files, by directory

I’m looking for a utility that allows me to trim the audio from the beginning and end of all audio files in a directory. I’ve found a couple of programs in searches, but nothing that quite meets my feature requirements. Specifically, I’m looking for something that can:

  • Trim the beginning and end only, skipping any silences within the file
  • Modify the original file rather than creating a new file. Alternatively, if that isn’t available at least the option to preserve the original’s file name in the copy as well as the original directory structure.

The other options I’ve found, such as mp3splt, mostly appear to create new files with edited filenames. As the batch of files which I would like to edit is spread throughout multiple directories and subdirectories, I would much rather edit the original files and preserve the file structure than manually attempt to sort everything back into the correct place.

In theory you could do something like this:

for x in $(ls --file-type *.mp3); 
     do
          echo "ffmpeg -i $x -af 'silenceremove=start_periods=1:start_duration=1:start_threshold=-60dB:detection=peak,aformat=dblp,areverse,silenceremove=start_periods=1:start_duration=1:start_threshold=-60dB:detection=peak,aformat=dblp,areverse' $x";
 done

https://ffmpeg.org/ffmpeg-filters.html#silenceremove

2 Likes

I like it @megavolt
ffmpeg sure is a powerful beast

1 Like

I just tried this solution, but judging from the terminal output it didn’t touch any of the files in sub-directories, only the files that were in my Music directory. Also,I listened to a couple of sample files in the Music directory, and it appears that there still is silence at the beginning of these files.

Yes that is exactly what it does.

Instead of

you could use the find command.

man find

I said “in theory”, means it is an example how it could be done. Read the manual of silenceremove :wink:

Also echo "text" means it just displays what it will run as text. If you you want to run it, then remove echo "". Be careful and make backups. It will overwrite files.