Trimming silence at the beginning and end of an audio file

Here’s a simple shell script that uses the amazing Ecasound to remove any silence from the beginning and end of an audio file:

#!/bin/sh
#
# from http://osdir.com/ml/audio.ecasound.general/2005-08/msg00002.html
#
# description: removes silence from the beginning and the end
#              of a file
# version: 20050807-1
# usage: ecatrimsilence.sh <inputfile>
 
tmp=ecatrimsilence-tmp.wav
 
if test -e "$tmp" ; then
  echo "error: temp file $tmp exists, unable to continue..."
  exit 1
fi
 
if test ! -e "$1" ; then
  echo "error: input file $1 does not exist, unable to continue..."
  exit 2
fi
 
format=`ecalength -sf "$1"`
 
echo "Trimming file ${1}."
echo "Removing silence at the end..."
ecasound -q -f:${format} -i reverse,"${1}" -o "${tmp}" -ge:1,0,0 -b:256
rm -f "${1}"
echo "Removing silence at the beginning..."
ecasound -q -f:${format} -i reverse,"${tmp}" -o "${1}" -ge:1,0,0 -b:256
rm -f "${tmp}"
echo "Done."
</inputfile>

I know some GUI programs that offer this functionality (Audacity, for example), but no other command-line tool.

Do you?

Comments

  1. Dan
    July 3rd, 2008 | 5:01 pm

    shouldn’t the second error be (s/tmp/1/):

    echo “error: input file $1 does not exist, unable to continue…”

    ?

    very useful though, thank you

  2. July 3rd, 2008 | 5:15 pm

    Yeah, indeed. Thanks! :)

  3. July 3rd, 2008 | 5:19 pm

    I’ve also added double quotes now to account for special characters in file names.

  4. Glen
    July 15th, 2008 | 3:52 pm

    will this work for mp3 files or just wav???

  5. July 15th, 2008 | 4:32 pm

    Yes, per the ecasound man page.

Leave a reply