Skip to content

Instantly share code, notes, and snippets.

@3zcurdia
Last active September 28, 2025 17:52
Show Gist options
  • Select an option

  • Save 3zcurdia/a1ff76b68b9ace100d181878b52510e0 to your computer and use it in GitHub Desktop.

Select an option

Save 3zcurdia/a1ff76b68b9ace100d181878b52510e0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Enhanced FLAC to M4A conversion script with proper concurrency control
set -euo pipefail
get_total_size() {
local files=("$@")
if [[ ${#files[@]} -gt 0 ]]; then
du -shc "${files[@]}" 2>/dev/null | tail -n1 | cut -f1
else
echo "0B"
fi
}
get_cpu_count() {
if command -v nproc >/dev/null 2>&1; then
nproc
elif [[ "$OSTYPE" == "darwin"* ]]; then
sysctl -n hw.logicalcpu
else
echo "4"
fi
}
check_dependencies() {
if ! command -v ffmpeg >/dev/null 2>&1; then
echo "Error: ffmpeg is not installed or not in PATH" >&2
exit 1
fi
}
convert_file() {
# Basic ffmpeg command
# ffmpeg -i "$f" -c:a libfdk_aac -vbr 5 -map 0:a -map_metadata 0 "${f%.flac}.m4a" &
local input_file="$1"
local output_file="${input_file%.flac}.m4a"
echo "Converting: $input_file -> $output_file"
if ffmpeg -hide_banner -loglevel error \
-threads 1 \
-i "$input_file" \
-c:a libfdk_aac \
-vbr 5 \
-cutoff 20000 \
-afterburner 1 \
-eld_sbr 0 \
-signaling 0 \
-latm 0 \
-header_period 5 \
-map 0:a \
-map_metadata 0 \
-disposition:a:0 default \
-movflags +faststart+use_metadata_tags \
-write_id3v2 1 \
-id3v2_version 4 \
-metadata:s:a:0 language=und \
-avoid_negative_ts make_zero \
-fflags +bitexact+genpts \
-max_muxing_queue_size 1024 \
-y \
"$output_file" 2>/dev/null; then
echo "βœ“ Completed: $output_file"
elif ffmpeg -hide_banner -loglevel error \
-threads 1 \
-i "$input_file" \
-c:a aac \
-b:a 320k \
-cutoff 22100 \
-profile:a aac_low \
-aac_coder twoloop \
-aac_pns 1 \
-aac_is 0 \
-aac_ms 0 \
-map 0:a \
-map 0:a \
-map_metadata 0 \
-disposition:a:0 default \
-movflags +faststart+use_metadata_tags \
-write_id3v2 1 \
-id3v2_version 4 \
-metadata:s:a:0 language=und \
-avoid_negative_ts make_zero \
-fflags +bitexact+genpts \
-max_muxing_queue_size 1024 \
-y \
"$output_file" 2>/dev/null; then
echo "βœ“ Completed: $output_file (using built-in AAC encoder)"
else
echo "βœ— Failed: $input_file - trying with error output..." >&2
ffmpeg -i "$input_file" \
-c:a aac \
-b:a 320k \
-cutoff 22100 \
-map 0:a \
-map_metadata 0 \
-y \
"$output_file"
return 1
fi
}
export -f convert_file
main() {
check_dependencies
flac_files=(*.flac)
if [[ ! -e "${flac_files[0]}" ]]; then
echo "No FLAC files found in current directory" >&2
exit 1
fi
MAX_JOBS=$(get_cpu_count)
current_dir=$(basename "$PWD")
output_dir="$current_dir"
mkdir -p "$output_dir"
total_input_size=$(get_total_size "${flac_files[@]}")
echo "Input: ${#flac_files[@]} FLAC files, Total size: $total_input_size"
echo "Starting conversion with $MAX_JOBS concurrent jobs..."
echo
printf '%s\0' "${flac_files[@]}" | \
xargs -0 -n 1 -P "$MAX_JOBS" -I {} bash -c 'convert_file "$@"' _ {}
echo
if ls *.m4a 1> /dev/null 2>&1; then
m4a_files=(*.m4a)
total_output_size=$(get_total_size "${m4a_files[@]}")
converted_count=${#m4a_files[@]}
mv *.m4a "$output_dir/"
echo
echo "πŸ“Š CONVERSION SUMMARY:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Input: ${#flac_files[@]} FLAC files β†’ $total_input_size"
echo "Output: $converted_count M4A files β†’ $total_output_size"
echo "Files location: $output_dir/"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
else
echo "❌ No M4A files were created"
exit 1
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment