Can ffmpeg merge multiple m4a files into one ~chapterized~ file

audioaudiobookmergeopen source

Given a folder full of 'm4a' files, I can merge them all into one big m4a file using ffmpeg like so:

ffmpeg -f concat -i <(for f in ./*.m4a; do echo "file '$PWD/$f'"; done) \
-c copy ../OUTPUT.m4a

However, what I would like to do is have OUTPUT.m4a contain chapters for each one of the original input files (each chapter just needs a number, not a name).

I have tried googling for this, but everything I have found seems to be about merging video files and MKVs, not m4a files.

Ideally, I would like to be able to do this via the command line, as I have 60+ folders, and would like to be able to script this rather than loading it into an app of some sort.

Any help would be appreciated.

Best Answer

I use mp4v2 in my open-source app to join together iTunes tracks. You can install mp4v2 using Homebrew.

To add chapters, create a text file in the following format:

00:00:00.000 Name of first chapter
00:00:23.500 Name of second chapter
00:02:40.700 Third chapter, etc

You might be able to automate the creation of the chapter file by using ffprobe (see example written in AppleScript and shell script).

For an output file called "OUTPUT.m4a", save the chapters file as "OUTPUT.chapters.txt". Then run mp4chaps -i OUTPUT.m4a in the same directory as the output file to add the chapters to it.