July 3, 2008
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(5)
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
Yeah, indeed. Thanks! :)
I’ve also added double quotes now to account for special characters in file names.
will this work for mp3 files or just wav???
Yes, per the ecasound man page.