Skip to content

Instantly share code, notes, and snippets.

@rileyjshaw
Created November 17, 2023 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rileyjshaw/73c5081d73a420c0a205ccdcb89e284f to your computer and use it in GitHub Desktop.
Save rileyjshaw/73c5081d73a420c0a205ccdcb89e284f to your computer and use it in GitHub Desktop.
Convert a folder of .flac albums to 192kbps MP3s to save space on my OpenSwim headphones
#!/bin/bash
# Define the source and target directories.
SOURCE_DIR="./originals"
TARGET_DIR="./processed"
# Ensure ffmpeg is installed.
if ! command -v ffmpeg &> /dev/null
then
echo "ffmpeg could not be found, please install it."
exit 1
fi
# Remove the year prefix from the album name, if present.
strip_year() {
local album_name="$1"
# This regex removes a leading year in the format "YYYY - ".
echo "$album_name" | sed -E 's/^[0-9]{4} - //'
}
# Iterate over the artist directories.
for artist_path in "$SOURCE_DIR"/*; do
if [ -d "$artist_path" ]; then
artist_name=$(basename "$artist_path")
# Iterate over the album directories within each artist directory.
for album_path in "$artist_path"/*; do
if [ -d "$album_path" ]; then
album_name=$(basename "$album_path")
# Strip the year from the album name.
processed_album_name=$(strip_year "$album_name")
# Create a processed directory for the artist-album.
mkdir -p "$TARGET_DIR/$artist_name - $processed_album_name"
# Convert each FLAC file to MP3.
for flac_file in "$album_path"/*.flac; do
if [ -f "$flac_file" ]; then
base_name=$(basename "$flac_file" .flac)
mp3_file="$TARGET_DIR/$artist_name - $processed_album_name/$base_name.mp3"
# Convert to MP3 with 192 kbps bitrate.
ffmpeg -i "$flac_file" -ab 192k "$mp3_file"
fi
done
fi
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment