Last active
June 30, 2025 06:35
-
-
Save maddisondesigns/52c76e86e21d9d36b930b187917c1917 to your computer and use it in GitHub Desktop.
Convert FLAC to M4A (AAC/ALAC) with ffmpeg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Convert FLAC to M4A with ffmpeg | |
| # Usage: convert_music.sh source_dir destination_dir | |
| # Requires: ffmpeg - https://ffmpeg.org/download.html | |
| # | |
| # Author: Rick Makes - https://www.rickmakes.com/batch-convert-lossless-audio-to-aac-shell-script/ | |
| # Modified: Anthony Hortin | |
| # | |
| set -e # exit script if control-c is used | |
| USAGE="Usage: sh ConvertFlacToM4A.sh source_dir destination_dir" | |
| # --- Options processing ------------------------------------------- | |
| if [ $# == 0 ] ; then | |
| echo $USAGE | |
| exit 1; | |
| fi | |
| # Convert relative path to absolute. Also remove trailing slash | |
| SOURCE_DIR="$(cd "$(dirname "$1")"; pwd)/$(basename "$1")" | |
| SOURCE_DIR=$(dirname "$SOURCE_DIR/temp") # this fixes . and ./ | |
| DESTINATION_DIR="$(cd "$(dirname "$2")"; pwd)/$(basename "$2")" | |
| DESTINATION_DIR=$(dirname "$DESTINATION_DIR/temp") # this fixes . and ./ | |
| echo "Converting from: "$SOURCE_DIR | |
| echo "Converting TO: "$DESTINATION_DIR | |
| find "$SOURCE_DIR" \( -iname '*.flac' -or -iname '*.m4a' \) -type f -print | while read -r FILE | |
| do | |
| ORIG_DIR=$(dirname "$FILE") | |
| # Get basename and remove extension | |
| BASE=$(basename "$FILE") # get filename | |
| BASE=${BASE%.*} # remove extension from filename | |
| NEW_DIR=${ORIG_DIR/$SOURCE_DIR/$DESTINATION_DIR} | |
| mkdir -p "$NEW_DIR" | |
| NEW_FILE="$NEW_DIR/$BASE.m4a" | |
| if [ ! -f "$NEW_FILE" ]; then | |
| echo "Converting $FILE to $NEW_FILE" | |
| # Uncomment whichever is your preferred method below | |
| # AAC Lossy Compression | |
| #ffmpeg -hide_banner -i "$FILE" -c:a aac -vn -b:a 320k "$NEW_FILE" </dev/null | |
| # ALAC Lossless Compression | |
| ffmpeg -hide_banner -i "$FILE" -c:a alac -vn -b:a 1000k "$NEW_FILE" </dev/null | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment