Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 2 command line options: `indir` and `outdir`.
# The destination (`outdir`) is relative to the current
# directory of where you were when the script was run.
#
# 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=16000
# Target num channels
TARGET_NC=1
# 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" remix 1,2
echo "saved as $output"
done
@Zaragata
Copy link

Zaragata commented Jan 30, 2022

Hey, I'm trying to learn some bash and ended up taking up your script 'cause I wanted to resample my entire FLAC collection into Ogg Vorbis instead of just using mp3fs.

So I made some small changes (sorry if I butchered your code!):

#!/bin/bash

SourceDirectory=$1
DestinedDirectory=$2
TargetCompression=7

mkdir -p "$DestinedDirectory"

find "$SourceDirectory" -type f -print0 | while read -d $'\0' FoundFile
do
  echo "Processing" $FoundFile

  OutputFile=$DestinedDirectory${FoundFile#"$SourceDirectory"}
  OutputDirectory=$(dirname "$OutputFile")

  if [[ "$FoundFile" == *.flac ]]
  then
    mkdir -p "$OutputDirectory"/spectrals/
    OutputFile=${OutputFile%.flac}.ogg
    OutputBasename=$(basename "$OutputFile")
    sox "$FoundFile" -C $TargetCompression -G "$OutputFile" remix 1,2 norm -0.1 dither -s
    sox "$OutputFile" -n spectrogram -o "$OutputDirectory/spectrals/${OutputBasename%.ogg}.jpg"
  else
    mkdir -p "$OutputDirectory"
    cp "$FoundFile" "$OutputFile"
  fi

  echo "Saved as" $OutputFile
done

Just commenting the code in just in case someone was looking for the same thing.

Ty so much for making this gist! It made learning about piping through a while loop a breeze! :)

@alfredoej87
Copy link

alfredoej87 commented Mar 5, 2022

How would you avoid problems of clipping and dithering? I undersand you need to add a -G option. Is that correct?

sox WARN rate: rate clipped 1 samples; decrease volume?
sox WARN dither: dither clipped 1 samples; decrease volume?

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