Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Last active May 3, 2022 16:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vadimkantorov/00bf4fbe4323360722e3d2220cc2915e to your computer and use it in GitHub Desktop.
Save vadimkantorov/00bf4fbe4323360722e3d2220cc2915e to your computer and use it in GitHub Desktop.
Script to split audio files by silence
set -e
# for f in *.m4a; do ffmpeg -i "$f" -af "silenceremove=stop_periods=-1:stop_duration=0.2:stop_threshold=-40dB:window=1" -y "nosilence/$f"; done
for f in $1/*.wav; do
fname=$(basename $f)
segdir=segments.$fname
rm -rf $segdir
mkdir $segdir
for channel in 0 1; do
ffmpeg -hide_banner -nostdin -y -i $f -af "pan=mono|c0=c$channel,silencedetect=noise=-20dB:d=2" -f null - 2>&1 | grep 'silence_' > $fname.txt
tail -n +2 $fname.txt | cut -d':' -f2 | cut -d'|' -f1 | xargs -n2 | while read silence_end silence_start; do ffmpeg -loglevel panic -hide_banner -nostdin -y -i $f -map_channel 0.0.$channel -ss "$silence_end" -to "$silence_start" "$segdir/${fname}_${channel}_${silence_end}_${silence_start}.wav" || true; done
rm $fname.txt
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment