Skip to main content

A tool for converting PDFs to M4B audiobooks using AWS polly.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pdf2m4b-0.1.2.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pdf2m4b-0.1.2-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file pdf2m4b-0.1.2.tar.gz.

File metadata

  • Download URL: pdf2m4b-0.1.2.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.11.6 Linux/6.8.0-51-generic

File hashes

Hashes for pdf2m4b-0.1.2.tar.gz
Algorithm Hash digest
SHA256 30d873b75276bb2979f407a322d7d6744afc1c2e72e248f15d2fca292c6f3b0f
MD5 1da4c678e96dac97dcc1ef294e4ddbcb
BLAKE2b-256 69181d0acd1ce64e20e05840b5649e70663cc2bb6e8e37e034dc3c2b139f044b

See more details on using hashes here.

File details

Details for the file pdf2m4b-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pdf2m4b-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.11.6 Linux/6.8.0-51-generic

File hashes

Hashes for pdf2m4b-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 824d0c8580095d785bd5fb6c6ebde644f544543daae6b9c0d2a73fe1f80446b0
MD5 f29aaabf420625724ccda621e2952df9
BLAKE2b-256 d048dd7164957c9ecb745ed8edc9d75a425eb6a66d05ed7729f0984e4a328891

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page