There was an error in this gadget


Wednesday, June 18, 2008

Convert text files to MP3 under Linux

Even though I am not a fan of audio books and the only thing I use my iPod for is listening music still while exploring Festival(which is basically a General multi-lingual speech synthesis system) I found out that it's very easy to convert normal text files into mp3 using festival . Now this could be especially useful for people who either listen to audio books or people who have difficulty in reading.

First we would need to install festival. We can install festival with the following command :

sudo apt-get install festival

Now we need to install voices which would be used to dictate the text (install any one of the following ) :

To install British Accented male voice issue the following command :

sudo apt-get install festvox-rablpc16k

To install American Accented male voice issue the following command:

sudo apt-get install festival festvox-kallpc16k

Now once you have installed festival and voice package we are ready to convert text files .wav files .

Lets say you want computer to read file named kaddu.txt you would need to pipe(send output of one command to other ) the cat command to festival. You can do so by issuing the following command :
cat -A kaddu.txt | festival --tts
Now instead if you want to convert kaddu.txt to kaddu.wav you can do so by piping the output of cat to a tool that comes with festival text2wave. You can do so by issuing the following command in the terminal window :
cat -A kaddu.txt | text2wave -o kaddu.wav
Now however if you want to remove special characters and want festival/text2wav to read only a to z,A to Z, space, “,”,”.”,”!”,”?”. You can do so by piping the output of cat to sed and then piping the output of sed to text2wav/festival.
cat kaddu.txt |sed 's/[^a-zA-Z .,!?]//g'|festival --tts

cat kaddu.txt|sed 's/[^a-zA-Z .,!?]//g'|text2wave -o kaddu.wav
Now the (.wav) file produced is uncompressed and are huge in size so you might like to convert it to mp3 to reduce it's size and to allow the files to be played on iPod or any other mp3 player. You can convert .wav files easily to (.mp3) file format by using lame .

To install lame issue the following command in the terminal window :
sudo apt-get install lame
after istallation is over to convert say kaddu.wav to kaddu.mp3 issue the following command :
lame -f kaddu.wav kaddu.mp3
NOTE : I am using -f switch here which basically encodes mp3 file faster and in lower quality .

Article Written by : Ambuj Varshney (
For Desktop on Linux Blog ,
(C) 2008 , Ambuj Varshney


Matthew M. Kaufman said...

Thank you very much for this post! It's the first one I've found on this topic. It's very descriptive and excellent too. Especially for your sed example. Plus the regular expression command line parsing in general. That saves a ton of wonder and difficult work, with a simple simple and now obvious fix (that I wouldn't have thought of, actually. I would have probably cleaned up text file to input by PHP..importing it clean. maybe this is better).

Anyway. Do you have more on this topic? My email is matt at mattkaufman dot dot dot or comma whatever they use (dot) org

Anonymous said...


Lennon V. C. said...

Thanks for this post now I can study in the car.

Anonymous said...

Excellent and very descriptive posting. Thanks for taking the time to create it. It was most helpful.

Post a Comment