Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
echo "Bitrate for sox -C: " $BITRATE
OUTFILE=$(basename "${d2//\'}")
OUT_DIR=/your-out-dir/"$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" "$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 "$WAV"
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.