This project provides a set of Python scripts for converting text to speech using OpenAI's API and concatenating multiple MP3 files into a single audio file using ffmpeg
.
Intended use case is to paste your written material into the input.txt file or to generate an input.txt file within the same directory as the code files and run the main.py
script. The script is written in such a way that it will automatically chunk your pasted text by complete sentences so as to not run over the 4096 input token limit - this will create multiple MP3 files which are then joined together using FFMPEG, and this should create a seamless-sounding narrative reading of your text.
The default voice model chosen is fable
, but you can change this to your voice model of choice by modifying the voice
variable inside the convert_text_to_speech
function.
- Text-to-Speech Conversion: Uses OpenAI's TTS model to convert text files into speech and save the output as MP3 files.
- Audio File Concatenation: Combines multiple MP3 files into a single output file using
ffmpeg
.
Before running the scripts, make sure you have the following installed:
- Python 3.x (this program was developed in a 3.12.4 environment)
ffmpeg
(for audio concatenation)- OpenAI API key (for text-to-speech conversion)
Install the required Python libraries by running:
pip install openai toml
- Replace
'your-api-key'
in thesettings.toml
file with your actual OpenAI API key. - If desired, change the
voice
variable formfable
to your desired voice model within theconvert_text_to_speech
function. - Place your text file in the same directory or provide the correct path to the
input.txt
file. - Run the script to generate MP3 files for each chunk of the text:
python main.py
- The generated MP3 files will be saved as
output_part_N.mp3
and automatically concatenated into afinal_output.mp3
file.
Please note: the main.py
script will automatically invoke the audio concatenation function.
However, if you wish to call this separately if you have multiple MP3 files (e.g., output_part_1.mp3
, output_part_2.mp3
, etc.), you can concatenate them into a single MP3 file:
python concatenate.py
- The final concatenated MP3 file will be saved as
final_output.mp3
.
This project is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0).
You are free to:
- Share and adapt the material for non-commercial purposes, as long as you give appropriate credit.
- If you adapt or modify the material, you must distribute your contributions under the same license (CC BY-NC-SA 4.0).
No commercial use of this software is permitted without permission from the author. For commercial inquiries, please contact the author.
For more details, see the LICENSE file.