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
November 20, 2013 at 4:41 pm |
Worked like a charm. Thanks!
November 27, 2013 at 7:15 am |
Thank you, very helpful.