A Python program to convert a .txt file, in English, into an MP3 audio file, with choice of English language accent
- Known to work on Python versions 3.6.8, 3.7.13, 3.8.13, 3.9.11, 3.10.0, 3.10.3 and 3.10.8 (at least)
git clone https://github.com/xanderstevenson/txt_2_mp3
cd txt_2_mp3
python3 -m venv venv
source venv/bin/activate # for Mac and Linux
source venv/Scripts/activate # for Windows
pip install -r requirements.txt
*** requirments sometimes vary per OS, so please install other dependencies as your system advises.
$ python3 txt_2_mp3.py
WELCOME to TXT_2_MP3
Enter the path and filename of the .txt file, to convert from text to speech (example: 'readthis.txt').
: readthis.txt
Please choose an English accent (type a number and hit ENTER)
1. English (Australia)
2. English (United Kingdom)
3. English (United States)
4. English (Canada)
5. English (India)
6. English (Ireland)
7. English (South Africa)
: 6
MP3 file created at /txt_2_mp3/mp3s/readthis.mp3
- For a long text, such as several pages, it could take a few minuted for the mp3 file to be rendered.
-
MP3s are always saved to 'mp3s' folder one level down from txt-2-mp3.py. If the 'mp3s' folder does not exist, it will be created.
-
Automatic play of audio sample can be achieved by uncommenting a few lines of code, although overuse could damage your speakers. Mine started crackling after playing these samples hundreds of times while debugging the main function but a system restart cleared it up. Still, use at your own risk. I'm not responsible for any damages.
-
If the filename given does not exist, the program will exit. However, if the choice of accent is not within the range, the default of American English will be applied.
-
.docx files can be used, in certain circumstances. For example, creating a .docx file in your IDE and pasting in code may work, but using a .docx file which has been formatted in MS Word will result in an error.
-
Tip: When pasting from a web page or Word doc, delete the image file names or it will read them (they are often long). Delete long numbers are any phrases which are not normally spoken, such as URLs.
-
Credit: Much was borrowed from John Capobianco's wiktrola project for this: https://www.youtube.com/watch?v=HbHaZRWq3_I