Last active
January 7, 2025 06:58
-
-
Save ostechnix/77b89b53877bf492cef35cc59f64a795 to your computer and use it in GitHub Desktop.
Audiogenipy - A Python Script to Create an Audiobook from a Text File using Python and gTTS in Linux, macOS and Windows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment