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.

TODOs

  • example PDFs
  • run more pdfs through it

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.1.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.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pdf2m4b-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 ffae8a51910ed093477a290397d2ee9d15b647d49d60ef2adee4b2ed8b28e505
MD5 847abbc52de4d1bc9744374018259817
BLAKE2b-256 c16635cf7929ef6b620f27dfcb42d8fe73f9499b94e13db27b4210abaaab7d7e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pdf2m4b-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3bb271f0ed31678f8ced11c2d2394b53ae76c6d7b3b879def5e5086193223f6a
MD5 fae0c2345bd56dc0fe5bdd3ce746f878
BLAKE2b-256 fc14ffc7c8545637cba4565fb115152ff03909e02e7fda43f9e8f33976231a5d

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