Skip to content

Instantly share code, notes, and snippets.

@moui72
Forked from jorgehatccrma/recursive-resample.sh
Last active March 25, 2019 16:22
Show Gist options
  • Save moui72/33e18bdd0e155ba1356080b39376f17b to your computer and use it in GitHub Desktop.
Save moui72/33e18bdd0e155ba1356080b39376f17b to your computer and use it in GitHub Desktop.
Recursively resample a bunch of audio files in a directory, using sox
#!/bin/bash
# A simple script to recursively resample a bunch of files
# in a directory. Only certain file extensions (mp3, aac,
# flac, wav) are considered.
#
# It takes 3 command line options: `indir`, `outdir` and `target_sr`.
# The destination (`outdir`) is relative to the current
# directory of where you were when the script was run.
# The desired sample rate should be provided as `target_sr`.
#
# Example: resample.sh audio/ resampled/
#
# The direcotry structure inside `indir` will be replicated
# in `outdir`.
# Sourece directory with files to convert
InDir=$1
# Set the directory you want for the converted files
OutDir=$2
# make sure the output directory exists (create it if not)
mkdir -p "$OutDir"
# Target sample rate
TARGET_SR=$3
# Convert each file with SoX, and write the converted file
# to the corresponding output dir, preserving the internal
# structure of the input dir
find -E $InDir -type f -iregex '.*\.(mp3|wav|flac|aac)$' -print0 | while read -d $'\0' input
do
echo "processing" $input
# the output path, without the InDir prefix
output=${input#$InDir}
# replace the original extension with .wav
output=$OutDir${output%.*}.wav
# get the output directory, and create it if necessary
outdir=$(dirname "${output}")
mkdir -p "$outdir"
# finally, convert the file
sox "$input" -r $TARGET_SR "$output"
echo "saved as $output"
done
@moui72
Copy link
Author

moui72 commented Mar 25, 2019

minor changes from https://gist.github.com/jorgehatccrma/24786ee7fc8158e7bd1b010d7cf34bbe (tyvm @jorgehatccrma)

removed the channels conversion and made target_sr a third input arg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment