#!/bin/bash

INPUT="input.wav"

# Check if input file exists
if [[ ! -f "$INPUT" ]]; then
    echo "Error: $INPUT not found!"
    exit 1
fi

# Create directories if they don't exist
mkdir -p opus apple-heaacv2

# Define Opus targets: Bitrate|Channels|Suffix
opus_targets=(
    "6|1|6k_mono"
    "12|2|12k_stereo"
    "24|2|24k_stereo"
    "32|2|32k_stereo"
    "64|2|64k_stereo"
)

# Define Apple HE-AACv2 targets: Bitrate|Channels|Suffix
# Note: 6k is too low for AAC-HEv2 (minimum is usually ~8-10k)
aac_targets=(
    "12|2|12k_stereo"
    "24|2|24k_stereo"
    "32|2|32k_stereo"
    "64|2|64k_stereo"
)

# Process Opus
for target in "${opus_targets[@]}"; do
    IFS="|" read -r br ch suffix <<< "$target"
    echo "Encoding Opus: $suffix..."
    ffmpeg -hide_banner -loglevel error -i "$INPUT" \
        -c:a libopus -b:a "${br}k" -ac "$ch" -vbr on \
        "opus/opus_${suffix}.opus" -y
done

# Process Apple HE-AACv2
for target in "${aac_targets[@]}"; do
    IFS="|" read -r br ch suffix <<< "$target"
    echo "Encoding Apple HE-AACv2: $suffix..."
    # aac_at automatically selects HE-AAC v2 for low bitrate stereo
    ffmpeg -hide_banner -loglevel error -i "$INPUT" \
        -c:a libfdk_aac -profile:a aac_he_v2 -b:a "${br}k" -ac "$ch" \
        "libfdk-heaacv2/heaac_${suffix}.m4a" -y
done

echo "-----------------------------------------------"
echo "Done! Check the 'opus' and 'apple-heaacv2' folders."
