Home

Awesome

:warning: DEPRECIATED. USE auto-m4b DOCKER

This has been packaged into an easy to use docker. Consider all information below outdated.

M4B-Tool Automation

Table of Contents

  1. Overview
  2. Linux Install
    1. Install m4b-tool
    2. Docker Run Command
    3. Create the auto-m4b-tool.sh script
    4. Running the auto-m4b-tool.sh script
  3. Windows Install
  4. Tagging
  5. Improvements
  6. Notes

Overview:

A method to watch a directory for newly added audiobooks, which triggers a script that converts the contents of that new folder into an m4b, and saves it to mp3tag's default directory ready for post processing.

I use multiple OSes for this particular automation. As such, this particular method may be impractical to some, but there's nothing special about these steps that requires multiple computers, and everything could be done solely on windows.

Please consider contributing a Windows Only method if you write a similar windows script.

Computer Architecture

This is how my system is set up. I have a fileserver/nas that uses SMB to share my folders with all my computers.

ComputerOSNoteable Installs
Fileserver VMOMV (debian linux)SMB network share, MergerFS+SnapRAID
Docker Server VMUbuntu 20.04M4b-tool docker and auto-m4b-tool.sh script
HTPCWindowsMp3tag, Dropit

Important folders:

Automated workflow:

  1. Newly acquired audiobooks are put in /original
  2. Auto Copy new books to appropriate folder based on filetype
    • If book is already an m4b, then copy to /untagged/Book1.m4b
    • If book is mp3, then copy to /mp3merge/Book1/*.mp3
  3. Every 5 min the auto-m4b-tool.sh script checks /mp3merge for new folders, when found creates a single chapterized M4b
  4. This newly created m4b file is saved to /untagged
  5. /mp3merge/Book1 folder is moved to /delete and the contents of /delete is deleted
  6. Open mp3tag, all books that need processing will be loaded
  7. Use mp3tag audible websource script to tag Keyboard Shortcut = Ctrl+shift+i
  8. Use mp3tag action script to rename/relocate to /audiobooks Keyboard Shortcut = Alt+a 1

Install

First let's prepare the Linux machine (Docker Server VM) . We will be installing the m4b-tool docker, configuring a docker run command, and creating the automation script.

Linux (Docker Server VM):

Install m4b-tool via docker

Docker is by far the easiest way to install and use m4b-tool. Other methods will not be covered in this guide. Run the following 5 commands.

# Install FFMPEG
sudo apt install ffmpeg -y

# clone m4b-tool repository
git clone https://github.com/sandreas/m4b-tool.git

# change directory
cd m4b-tool

# build docker image - this will take a while
docker build . -t m4b-tool

# testing the command
docker run -it --rm -u $(id -u):$(id -g) -v "$(pwd)":/mnt m4b-tool --version

For other methods of installing m4b-tool see https://github.com/sandreas/m4b-tool#installation


Understanding the docker run command

The docker run command is the heart of this operation. There are two sets of variables we need to define that correspond to a Docker portion and a m4b-tool portion of this command. The docker portion requires us to set the paths (-v volumes) that we will be working with. The m4b-tool portion will define how to encode and combine the mp3 files.

Example:


docker run -it --rm -u $(id -u):$(id -g) -v /path/to/temp/mp3merge:/mnt -v /path/to/temp/untagged:/untagged m4b-tool merge "$file" -n -q --audio-bitrate="$bit" --skip-cover --use-filenames-as-chapters --audio-codec=libfdk_aac --jobs=4 --output-file="$string3" --logfile="$string5"

Options Explained:

All you need to do is update the two -v paths to your own


Create the auto-m4b-tool.sh script

This is a Linux Bash script. You will need to update lines 23, 24, 25, and 31 of the script to your specific directories everywhere you see /path/to/....

#!/bin/bash
# set n to 1
n=1
# continue until $n  5
while [ $n -ge 0 ]
do
	if    ls -d */ 2> /dev/null
	then
		echo Folder Detected
		string1="/untagged/"
		string2=".m4b"
		string4=".log"
		for file in *; do
			if [ -d "$file" ]; then
		mpthree=$(find "$file" -maxdepth 2 -type f -name "*.mp3" | head -n 1)
		string3=$string1$file$string2
		string5=$string1$file$string4
		echo Sampling $mpthree
		bit=$(ffprobe -hide_banner -loglevel 0 -of flat -i "$mpthree" -select_streams a -show_entries format=bit_rate -of default=noprint_wrappers=1:nokey=1)
		echo Bitrate = $bit
		echo The folder "$file" will be merged to  "$string3"
		echo Starting Conversion
		docker run -it --rm -u $(id -u):$(id -g) -v /path/to/temp/mp3merge:/mnt -v /path/to/temp/untagged:/untagged m4b-tool merge "$file" -n -q --audio-bitrate="$bit" --skip-cover --use-filenames-as-chapters --audio-codec=libfdk_aac --jobs=4 --output-file="$string3" --logfile="$string5"
		mv /path/to/temp/mp3merge/"$file" /path/to/temp/delete/
		mv /path/to/temp/untagged/"$file".chapters.txt /path/to/temp/untagged/chapters
		echo Finished Converting
		echo Deleting duplicate mp3 audiobook folder
		fi
		done
	else
		rm -r /path/to/temp/delete/* 2> /dev/null
		echo No folders detected, next run 5min...
		sleep 5m
	fi
done
cd /path/to/temp/mp3merge
nano auto-m4b-tool.sh

Run the auto-m4b-tool.sh script

This will run the script in a terminal window. To exit the script type ctrl-c. You can get fancy and run the script as a service, or set a cron job to start this script when the computer starts, but for my pursposes having an extra terminal window open with this running helps to see what's actually happening and keep tabs on the conversion.


Windows (HTPC) Install

So far we have a script that watches a directory (/mp3merge) for new folders, converts the contents of that new folder into an m4b, and saves it to mp3tag's default directory (/untagged) ready for post processing.

Now we need to configure the Windows portion of this process to monitor your /original folder for recent additions and copy mp3 book folders to /mp3merge. I use a program called Dropit to handle this.

Install Dropit

Dropit is a very configurable, lightweight, windows utility, that will help us monitor /original for changes and copy the files and folders to the right locations.

<p float="left"> <img src="https://i.imgur.com/H7MUIar.png" width="40%" /> </p>

Tagging

Return to Guide to configure Mp3tag. Make sure to set /temp/untagged as mp3tag's default folder.

MP3TAG: To verify what chapter titles were set, and if the chapter order is correct, Open Mp3tag, go to Tools > Options > Advanced > CHECK "List chapters as separate files". You can close mp3tag, then reopen, and you should see all of the chapters as if they were separate files.

WARNING: If you are using the option of seeing all chapters as files, saving the tags to file will take a very long time. Once you verified that every thing looks good, best to switch back to only seeing the m4b as a single file (UNCHECK, close, and reopen).


Improvements


Notes:

There are many ways to customize this workflow. I'd love to hear what you've come up with.

The script will sample one of the mp3 files and dynamically set the bitrate based on the source material. Eg. book1.mp3 is 64k then book1.m4b is 64k, book2.mp3 = 128k, book2.m4b = 128k, etc. The output AAC will be variable bitrate (VBR), no way to force CBR https://github.com/sandreas/m4b-tool/issues/55

Huge shout-out to sandreas who created the amazing m4b-tool and to tylerdotdo for sharing the original auto-m4b-tool.sh script!


<a href="https://www.buymeacoffee.com/seanap" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-green.png" alt="Buy Me A Book" height="41" width="174"></a>