Skip to main content

pdf2m4b

pdf2m4b is a command-line tool that converts PDF documents into M4B audiobooks using an end-to-end pipeline. It extracts text from PDFs, organizes the content into a structured hierarchy (chapters/sections), synthesizes speech via AWS Polly, and finally combines the audio segments into an M4B audiobook file using FFmpeg.

Features

  • PDF to Markdown: Extract text from PDFs using pymupdf4llm.
  • Structured Chapters: Parse Markdown into a hierarchical folder structure.
  • Text-to-Speech: Generate audio for each chapter with AWS Polly.
  • Audiobook Creation: Combine audio segments into a single M4B audiobook with chapter metadata.
  • Flexible Logging: Uses structlog for logging with options for colorized terminal output or JSON logging.
  • Easy Installation: Available on PyPI and installable via pip.

Usage example

$ export AWS_ACCESS_KEY_ID=AKI...
$ export AWS_SECRET_ACCESS_KEY=7SR...
$ pip install pdf2m4b
$ python -m pdf2m4b.main --pdf ../extracted.pdf
2025-02-03 07:27:58 [info     ] Converting PDF to Markdown     func_name=main markdown=output/output.md module=main pdf=../extracted.pdf
Processing ../extracted.pdf...
[========================================]
2025-02-03 07:28:01 [info     ] Markdown written               func_name=pdf_to_md module=pdf_to_md output=output/output.md
2025-02-03 07:28:01 [info     ] Converting Markdown to folder structure chapters=output/chapters func_name=main markdown=output/output.md module=main
2025-02-03 07:28:01 [info     ] Folder structure created       func_name=convert_md module=md_to_folders output=output/chapters
2025-02-03 07:28:01 [info     ] Starting TTS synthesis         chapters=output/chapters func_name=main module=main
2025-02-03 07:28:01 [info     ] Chunked text                   chunks=3 func_name=process_md_file md_file=output/chapters/02_32_multiplexing_and_demultiplexing/00.md module=tts_polly
2025-02-03 07:28:01 [info     ] Processing TTS chunk           chunk=1 func_name=process_md_file md_file=output/chapters/02_32_multiplexing_and_demultiplexing/00.md module=tts_polly total_chunks=3 words=407
2025-02-03 07:28:06 [debug    ] Chunk synthesized              chunk=1 func_name=process_md_file md_file=output/chapters/02_32_multiplexing_and_demultiplexing/00.md module=tts_polly time=5.158603383999434

...

...

2025-02-03 07:28:58 [info     ] Added chapter                  end_ms=1567077 func_name=create_m4b module=make_m4b start_ms=1327299 title='32 Multiplexing And Demultiplexing: Connectionless Multiplexing And Demultiplexing'
2025-02-03 07:28:58 [info     ] Added chapter                  end_ms=1767232 func_name=create_m4b module=make_m4b start_ms=1567077 title='32 Multiplexing And Demultiplexing: Connection-Oriented Multiplexing And Demultiplexing'
2025-02-03 07:28:58 [info     ] Wrote chapter metadata         filename=chapters.txt func_name=create_m4b module=make_m4b
2025-02-03 07:28:58 [info     ] Running FFmpeg                 command='ffmpeg -y -loglevel quiet -f concat -safe 0 -i concat_list.txt -i chapters.txt -map_metadata 1 -c:a aac output.m4b' func_name=create_m4b module=make_m4b
2025-02-03 07:29:26 [info     ] Successfully created M4B file  func_name=create_m4b module=make_m4b output_file=output.m4b
2025-02-03 07:29:26 [info     ] Audiobook creation complete    func_name=main module=main

AWS costs

As of February 2025, it's around $10 per book. The project uses the generative setting of AWS Polly; check an example below.

Example output

See here for an output audiobook, which is the end result. Here is the corresponding input. Here is an intermediate markdown file, and here is a file tree with hierarchical text and audio snippets. It's a rather adversarial example; there are lots of formulas that the initial parsing does wrong, and which are then filtered out before passing the text to the model. It performs quite well if you use it on an ordinary plain text without formulas.

Metadata

Release files for pdf2m4b 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdf2m4b 0.1.2
File Size Uploaded
pdf2m4b-0.1.2.tar.gz 9.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pdf2m4b 0.1.2
File Interpreter ABI Platform
pdf2m4b-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / pdf2m4b-0.1.2.tar.gz

Download URL pdf2m4b-0.1.2.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
30d873b75276bb2979f407a322d7d6744afc1c2e72e248f15d2fca292c6f3b0f
BLAKE2b-256 checksum
How to use checksums
69181d0acd1ce64e20e05840b5649e70663cc2bb6e8e37e034dc3c2b139f044b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.0 CPython/3.11.6 Linux/6.8.0-51-generic

Release files / pdf2m4b-0.1.2-py3-none-any.whl

Download URL pdf2m4b-0.1.2-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
824d0c8580095d785bd5fb6c6ebde644f544543daae6b9c0d2a73fe1f80446b0
BLAKE2b-256 checksum
How to use checksums
d048dd7164957c9ecb745ed8edc9d75a425eb6a66d05ed7729f0984e4a328891
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.0 CPython/3.11.6 Linux/6.8.0-51-generic

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page