0

I need to cut off the first say 3 seconds from a batch of wav files. Is there a way to do it from the command line or using a linux native program?

Thanks.

black-clover
  • 321
  • 1
  • 2
  • 7

1 Answers1

1

You can use sox (see man sox for its options) to process one file:

sox music.wav shortermusic.wav trim 3

The timing value is hours:minutes:seconds, defaulting to the smallest interval(s) if parts are omitted, so here we can use 3 for three seconds.

After that it's just a case of using a loop to iterate across the set of files

mkdir trimmed
for m in *.wav
do
    printf 'Trimming: %s\n' "$m"
    sox "$m" trimmed/"$m" trim 3
done

The trimmed files will be put into a subdirectory trimmed so that you don't get to trim them twice, accidentally

If you have ffmpeg installed you can visually check the duration of a file:

ffprobe music.wav 2>&1 | grep Duration

or a little more elegantly in seconds:

ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 music.wav
roaima
  • 107,089
  • 14
  • 139
  • 261
  • Thanks a lot, it worked beautifully! Also, I was mistaken, and the cut I needed wasn't 3 seconds but 0.70 seconds and, in case someone else needs this, using 0.70 worked just fine. – black-clover Aug 21 '23 at 21:11
  • @black-clover glad to hear it worked for you. There is also a "remove leading silence" option, in case that's more relevant. As always, test with a single file and then apply with an appropriate loop – roaima Aug 21 '23 at 21:30
  • Yes, I tested it on a few files and then when on with a working copy of the library. Worked like a charm. Fast and precise. Indeed, most of the samples needed to remove a silence, but some had sound, so the time cut was what worked for me. – black-clover Aug 21 '23 at 23:44