Skip to content

Instantly share code, notes, and snippets.

@Thadah
Created October 4, 2023 15:17
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 Thadah/28a83b3f97f7bdcb76a75c3d3337f7b5 to your computer and use it in GitHub Desktop.
Save Thadah/28a83b3f97f7bdcb76a75c3d3337f7b5 to your computer and use it in GitHub Desktop.
Converts a folder full of MP3 files into AAC-LC M4A files with all metadata and artwork
#!/bin/bash
# Define the source and destination directories
SRC_DIR="/path/to/folder/"
DEST_DIR="/path/to/folder/"
# Define audio quality
# 0.1 (Highest Quality) - 2 (Lowest Quality)
AUDIO_QUALITY=1
AUDIO_SAMPLING=44100
# Parallel Threads
THREADS=4
# Create the destination directory if it doesn't exist
mkdir -p "$DEST_DIR"
# Function to process each file
process_file() {
FILE="$1"
# Create the destination directory for this file, preserving the directory structure
FILE_DIR=$(dirname "${FILE#$SRC_DIR}")
mkdir -p "$DEST_DIR/$FILE_DIR"
# Define the destination file name
DEST_FILE="$DEST_DIR/$FILE_DIR/$(basename "$FILE" .mp3).m4a"
# Skip if file already exists
if [[ -f "$DEST_FILE" ]]; then
return
fi
# Extract artwork to a temporary file
TEMP_ARTWORK=$(mktemp).jpg
ffmpeg -i "$FILE" -an -vcodec mjpeg -vframes 1 -y "$TEMP_ARTWORK"
# Convert the audio
ffmpeg -i "$FILE" -c:a aac -q:a $AUDIO_QUALITY -ar $AUDIO_SAMPLING -map_metadata 0 -vn "$DEST_FILE"
# Embed the artwork into the m4a file using atomicparsley
atomicparsley "$DEST_FILE" --artwork "$TEMP_ARTWORK" --overWrite
# Remove the temporary artwork file
rm "$TEMP_ARTWORK"
}
export -f process_file
export SRC_DIR
export DEST_DIR
export AUDIO_QUALITY
export AUDIO_SAMPLING
# Use GNU Parallel to process the files in parallel
find "$SRC_DIR" -type f -name '*.mp3' | parallel -j $THREADS process_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment