Skip to content
← All notes

Engineering · · 3 min read

1,189 Chapters of the Bible: mkdir as an Atomic Lock for Any Number of Workers

Twice before, the same batch job had been started twice and the two runs overwrote each other's files. For the Chinese Union Bible I changed how I parallelise: don't split the work, whoever gets the lock does the chapter. Merging the books afterwards brought three more audio/video traps.

Two earlier accidents

With the Brahma Net Sutra and the Golden Light Sutra the same thing happened: the batch script got started twice, two processes wrote frames into the same temp folder, overwrote each other, and the output was broken.

Back then I parallelised by splitting ranges by hand: process A did chapters 1–20, process B did 21–40. Start one extra run by mistake (usually me), or overlap two ranges, and it breaks.

The Chinese Union Bible is 66 books and 1,189 chapters, so parallelism was a must — and I wanted to add or remove workers at any time: fewer during the day when I need the computer, more at night.

Whoever gets the lock does the chapter

The new approach doesn't split anything. Every worker walks the chapters from the start and, for each one, first tries to create a lock directory:

mkdir .lock_0001

The point is that mkdir is atomic on a single filesystem: if two processes create the same directory at once, exactly one succeeds. The winner owns the chapter; the loser skips it and tries the next.

Two more pieces make it work:

  • Chapters that already have a finished video are skipped, so a worker can be restarted any time.
  • A trap removes the worker's own lock when it exits — including when it's stopped halfway — so no chapter stays "taken" forever.

No more hand-assigned ranges. During the run I moved between 1 → 2 → 3 → 1 workers several times; three loaded the machine too much when I needed to use it. Not a single file was corrupted by concurrent writes.

Three traps when merging the books

With all 1,189 chapter videos done, each book was to become one continuous video (Genesis' 50 chapters as one, and so on) — 66 in all. That step had three traps of its own:

  1. AAC can't be stream-copied across files. Each chapter's audio was encoded separately; concatenating with stream copy produced decode errors at every join. Now each chapter is decoded to WAV, concatenated, and encoded once at the end.
  2. No -shortest on the final mux. By design the last chapter's video runs 5 seconds longer than its audio to hold the end card. -shortest trims the output to the audio length and chops most of the end card. A duration check caught it.
  3. Audio folders aren't books. Two audio folders were shared by several books (Romans to Philemon, 13 books, in one; Hebrews to Jude, 8 books, in another). Grouping by folder first merged 13 books into a single video. Chapters are now matched to books by exact title.

After merging, the per-chapter files were deleted to free space — but only after each book passed two gates: the merged duration within 1 second of the sum of its chapters, and a full decode of the file with zero errors. All 66 passed first time, and afterwards I decoded all 66 once more as a final check.

A template you can reuse

  • Don't split ranges; let workers claim units with an mkdir lock and skip what they can't claim.
  • Skip finished units automatically, so workers can be added, removed or restarted at will.
  • Release locks with a trap on exit.
  • To join separately encoded audio, decode to PCM first.
  • Before deleting originals, pass at least two independent checks.