Skip to content

Instantly share code, notes, and snippets.

@Friedjof
Last active November 6, 2024 18:31
Show Gist options
  • Save Friedjof/ce1aff2baeb557854cd1d5a748418b57 to your computer and use it in GitHub Desktop.
Save Friedjof/ce1aff2baeb557854cd1d5a748418b57 to your computer and use it in GitHub Desktop.

Audible Export Guide

This guide walks you through the process of downloading and converting your Audible audiobooks using audible-cli and AAXtoMP3.

Requirements

You’ll need the following tools:

  • audible-cli: A command-line tool for downloading Audible audiobooks.
  • AAXtoMP3: A script to convert AAX files into MP3 or M4B formats.

Step 1: Install audible-cli

  1. Install audible-cli
    Install audible-cli globally on your system using pip (make sure Python is installed):

    pip install audible-cli
  2. Log in to Audible
    Log in to your Audible account by running the following command. This starts the login process and saves your credentials:

    audible quickstart

Step 2: Install AAXtoMP3

  1. Clone the Repository
    Clone the AAXtoMP3 repository to save the script locally:

    git clone https://github.com/KrumpetPirate/AAXtoMP3.git
  2. Change into the Directory
    Navigate into the newly created AAXtoMP3 directory:

    cd AAXtoMP3
  3. Make the Script Executable
    Set permissions to make the AAXtoMP3 script executable:

    chmod +x AAXtoMP3

Now AAXtoMP3 is ready for use.

Step 3: Download Audiobooks

  1. Download Audiobooks
    Use the following command to download all audiobooks from your Audible account in AAXC format, including cover images and chapter information:

    audible download --aaxc --cover --cover-size 1215 --chapter --all

    This command:

    • Downloads all available audiobooks (--all).
    • Saves files in AAXC format.
    • Adds cover images and chapter data at the specified cover size (--cover-size 1215).
  2. Retrieve Activation Bytes
    You’ll need the activation bytes for conversion, which can be obtained with this command:

    audible activation-bytes

    Save the output, as you’ll need it for the conversion.

Step 4: Convert Audiobooks

  1. Convert to M4B Format
    Use the AAXtoMP3 script to convert the downloaded AAXC files to M4B format. Include your activation bytes as follows:

    bash AAXtoMP3 -e:m4b --use-audible-cli-data --authcode <activation-bytes> *.aaxc

    Replace <activation-bytes> with the activation bytes retrieved in the previous step.

    This command:

    • Converts the files to M4B format (-e:m4b).
    • Uses both --use-audible-cli-data and --authcode <activation-bytes> for authentication and decryption.

Summary of Key Commands

# Install audible-cli
pip install audible-cli

# Audible login
audible quickstart

# Download audiobooks
audible download --aaxc --cover --cover-size 1215 --chapter --all

# Retrieve activation bytes
audible activation-bytes

# Clone AAXtoMP3 repository and make executable
git clone https://github.com/KrumpetPirate/AAXtoMP3.git
cd AAXtoMP3
chmod +x AAXtoMP3

# Convert audiobooks
bash AAXtoMP3 -e:m4b --use-audible-cli-data --authcode <activation-bytes> *.aaxc

This should allow you to successfully download and convert your Audible audiobooks.

@Friedjof
Copy link
Author

Friedjof commented Nov 6, 2024

This script automates the process of downloading and converting Audible audiobooks to M4B format. It sets up a dedicated environment in a directory named AudibleExporter, manages dependencies, and runs each necessary step for a seamless audiobook export.

#!/bin/bash

# Set up directories and filenames
SCRIPT_DIR=$(pwd)
EXPORT_DIR="$SCRIPT_DIR/AudibleExporter"
VENV_DIR="$EXPORT_DIR/venv"
AAX_SCRIPT="$EXPORT_DIR/AAXtoMP3"
AAX_REPO="https://github.com/KrumpetPirate/AAXtoMP3.git"

# Step 0a: Check for mp4art and install if missing
if ! command -v mp4art &> /dev/null; then
    echo "WARN: mp4art was not found in your PATH."
    echo "Without it, this script will not be able to add cover art to m4b files."
    
    # Attempt to install mp4art based on the OS
    if [[ "$OSTYPE" == "darwin"* ]]; then
        echo "Attempting to install mp4v2 on macOS..."
        if command -v brew &> /dev/null; then
            brew install mp4v2
        else
            echo "Homebrew is not installed. Please install Homebrew and rerun the script."
            exit 1
        fi
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        echo "Attempting to install mp4v2 on Ubuntu/Debian from source..."
        
        # Install dependencies for building mp4v2 from source
        sudo apt update
        sudo apt install -y build-essential cmake libtool pkg-config

        # Clone, build, and install mp4v2
        git clone https://github.com/enzo1982/mp4v2.git
        cd mp4v2
        cmake .
        make
        sudo make install
        cd ..
        rm -rf mp4v2

        # Add library path and update cache
        echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/mp4v2.conf
        sudo ldconfig

        # Verify installation
        if ! command -v mp4art &> /dev/null; then
            echo "mp4art could not be installed. No cover art will be added to m4b files."
        else
            echo "mp4art successfully installed."
        fi
    else
        echo "Unsupported OS. Please manually install mp4v2 for your system."
        exit 1
    fi
else
    echo "mp4art is already installed."
fi

