Skip to main content

Automate chunking long texts to produce a single audio file from text-to-speech APIs

Project description

tts-joinery

tts-joinery is a Python library and CLI tool to work around length limitations in text-to-speech APIs.

Since currently-popular APIs are limited to 4096 characters, this library will:

  • Chunk the input text into sentences using the NLTK Punkt module (for better audio by avoiding segments split in the middle of a word or sentence).
  • Run each chunk through the TTS API
  • Join together the resulting output to produce a single MP3 file

Currently only the OpenAI API is supported, with the intent to add more in the future.

Installation

pip install tts-joinery

or use pipx to install as a standalone tool.

Requires ffmpeg for the audio file processing.

Installation may vary depending on your system. On Linux you can use your system package manager. On Mac brew install ffmpeg should work.

Usage

Command-Line Interface (CLI)

The CLI expects to find an OpenAI API Key in a OPENAI_API_KEY environment variable, or in a .env file.

Syntax

ttsjoin [OPTIONS] [COMMAND]

Options

Options:
--input-file FILENAME   Plaintext file to process into speech, otherwise stdin
--output-file FILENAME  MP3 result, otherwise stdout
--model TEXT            Slug of the text-to-speech model to be used
--service TEXT          API service (currently only supports openai)
--voice TEXT            Slug of the voice to be used
--no-cache BOOLEAN      Disable caching
--help                  Show this message and exit.

Commands:
  cache [clear, show]

Examples

  1. Using an input file and specifying an output file:
ttsjoin --input-file input.txt --output-file output.mp3 --model tts-1 --service openai --voice onyx
  1. Using stdin and stdout with default options:
echo "Your text to be processed" | ttsjoin > output.mp3
  1. Each chunk of text is cached for performance when running the same text multiple times, this can be disabled:
ttsjoin --input-file input.txt --output-file output.mp3 --no-cache
  1. Clear cache directory
ttsjoin cache clear

Python Library

You can also use tts-joinery as part of your Python project:

import nltk

from joinery.op import JoinOp
from joinery.api.openai import OpenAIApi

# Only need to download once, handled for you automatically in the CLI
nltk.download('punkt_tab', quiet=True)

tts = JoinOp(
    text='This is only a test!',
    api=OpenAIApi(
        model='tts-1-hd',
        voice='onyx',
        api_key=OPENAI_API_KEY,
    ),
)

tts.process_to_file('output.mp3')

Changelog

v1.0.4 (2024-10-11)

  • Fixed issue with nltk dependency #4
  • Model, voice, and service CLI params are now case-insensitive

v1.0.3 (2024-10-05)

  • Added cache management commands to cli
  • Fixed a bug when running
  • Added end-to-end tests

v1.0.2 (2024-10-03)

  • Fixed crash when running with caching disabled (#3)

Contributing

Contributions welcome, particularly other TTS APIs, check the issues beforehand and feel free to open a PR. Code is formatted with Black.

Test can be run manually. Suite includes end-to-end tests with live API calls, ensure you have an OPENAI_API_KEY set in .env.test, and run pytest. You can install development dependencies with pip install -e .[test]

Contributors

Special thanks to:

License

This project is licensed under the MIT License.

Copyright 2024, Adrien Delessert

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

tts_joinery-1.0.4.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

tts_joinery-1.0.4-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file tts_joinery-1.0.4.tar.gz.

File metadata

  • Download URL: tts_joinery-1.0.4.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for tts_joinery-1.0.4.tar.gz
Algorithm Hash digest
SHA256 8fc29a93c6f3ee3439f1a5efe3d3b77c52044a2f477aac2f8c6f34c2283aa778
MD5 a04ca5125711f7d86cfb918bafa26c9f
BLAKE2b-256 c2b9bd74ca4dfc9872430d71d44d66032b9dc04cde3ed625e0654090957c1c40

See more details on using hashes here.

File details

Details for the file tts_joinery-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: tts_joinery-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for tts_joinery-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2f3c64d2eb2f12e94e039219973fe47ec54b1749c77b6b91bf1a1a3b460207c9
MD5 6ef7cdeaa7689334ceb3a6d9439f122c
BLAKE2b-256 e28708566df9f1d307fec994cbeaa8db59108b8d8ab3b1e09dc0cfde742b0b88

See more details on using hashes here.

Supported by

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