Skip to content

Instantly share code, notes, and snippets.

@sardbaba
Last active May 24, 2022 16:51
Show Gist options
  • Save sardbaba/7bc74eff77c109c9c72ca742f8114a51 to your computer and use it in GitHub Desktop.
Save sardbaba/7bc74eff77c109c9c72ca742f8114a51 to your computer and use it in GitHub Desktop.
Converts multiple mp3 to one single mp3
#!/bin/bash
for d in */ ; do
mp3PATH=$PWD/"$d"
OUTDIR=$(basename "${mp3PATH//\'}")
echo $mp3PATH $OUTDIR
if [ -d "$mp3PATH" ]; then
for d2 in "$mp3PATH"*; do
MIN_BITRATE=$(soxi -B "$d2"/*.mp3 | grep -o -E '[0-9]+' | sort -n | head -1)
echo "Min bitrate: " $MIN_BITRATE
if [ $MIN_BITRATE -lt 160 ]; then
BITRATE=128
elif [ $MIN_BITRATE -lt 192 ]; then
BITRATE=160
elif [ $MIN_BITRATE -lt 224 ]; then
BITRATE=192
elif [ $MIN_BITRATE -lt 256 ]; then
BITRATE=224
elif [ $MIN_BITRATE -lt 320 ]; then
BITRATE=256
else
BITRATE=320
fi
# BITRATE=256 # STATICALLY DEFINED
echo "Bitrate for sox -C: " $BITRATE
OUTFILE=$(basename "${d2//\'}")
OUT_DIR=/home/your-outdir/"$OUTDIR"
[ -d "$OUT_DIR" ] || mkdir "$OUT_DIR"
echo $OUT_DIR
# Add files info
echo -n > "$OUT_DIR"/"$OUTFILE"_info.txt
for f in "$d2"/*.{mp3,Mp3,MP3,mP3}; do
if [ -f "$f" ]; then
BF=$(basename "$f")
E=$(soxi -d "$f")
echo "$BF | $E" >> "$OUT_DIR"/"$OUTFILE"_info.txt
fi
if [ ! -f "$f".wav ]; then
echo "$f"
ffmpeg -i "$f" -ar 44100 "$f".wav >/dev/null 2>&1
fi
done
for f in "$d2"/*.flac; do
if [ -f "$f" ]; then
BF=$(basename "$f")
E=$(soxi -d "$f")
echo "$BF | $E" >> "$OUT_DIR"/"$OUTFILE"_info.txt
fi
if [ ! -f "$f".wav ]; then
echo "$f"
ffmpeg -i "$f" "$f".wav >/dev/null 2>&1
fi
done
WAV="$d2"/*.wav
sox "$WAV" -C $BITRATE "$OUT_DIR"/"$OUTFILE"_"$BITRATE"k.mp3
echo "Removing partial WAV files..."
rm "$d2"/*.wav
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment