Bash one-liner using sox to batch convert the sampling frequency of audio files

A bash one-liner to batch convert the sampling rate of WAV files using the SoX tool.  The example will resample *.wav files in the current directory to 8000Hz and place the output in an existing subdirectory called 8000Hz.

The one-liner below is overkill for this task, but the extra arguments provide a starting point for modification for related tasks.  The use of find/xargs should help the one-liner deal with very large numbers of audio files and filenames that contain whitespace.

Code


find . -maxdepth 1 -name '*.wav' -type f -print0 | xargs -0 -t -r -I {} sox {} -r 8000 8000Hz/{}

Explanation

An explanation of commands and flags from left-to-right:

find .
searches the current directory subtree
-maxdepth 1
excludes subdirectories
-name '*.wav' -type f
outputs files that match the glob *.wav
-print0
first step in handling filenames with spaces
xargs
executes a command given the output of find
-0
second step in handling filenames with spaces
-t
prints on stdout the command that is to be executed
-r
runs SoX command iff find output is generated
-I {}
specifies the pattern “{}” is replaced by the output of find
sox {} -r 8000 8000Hz/{}
is the resampling command
Advertisements

Tags: ,

2 Responses to “Bash one-liner using sox to batch convert the sampling frequency of audio files”

  1. Marie Says:

    Worked like a charm. Thanks!

  2. Mousa Says:

    Thank you, very helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: