Saving your Audible books to MP3 using Python, Colab, and AAXtoMP3

If you’re like me and you don’t like having your audiobooks tied up in Audible. There is a way to convert them to MP3.

I wrote some Python code in Google Colab to show this process.

The first thing you’ll want to do is mount Google drive so you have access to your files. This can be done here.

Your working_folder is the location in your Google drive that the files will be backed up to.

working_folder = 'Books/audible_backup'

# <----- Make sure to mount drive in Colab first 
import os
import sys
if 'google.colab' in str(get_ipython()):
  print('Running on CoLab')
  root_dir='/content/drive/My Drive/' + working_folder
  if os.path.isdir(root_dir):
    %cd $root_dir
  else:
    print('Check your working_folder or if Google drive is mounted')
    sys.exit()
  sys.path.append(root_dir)  
  print('Colab code has been executed')
else:
  print('Not running on Colab')

Here is my Google Drive mounted in Windows for comparison

Install the dependencies

!apt-get update
!apt-get install ffmpeg libav-tools x264 x265 bc
!apt-get install mediainfo
!pip install audible
!pip install audible-cli
!pip install ffmpeg
!apt-get install mp4v2-utils
!apt-get install jq
!apt-get install bc

!add-apt-repository ppa:savoury1/ffmpeg4
!apt-get update
!apt-get install ffmpeg
!ffmpeg -version
!apt-get install mp4v2-utils
import os

Run audible-quickstart to attach your Audible account.

!audible-quickstart

Now we’re going to make a backup of all of your audiobooks in AAX and AAXC format.

%cd /content/drive/My Drive/Books/audible_backup
!audible download --all --aax --cover --cover-size 1215 --chapter
!audible download --all --aaxc --cover --cover-size 1215 --chapter

Once you clone all your books to AAX and AAXC files in Google Drive it should look like this.

Now we need your AUTHCODE for your AAX files.

AUTHKEY = !audible activation-bytes
AUTHKEY = AUTHKEY[0]
print(AUTHKEY)

Next, we’ll clone a tool from Github called AAXtoMP3 and make sure it has executable permissions. This tool does all the heavy lifting.

%cd /root
!git clone https://github.com/KrumpetPirate/AAXtoMP3.git
%cd /root/AAXtoMP3
!chmod u+x /root/AAXtoMP3/interactiveAAXtoMP3
!chmod u+x /root/AAXtoMP3/AAXtoMP3

Now that you’ve cloned AAXtoMP3 you can run the following code:

%cd '/content/drive/My Drive/Books/audible_backup'
for filename in os.listdir('/content/drive/My Drive/Books/audible_backup'):
  if filename.endswith('.aaxc'):
    print(filename)
    os.system(f'/root/AAXtoMP3/AAXtoMP3 -e:mp3 --level 4 -c -l 1 -n {filename}')
  if filename.endswith('.aax'):
    print(filename)
    os.system(f'/root/AAXtoMP3/AAXtoMP3 -e:mp3 --level 4 -c -A {AUTHKEY} -l 1 {filename}')

Open In Colab

Leave a Reply