Generating an audiobook from a text file is a fun and practical project that you can easily accomplish using Python. Whether you want to listen to your favorite book, a long document, or even your own writing, Python makes it simple to convert text into speech. In this step-by-step guide, we will explain how to create audiobooks from text files using a Python script called Audiogenipy in Linux. The steps given below should also work on macOS and Windows as well.
Table of Contents
What You’ll Need
Before we proceed, here’s what you’ll need:
- Python Installed: Make sure Python is installed on your computer. In Linux, Python is pre-installed. If not, you can just install using your default package manager, for example
sudo apt install python
in DEB-based systems. - Audiogenipy: A Python script to convert text files into audiobooks.
- gTTS Library: We’ll use the
gTTS
(Google Text-to-Speech) library to convert text into speech. You will need Pip to install this library. - A Text File: Prepare a text file containing the content you want to convert into an audiobook.
What is Audiogenipy?
Audiogenipy is a simple Python script to convert text files into audiobooks effortlessly. Under the hood, Audiogenipy uses the Google Text-to-Speech (gTTS) library, which leverages Google’s advanced text-to-speech technology to transform any text file into high-quality audio.
The gTTS library provides natural-sounding speech in multiple languages. Unlike some other Google services, you don’t need an API key to use gTTS
. It works directly out of the box.
gTTS is free and opensource library. It uses Google’s free text-to-speech API, which is also free for personal and non-commercial use.
Whether you want to listen to your favorite book, a long document, or even your own writing, Audiogenipy makes it easy to turn text into speech with minimal user input.
Limitations
While gTTS
is free, it does have some limitations:
- Rate Limits: Google may impose rate limits on the number of requests you can make in a given time period.
- No Custom Voices: The library does not support custom or user-specific voices. It only uses Google’s pre-trained voices.
- No Offline Mode:
gTTS
requires an internet connection to work, as it relies on Google’s servers for text-to-speech conversion.
Step 1: Install the gTTS Library
The first step is to install the gTTS
library. Open your terminal and run the following command:
pip install gTTS
This will install the library, which allows you to convert text into speech using Google’s Text-to-Speech API.
Step 2: Prepare Your Text File
Create a text file with the content you want to convert into an audiobook. For example, save the file as my_text_file.txt
. Here’s an example of what the file might look like:
Hello! This is an example of a text file that will be converted into an audiobook. You can add any text you want here.
Make sure the text file is saved in the same directory as your Python script, or note its full path.
Step 3: Create an Audiobook from Text using Python
We can use a Python script named Audiogenipy to convert the text file into an audiobook. This script is very basic with a few lines of code. The code is posted in OSTechNix GitHub Gist page. You can download and modify however you want.
Below is the complete code:
#!/usr/bin/env python3
# ------------------------------------------------------------------
# Script Name: Audiogenipy
# Description: A Python Script to Create an Audiobook
# from a Text File using Python and gTTS.
# Website: https://gist.github.com/ostechnix
# Version: 1.0
# Usage: python audiogenipy.py
# ------------------------------------------------------------------
from gtts import gTTS
import os
import platform
def create_audiobook(text_file, output_file):
with open(text_file, 'r', encoding='utf-8') as file:
text = file.read()
tts = gTTS(text, lang='en')
tts.save(output_file)
print(f"Audiobook saved as {output_file}")
def generate_unique_filename(base_name):
"""Generate a unique filename by appending a number if the file already exists."""
if not os.path.exists(base_name):
return base_name
# Split the base name into name and extension
name, ext = os.path.splitext(base_name)
counter = 1
# Keep incrementing the counter until a unique name is found
while os.path.exists(f"{name}_{counter}{ext}"):
counter += 1
return f"{name}_{counter}{ext}"
# Ask the user for the input text file path
text_file = input("Enter the path to the text file: ")
# Automatically generate the output file name if not provided
if text_file.endswith('.txt'):
default_output_file = text_file[:-4] + ".mp3" # Remove .txt and add .mp3
else:
default_output_file = text_file + ".mp3" # Add .mp3 if not already .txt
# Ask the user for the output file path, or use the default
output_file = input(f"Enter the path to save the audiobook (default: {default_output_file}): ").strip()
# If the user didn't provide an output file name, use the default
if not output_file:
output_file = default_output_file
# Generate a unique filename if the output file already exists
output_file = generate_unique_filename(output_file)
# Call the function to create the audiobook
create_audiobook(text_file, output_file)
# Prompt the user to play the audiobook or exit
choice = input("Do you want to play the audiobook? (yes/no): ").strip().lower()
if choice == "yes":
# Open the audio file using the default media player based on the OS
if platform.system() == "Windows":
os.system(f"start {output_file}") # Windows
elif platform.system() == "Darwin": # macOS
os.system(f"open {output_file}")
elif platform.system() == "Linux":
os.system(f"xdg-open {output_file}") # Linux
else:
print("Unsupported operating system. Please open the file manually.")
else:
print("Exiting. Enjoy your audiobook!")
How the Script Works
- Read the Text File: The script opens the text file you specify and reads its content.
- Convert Text to Speech: The
gTTS
library converts the text into speech. - Save the Audiobook: The speech is saved as an MP3 file (or the user-specified format) at the location you specify.
- User Input: The script prompts you to enter the path to your text file and the desired output file name.
- Play Audiobook or Exit: Finally, it will prompt the user if they want to play the book or exit.
Step 4: Run the Script
1. Download the Audiogenipy script or Copy/paste the contents of this script into a text file with .py
extension, for example audiogenipy.py
.
2. Open your terminal and navigate to the directory where the script is saved.
3. Run the script using the following command:
python audiogenipy.py
4. When prompted, enter the path to your text file and the desired output file name. For example:
Enter the path to the text file: /home/ostechnix/file.txt
Enter the path to save the audiobook (default: /home/ostechnix/file.mp3): file.mp3
5. The script will generate the audiobook and prompt you if you want to play the audiobook or simply exit.
Audiobook saved as file,mp3 Do you want to play the audiobook? (yes/no):
Step 5: Listen to Your Audiobook
Once the script has finished running, you’ll find the audiobook file (e.g., file.mp3
) in the specified location. Open it using your favorite media player and enjoy listening to your text!
Customizing the Script
Here are a few ways you can customize the script to suit your needs:
1. Change the Language
If you want the audiobook in a different language, you can modify the lang
parameter in the gTTS
function. For example:
Tamil:
tts = gTTS(text, lang='ta')
Spanish:
tts = gTTS(text, lang='es')
2. Combine Multiple Text Files
If you have multiple text files, you can modify the script to read and combine their contents before converting them into speech.
3. Add Background Music
You can use additional libraries like pydub
to add background music to your audiobook.
Troubleshooting
- File Not Found Error: Ensure the text file exists and the path is correct.
- Unsupported Language: Check the
lang
parameter in thegTTS
function to ensure it matches the language of your text. - Audio File Not Playing: Ensure the output file is saved in a supported format (e.g., MP3) and your media player can open it.
FAQs
1. Can I use my own voice for the audiobook?
Unfortunately, the gTTS
library does not support custom voices. However, you can record your voice and combine it with the generated speech using tools like pydub
.
2. How do I add background music to the audiobook?
You can use the pydub
library to overlay background music on the generated audio file.
3. Is this script cross-platform?
Yes, the script works on Windows, macOS, and Linux. Just ensure you have Python and the gTTS
library installed.
Conclusion
Creating an audiobook from a text file using Audiogenipy script is a simple and fun project that anyone can do. With the gTTS
library, you can convert any text into speech and save it as an MP3 file. Whether you’re listening to a book, a document, or your own writing, this script makes it easy to turn text into audio.
Give it a try, and let your imagination run wild! You can use this script for personal projects, educational purposes, or even as a tool to make your daily tasks more enjoyable.
Related Read:
- Speech Note - An Offline Speech Recognition, Text-to-Speech, and Translation App for Linux
- eSpeak NG – A Text To Speech Synthesizer For Linux
- How To Use Google Translate From Commandline In Linux
Featured image by Mohamed Hassan from Pixabay.