# Step 0b: Check for mediainfo and install if missing
if ! command -v mediainfo &> /dev/null; then
    echo "WARN: mediainfo was not found in your PATH."
    echo "Without it, this script will not be able to add narrator and description tags to m4b files."
    
    # Attempt to install mediainfo based on the OS
    if [[ "$OSTYPE" == "darwin"* ]]; then
        echo "Attempting to install mediainfo on macOS..."
        if command -v brew &> /dev/null; then
            brew install mediainfo
        else
            echo "Homebrew is not installed. Please install Homebrew and rerun the script."
            exit 1
        fi
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        echo "Attempting to install mediainfo on Ubuntu/Debian..."
        sudo apt update
        sudo apt install -y mediainfo

        # Verify installation
        if ! command -v mediainfo &> /dev/null; then
            echo "mediainfo could not be installed. No narrator or description tags will be added to m4b files."
        else
            echo "mediainfo successfully installed."
        fi
    else
        echo "Unsupported OS. Please manually install mediainfo for your system."
        exit 1
    fi
else
    echo "mediainfo is already installed."
fi

# Step 1: Create AudibleExporter directory if it doesn’t exist
if [ ! -d "$EXPORT_DIR" ]; then
    echo "Creating AudibleExporter directory..."
    mkdir -p $EXPORT_DIR
else
    echo "AudibleExporter directory already exists."
fi

# Navigate to AudibleExporter directory
cd $EXPORT_DIR

# Step 2: Create and activate virtual environment if it doesn’t already exist
if [ ! -d "$VENV_DIR" ]; then
    echo "Creating virtual environment..."
    python3 -m venv $VENV_DIR
else
    echo "Virtual environment already exists."
fi
source $VENV_DIR/bin/activate

# Step 3: Install audible-cli in the virtual environment if not installed
if ! pip show audible-cli > /dev/null 2>&1; then
    echo "Installing audible-cli..."
    pip install --upgrade pip
    pip install audible-cli
else
    echo "audible-cli is already installed."
fi

# Step 4: Log into Audible (user interaction required if not logged in)
if [ ! -f "$HOME/.audible-cli/config" ]; then
    echo "Please log into your Audible account. This is a one-time setup."
    audible quickstart
else
    echo "Already logged into Audible."
fi

# Step 5: Always attempt to download audiobooks (re-download if files exist)
echo "Downloading audiobooks..."
audible download --aaxc --cover --cover-size 1215 --chapter --all

# Step 6: Retrieve activation bytes if not already retrieved
if [ -z "$ACTIVATION_BYTES" ]; then
    echo "Retrieving activation bytes..."
    # Capture the last 8 characters of the output
    ACTIVATION_BYTES=$(audible activation-bytes | tail -c 8)
    echo "Activation bytes: $ACTIVATION_BYTES"
else
    echo "Activation bytes already retrieved."
fi

# Step 7: Clone AAXtoMP3 repository to the parent directory, copy the script, and clean up if not already done
if [ ! -f "$AAX_SCRIPT" ]; then
    echo "Cloning AAXtoMP3 repository in the parent directory..."
    git clone $AAX_REPO ../AAXtoMP3
    echo "Copying AAXtoMP3 script and cleaning up..."

    # Move the AAXtoMP3 script into the AudibleExporter directory
    mv ../AAXtoMP3/AAXtoMP3 $AAX_SCRIPT
    rm -rf ../AAXtoMP3
else
    echo "AAXtoMP3 script already exists."
fi

# Step 8: Make the AAXtoMP3 script executable
chmod +x $AAX_SCRIPT

# Step 9: Always attempt to convert AAXC files to M4B format (even if files exist)
echo "Converting AAXC files to M4B format..."
bash $AAX_SCRIPT -e:m4b --use-audible-cli-data --authcode $ACTIVATION_BYTES *.aaxc

# Step 10: Deactivate virtual environment
echo "Deactivating virtual environment..."
deactivate

echo "Process complete. Audiobooks have been downloaded and converted."

Key Points

Here’s a brief explanation of each step in the script:

  1. Check and Install mp4art: The script first checks if mp4art is installed, which is required to add cover art to M4B files. If it’s missing, it attempts to install mp4v2 (the package containing mp4art) based on the operating system.

  2. Check and Install mediainfo: Similarly, it checks for mediainfo, which is needed to add narrator and description tags to M4B files. If mediainfo is not found, it will try to install it.

  3. Create AudibleExporter Directory: The script then creates a directory named AudibleExporter to organize the downloaded files and output. This ensures that all relevant files are saved in a single location.

  4. Set Up Virtual Environment: The script sets up and activates a Python virtual environment (venv) within AudibleExporter to isolate the installation of audible-cli.

  5. Install audible-cli: Inside the virtual environment, it installs audible-cli, a tool used to download Audible audiobooks in the AAXC format.

  6. Login to Audible: The script checks if the user is logged in to Audible via audible-cli. If not, it prompts for a one-time login.

  7. Download Audiobooks: The script initiates the download of all audiobooks in AAXC format, including cover images and chapter data.

  8. Retrieve Activation Bytes: It retrieves the activation bytes required to decrypt the downloaded files, capturing only the last 8 characters of the output.

  9. Clone and Set Up AAXtoMP3 Script: The script clones the AAXtoMP3 repository to the parent directory, moves the AAXtoMP3 script into AudibleExporter, and cleans up the cloned repository.

  10. Convert Audiobooks to M4B: The AAXtoMP3 script is used to convert the downloaded AAXC files into M4B format with cover art and tags, using the retrieved activation bytes for decryption.

  11. Deactivate Virtual Environment: Finally, the virtual environment is deactivated, and the script confirms the process is complete.

This workflow ensures that all necessary dependencies are installed, files are organized, and the conversion from AAXC to M4B is seamless.

Running the Script

Save the script as audible_exporter.sh, make it executable, and run it:

chmod +x audible_exporter.sh
./audible_exporter.sh

This will ensure all downloads and conversions are contained within the AudibleExporter directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment