Skip to content

Instantly share code, notes, and snippets.

@maddisondesigns
Last active December 23, 2025 06:42
Show Gist options
  • Select an option

  • Save maddisondesigns/52c76e86e21d9d36b930b187917c1917 to your computer and use it in GitHub Desktop.

Select an option

Save maddisondesigns/52c76e86e21d9d36b930b187917c1917 to your computer and use it in GitHub Desktop.
Convert FLAC to M4A (AAC/ALAC) with ffmpeg
#!/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
#Folder containing FFMpeg application
FFMPEG_DIR=~/Downloads/ffmpeg
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo "FLAC to M4A Conversion with FFMPEG"
echo "Usage: sh ConvertFlacToM4A.sh source_dir destination_dir"
exit 1;
fi
echo "---===: STARTING CONVERSION :===----"
echo ""
# 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
echo ""
echo "Making New Destination Folder at : "$DESTINATION_DIR
echo ""
mkdir -p "$DESTINATION_DIR"
echo "Looping through files..."
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_FILE="$DESTINATION_DIR/$BASE.m4a"
if [ ! -f "$NEW_FILE" ]; then
echo "=================================================="
echo ""
echo "Converting: "$FILE
echo " To: "$NEW_FILE
echo ""
# Uncomment whichever is your preferred method below
# AAC Lossy Compression
#$FFMPEG_DIR/ffmpeg -hide_banner -i "$FILE" -c:a aac -vn -b:a 320k "$NEW_FILE" </dev/null
# ALAC Lossless Compression
$FFMPEG_DIR/ffmpeg -hide_banner -i "$FILE" -c:a alac -vn -b:a 1000k "$NEW_FILE" </dev/null
fi
done
echo "Conversion complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment