Skip to main content

Tools for converting audio files to M4B format, combining M4B files with chapters, and splitting M4B files by chapters

Project description

M4B Tools

A Python package for converting audio files to M4B format, combining M4B files with chapters, and splitting M4B files by chapters. This package provides both a command-line interface and a programmatic API for comprehensive audiobook management.


About this project:
This project is a (successful!) experiment in using AI to generate an entire Python package from scratch. All code, documentation, and design were created with the help of GitHub Copilot (both Agent mode in VSCode and the coding agent on GitHub web), without manual coding. The process demonstrates the power and practicality of modern AI tools for real-world software development.


Quick Start

Install using pipx for an isolated command-line tool:

pipx install m4b-tools

Or install using pip:

pip install m4b-tools

Prerequisites

  • Python 3.7+: Required for the package
  • FFmpeg: Required for audio conversion and processing
    # Ubuntu/Debian
    sudo apt install ffmpeg
    
    # macOS (with Homebrew)
    brew install ffmpeg
    
    # Windows (with Chocolatey)
    choco install ffmpeg
    

Basic Usage

# Convert audio files to M4B
m4b-tools convert "*.mp3" ./output

# Combine M4B files with chapters
m4b-tools combine "*.m4b" combined.m4b --title "My Audiobook"

# Split M4B file by chapters
m4b-tools split audiobook.m4b ./chapters

API Usage

Use M4B Tools in your Python code:

import m4b_tools

# Convert a single file
success = m4b_tools.convert_to_m4b("input.mp3", "output.m4b")

# Batch convert files with progress
successful, total = m4b_tools.convert_all_to_m4b(
    "**/*.mp3", 
    "./output",
    show_progress_bar=True,
    max_workers=4
)

# Combine M4B files
success = m4b_tools.combine_m4b_files(
    input_pattern="*.m4b",
    output_file="combined.m4b",
    title="My Audiobook"
)

# Split M4B file by chapters
success = m4b_tools.split_m4b_file(
    "audiobook.m4b",
    "./chapters",
    output_format="mp3"
)

# Generate CSV template for metadata
success = m4b_tools.generate_csv_from_folder("./m4b_files")

Development

This project uses Hatch for development environment management.

Installation for Development

# Install hatch
pip install hatch

# Clone the repository
git clone https://github.com/elazarcoh/m4b-tools.git
cd m4b-tools

# Create and enter development environment
hatch shell

Running Tests

# Run all tests
hatch run test

# Run tests with coverage report
hatch run cov

# View coverage report (after running 'hatch run cov')
# Opens HTML report: htmlcov/index.html

The coverage configuration is set up to:

  • Track coverage for the src/m4b_tools package
  • Generate both terminal and HTML reports
  • Exclude common utility lines from coverage analysis

Command-Line Interface

After installation, use the m4b-tools command:

# Show help
m4b-tools --help

# Convert audio files to M4B
m4b-tools convert "**/*.mp3" ./output
m4b-tools convert "books/**/*.flac" ./converted -p -j 4

# Generate CSV template for combining
m4b-tools generate-csv ./m4b_files

# Combine M4B files using pattern
m4b-tools combine "*.m4b" output.m4b --title "My Book"

# Combine M4B files using CSV
m4b-tools combine --csv book_files.csv

# Split M4B files by chapters
m4b-tools split "*.m4b" ./output_chapters

# Dump M4B metadata as CSV (one row per chapter, all metadata repeated per row)
m4b-tools metadata audiobook.m4b --output metadata.csv

Advanced API Usage

For more complex use cases, the API provides additional options:

import m4b_tools

# Advanced conversion with custom options
successful, total = m4b_tools.convert_all_to_m4b(
    input_pattern="audiobooks/**/*.flac",
    output_dir="./converted",
    base_input_path="/path/to/audiobooks",  # Preserve directory structure
    flat_output=False,  # Keep nested folders
    show_progress_bar=True,
    max_workers=4,
    verbose=True
)

# Combine with CSV metadata file
success = m4b_tools.combine_m4b_files(
    csv_file="metadata.csv",  # Advanced metadata control
    preserve_chapters=True,   # Keep existing chapters
    temp_dir="./temp"
)

# Split with custom naming template
success = m4b_tools.split_m4b_file(
    input_file="audiobook.m4b",
    output_dir="./chapters",
    output_format="flac",
    naming_template="{author}/{book_title}/Chapter {chapter_num:02d} - {chapter_title}.{ext}",
    max_workers=4
)

CSV Format for Advanced Metadata

The CSV format allows you to specify detailed metadata and chapter titles:

# Metadata rows (optional, start with #):
#title,My Audiobook Title
#author,Author Name
#narrator,Narrator Name
#genre,Fiction
#year,2024
#description,Book description
#output_path,/path/to/output.m4b
#cover_path,cover.jpg

# Data rows:
file,title
chapter01.m4b,Introduction
chapter02.m4b,The Beginning
chapter03.m4b,The Middle
chapter04.m4b,The End

Cover Art Support:

  • Local files: #cover_path,/path/to/cover.jpg
  • URLs: #cover_path,https://example.com/cover.jpg

Features

Conversion Features:

  • ✅ Convert various audio formats (MP3, FLAC, M4A, AAC, OGG, WAV, WMA) to M4B
  • ✅ Batch conversion with parallel processing
  • ✅ Preserve directory structure or flatten output
  • ✅ Progress bars and verbose logging

Combination Features:

  • ✅ Combine multiple M4B files into a single audiobook
  • ✅ Chapter-based organization
  • ✅ Advanced CSV metadata control
  • ✅ Cover art support (local files and URLs)
  • ✅ Preserve existing chapter structure

Splitting Features:

  • ✅ Split M4B files by chapters into multiple formats
  • ✅ Support for MP3, M4A, M4B, AAC, OGG, and FLAC output
  • ✅ Flexible naming templates with metadata variables
  • ✅ Nested directory structure support
  • ✅ Metadata preservation in split files
  • ✅ Parallel chapter extraction for performance
  • ✅ Automatic filename sanitization
  • ✅ Chapter detection from M4B files
  • ✅ Fallback to single chapter for files without chapters
  • ✅ Comprehensive error handling and logging

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

m4b_tools-1.0.0.tar.gz (30.2 kB view details)

Uploaded Source

Built Distribution

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

m4b_tools-1.0.0-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file m4b_tools-1.0.0.tar.gz.

File metadata

  • Download URL: m4b_tools-1.0.0.tar.gz
  • Upload date:
  • Size: 30.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for m4b_tools-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c2422cc2496def229c1ff1504febc22e86a2c989250d94c2d1e7e637eaf23b34
MD5 a8c4330372464d239a59a80d4a4d0572
BLAKE2b-256 f76e539e18e20dec2dc7879efa79b4f254ae16eac11272aaa09fb8baae92ae60

See more details on using hashes here.

File details

Details for the file m4b_tools-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: m4b_tools-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for m4b_tools-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8317b78699da813ef26d9153e25578ba7e02f98760e5a32f8f703224f46b619
MD5 c0ebf6a668fb9af4873684d8610a256f
BLAKE2b-256 1cc753590470fc2861c6e8a4d52732918b8f482c70fa79c539bf4a8bf72c4e8a

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