Skip to content

Instantly share code, notes, and snippets.

@Chirishman
Last active August 24, 2022 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chirishman/55836729132ef657122f69734e4d2703 to your computer and use it in GitHub Desktop.
Save Chirishman/55836729132ef657122f69734e4d2703 to your computer and use it in GitHub Desktop.
WIP blog post

Authoring Richly Formatted Audiobooks

Why Bother

If you're anything like me (and I recognize that most people aren't) then you listen to a lot of audiobooks in a year. As technology advances though, there can be a temptation to either rebuy audiobooks which you bought on other media like CD in the past or to skip content that is only available in older formats due to inconveniences like lack of bookmarking, inability to skip to a desired chapter, and the player not remembering your position between sessions.

Now I've not been one to let these things stop me, I figure there always has to be a way to author advanced audiobooks if you try hard enough. I know it's possible to convert books in a very basic way simply by merging all of the CD tracks into one file and converting it to M4A (AAC) and renaming the file extension and I've done so in the past. Unfortunately the only thing that helps with is the basic player position memory and iTunes doesn't include a way to merge the CD tracks into a single file. There used to be applications for converting and adding chapters/art but unfortunately they all depended on old versions of iTunes and on Quicktime which can no longer even be installed on Windows.

Fortunately, OpenSource is here to rescue us. The rest of this post will be a full soup-to-nuts writeup on setting up and using the excellent m4b-tool including advice on sourcing chapter titles and including a real example.

Setup

Ubuntu

For my implementation I created an Ubuntu VM because the Windows instructions/install path looked... less than optimal. Honestly my advice is that it's simpler/easier to spin a Ubuntu VM using Microsoft's new VM QuickCreate tool than to mess around with installing PHP and using non-official releases of libraries. If I find myself overburdened with free time at some point maybe I'll write a Chocolatey package that deals with all of that.

  1. Run the installation command from the m4b-tool readme
# install all dependencies
sudo apt install ffmpeg mp4v2-utils fdkaac php-cli

# install / upgrade m4b-tool
sudo wget https://github.com/sandreas/m4b-tool/releases/download/v.0.4.0/m4b-tool.phar -O /usr/local/bin/m4b-tool && sudo chmod +x /usr/local/bin/m4b-tool

# check installed m4b-tool version 
m4b-tool --version

NB: On Ubuntu in order to use the higher quality fdk-aac encoder you need to manually recompile ffmpeg. For reference on that see the m4b-tool readme or this guide

Mac

Because one of the target readers of this post is a Mac user I will also include the setup instructions for OSX.

  1. Start by installing the Homebrew package manager for OSX. You can do so most easily by running the below command (from their official website) in a Terminal prompt.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  1. Copy and paste these installation commands into your prompt (from the m4b-tool readme) in order to use the brew command to perform the installs.
# install ffmpeg with best audio quality options
brew tap varenc/ffmpeg
brew tap-pin varenc/ffmpeg
brew uninstall ffmpeg
brew install ffmpeg --with-chromaprint --with-fdk-aac

# tap m4b-tool repository
brew tap sandreas/tap
brew tap-pin sandreas/tap

# install m4b-tool
brew install m4b-tool

# check installed m4b-tool version
m4b-tool --version

Workflow

Organize the files

Put them all in a folder with the book title for a name. Make sure they sort in the correct order (add padding zeroes if necessary)

2. Find the Chapter Names/Count of Chapters

Find them on a website like this one:

3. Find a Cover Image

Googling mostly works, but always try to find the highest quality copy available. I was able to source the highest quality version from the product page of this audiobook.

Once found, name the file cover.jpg and place it in the directory with the source files.

4. Create a Description file

Save the book's description into a new text file called description.txt and save it also into the source directory. This file must be saved as a UTF-8 text file according to the readme.

5. Merge the Audio Files

Use the m4b-tool to merge all of the files into one. The first path (directly after the merge command) should be the path to the folder which contains all of the individual input audio tracks. Second path should be where you want the file to come out, including the filename and extension. Make sure not to accidentally overwrite anything here by making sure that nothing already exists with that desired name in the folder in question.

m4b-tool merge --name="Book Title" "/path/to/my-audio-book/" --output-file="/path/to/merged.m4b"

A useful bonus tip: Here is an example from the m4b-tool documentation outlining how to make sure that books in a series are sorted correctly in your media player rather than alphabetized. The --series="Series Name" --series-part="1" portion from the example below can be added to whatever you have already worked up for the above command

m4b-tool merge --name="Harry Potter and the Chamber of Secrets" --series="Harry Potter" --series-part="2" --output-file="/path/to/output/Harry Potter and the Chamber of Secrets.m4b" "/path/to/input/Harry Potter and the Chamber of Secrets"

6. Find the Chapter Timecodes

This is the most time consuming part. Open the merged audio file (Recommended: Use Audacity or some other audio editing tool which both shows the timecode down to three decimal places and which lets you visually see the waveform which makes it much easier to find the dead air/quiet spots which are usually the chapter breaks).

When selecting the time signature for a chapter marker, wherever possible try to put it at least a few milliseconds and up to maybe half a second into the silence before the reading of the chapter heading begins. Obviously this will vary depending on how generous the chapter pauses are.

I find it easiest to keep track of the timecodes and chapters in a spreadsheet as shown below. As you can see I've added an "intro" and "outro" section, these really help listeners to do things like skip back to the start of chapter 1 without having to listen to the "opening credits" portion again and the marking of any outro credits and/or preview of the next book can aid users in knowing how much actual book they have left.

7. Prepare the Timecodes

Once the timecodes have been collected they need to be saved to a new text file named chapters.txt formatted like so:

00:00:00.000 Intro
00:04:19.153 This is
00:09:24.078 A way to add
00:14:34.500 Chapters manually

You now have two options.

a) Put the chapters.txt file into the directory with your merged m4b file and run the below command, appending the chapter info to the existing file

mp4chaps -i /path/to/merged.m4b

b) Put the chapters.txt file into source folder with the other text files and your master files and re-run your command from Step 5, performing a clean export all over again - this time with chapter data. The benefit here is that you can then zip up and archive the whole source folder for this book and know that you can use it again with this tool in future with no further fiddling needed should you ever need the data.

8. Other notes

Conclusion

asdf

@clee
Copy link

clee commented Jul 31, 2019

m4b-tool seems to support automatic chapter detection/embedding in v0.4.1, which saved me a ton of time. (I converted an audiobook that had one MP3 per chapter, and it worked perfectly for me).

@Chirishman
Copy link
Author

Chirishman commented Aug 5, 2019

@clee It does support a few ways of doing that, however if you use the silence detection method then it doesn't really distinguish between chapters, intros/outros, and any embedded preview of the next book which may be present. Also, for the book which I specifically was using as an example the tracks didn't line up with the chapter divides so that one wasn't viable for me for this book. I was trying to give a set of instructions which would work for as many potential scenarios as possible including (such as ripping and converting from a cassette which I did last year).

Here's the finished post with the embedded images etc:

https://blog.chiriserv.org/Advanced-Audiobook-Generation/

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