No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-10 23:10:18 +01:00
assets chore: initial commit 2026-11-10 23:09:54 +01:00
src feat: add app 2026-11-10 23:10:18 +01:00
tests feat: add app 2026-11-10 23:10:18 +01:00
.gitignore feat: add app 2026-11-10 23:10:18 +01:00
Cargo.lock feat: add app 2026-11-10 23:10:18 +01:00
Cargo.toml feat: add app 2026-11-10 23:10:18 +01:00
README.md chore: initial commit 2026-11-10 23:09:54 +01:00

m4b-rs

Merge a folder of audio files into one M4B audiobook with one chapter per file. The binary is m4b.

$ m4b merge ./book/ --author "Jane Doe"

It prints the plan (title, tags, output path, chapter list), asks before encoding, shows a progress bar with speed and ETA, then checks the result.

Commands

Command What it does
m4b merge <inputs>… Merge files or folders into one M4B. Folders are scanned recursively in natural order (2 before 10).
m4b tag <file> [changes] Change tags, chapter names or the cover without re-encoding.
m4b info <file>… Show tags, audio format, cover and chapters.
m4b check <file>… [--decode] Look for broken packet timestamps, chapter gaps and overlaps. --decode also compares the decoded length with the container's.

m4b merge --dry-run prints the plan and writes nothing. m4b <command> --help lists every option.

Retagging

$ m4b tag book.m4b --author "Jane Doe" --narrator "John Roe" --year 2021
$ m4b tag book.m4b --chapter 1=Prologue --chapter 2="Chapter One"
$ m4b tag book.m4b --chapter-names names.txt      # one title per line
$ m4b tag book.m4b --cover cover.jpg --set publisher=Voxa
$ m4b tag book.m4b --narrator ""                  # an empty value removes a tag

tag shows a before/after diff and asks before writing. It copies the audio packets with -c copy into a temp file next to the original and compares the result against the original: same packet count, same timeline end, same track duration, same chapter positions. Only then does the temp file replace the original. Pass -o copy.m4b to leave the original alone, or -n to see the diff only.

--set KEY=VALUE accepts any tag ffmpeg's M4B muxer can write (publisher, sort_name, grouping, copyright and others). Keys it can't write, such as series or isbn, are refused instead of silently dropped.

How the timeline stays correct

Some merge tools join files with ffmpeg's concat demuxer. That demuxer places each file at the duration its header reports. For VBR MP3s without an accurate Xing/LAME header, that duration is an estimate. When the estimate is short, the next file's packets collide with the previous file's, and the muxer nudges them forward by one tick each. The audio is all there but the timeline is compressed after every join. Players that follow timestamps report positions behind the audio, and the error grows with each chapter. When the estimate is long, you get gaps instead.

m4b merge never asks a container how long a file is:

  1. Each input is decoded to PCM by its own ffmpeg process.
  2. The PCM goes through one pipe into a single, continuous AAC encode. The encoder never sees file boundaries, so its timestamps are just a frame counter.
  3. Chapter marks are the running count of samples written into that pipe. They are written in a 1/sample_rate timebase, and the movie timescale equals the sample rate, so marks and durations are sample-exact.
  4. Before the file gets its final name, the output is checked: every packet must advance by exactly one frame, the track must end at the expected sample count, and each chapter must sit where the plan put it.

When a file's header duration disagrees with its decoded length by more than 0.1 s, merge lists it and uses the decoded length.

m4b check reports both failure shapes on existing files:

$ m4b check --decode broken.m4b
  ✗ 296 packets advance by less than a frame, losing 6.821s of timeline in 3 run(s)
    at 0:00:12.584: 121 packets, -2.802s
  ✗ decoded audio is 0:00:24.172 but the container says 0:00:17.342 (+6.830s)

Defaults

  • Title: --title, else the album tag, else the prefix shared by all file names, else the folder name. Trailing "audiobook" words in several languages are dropped ("Dune - Audiobook" becomes "Dune").
  • Chapter names: title tags when every file has a distinct one, else file names with the track number and shared prefix removed. --chapters numbered --chapter-format "Chapter {num}" numbers them instead.
  • Audio: AAC-LC at the most common input sample rate and up to 2 channels. 96 kb/s stereo or 64 kb/s mono. Uses libfdk_aac when ffmpeg has it (about 4× faster than the native encoder here), else ffmpeg's aac.
  • Cover: --cover, else cover/folder/front image in the input folder, else embedded art from the first input that has some.
  • Output: next to the input folder, named after the title. Existing files are not overwritten without --force.
  • Tags: title, album, artist and album artist (author), composer (narrator), date, genre, description, and the iTunes media kind set to Audiobook.

Requirements

ffmpeg and ffprobe on PATH, or set M4B_FFMPEG / M4B_FFPROBE.

$ cargo install --path .

Development

$ cargo test

The integration test in tests/merge.rs builds VBR MP3s without a Xing header, so their header durations are off by seconds, then asserts that every chapter boundary equals the sum of the decoded sample counts. It skips itself when ffmpeg lacks libmp3lame.