Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Generate LAME-encoded audio MP3 from text UNIX
Sometimes I come across an article on the web that I want to read, but I just don't have the patience or time at that precise moment. So I wrote this little script that takes a plain text file as input, and generates an audio MP3 using Vicki's voice. Then you can load the MP3 onto your iPod or other portable player and listen on the run. If you save the following script as txt2mp3 (make it executable, too!), then you simply type txt2mp3 textfile at the Terminal's prompt to convert your file. The script assumes that you have the LAME encoder installed on your system.
#/bin/csh

strings $1 > /tmp/$1.txt
say -v Vicki -f /tmp/$1.txt -o /tmp/$1:r.aiff 
rm -f /tmp/$1.txt
lame --quiet /tmp/$1:r.aiff $1:r.mp3
rm -f /tmp/$1.aiff
[robg adds: An earlier hint covers the use of the say command to convert text files to audio AIFF files.]
    •    
  • Currently 2.60 / 5
  You rated: 4 / 5 (5 votes cast)
 
[13,474 views]  

Generate LAME-encoded audio MP3 from text | 17 comments | Create New Account
Click here to return to the 'Generate LAME-encoded audio MP3 from text' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
skip the aiff
Authored by: fracai on Sep 30, '04 12:22:34PM
skip the aiff step by using named pipes. I'm not sure if named pipes have been covered here but they're nifty :)
#/bin/csh

strings $1 > /tmp/$1.txt
mkfifo /tmp/$1:r.namedPipe

lame --quiet /tmp/$1:r.namedPipe $1:r.mp3 &
say -v Vicki -f /tmp/$1.txt -o /tmp/$1:r.namedPipe

rm -f /tmp/$1.txt
rm -f /tmp/$1:r.namedPipe

I haven't tested this so it might need some adjustments, but something similar works with mplayer to convert real audio dumps to mp3

---
i am jack's amusing sig file

[ Reply to This | # ]

skip the aiff, also missing the bang
Authored by: fracai on Sep 30, '04 01:31:54PM

Thanks for the below post. My mod is also missing the !

---
i am jack's amusing sig file



[ Reply to This | # ]
skip the aiff, also missing the bang
Authored by: st on Sep 30, '04 05:41:22PM
Nope, does not work. As already stated below, lame needs to do some file seeks with AIFF input. So, in this case you definetely need a temporary file. The "safe" way to do that is with the following script

#! /bin/bash
aiff=`mktemp -t aiff.XXXXXXXXXX`;
say -v Vicki -o "$aiff"
lame --quiet "$aiff" -
rm "$aiff"
This script acts as a filter: text in, mp3 out

[ Reply to This | # ]
Generate LAME-encoded audio MP3 from text
Authored by: osxpounder on Sep 30, '04 12:36:52PM
I don't see a link to download the LAME encoder at that link... but I seem to have found it here:

Download LameLib

What I downloaded is a single mysterious file, called "LameLib". No installer, no executable, and no readme.txt, or any docs.

Apparently, if you want to use LAME, you'll first have to join a mailing list and either search, or beg, for help. I see no links to downloadable compiled binaries, so you'll have to have Developer Tools installed, and compile it yourself. There was once a link from a hint here that led to a downloadable binary, but that link is now dead.

I think I'll stick with the say hint; thanks so much for reminding us of it, robg!

---
--
osxpounder

[ Reply to This | # ]

Generate LAME-encoded audio MP3 from text
Authored by: st on Sep 30, '04 05:35:40PM
You can use fink to download lame or any other software coming from the linux world.

[ Reply to This | # ]
Generate LAME-encoded audio MP3 from text
Authored by: ubrgeek on Sep 30, '04 08:24:40PM

Was just about to post the same. Although, I'd recommend Fink Commander if you're not thrilled about command line stuff. It makes it much easier to deal with this kind of stuff.



[ Reply to This | # ]
Generate LAME-encoded audio MP3 from text
Authored by: aramis on Sep 30, '04 12:49:38PM

Don't forget the bang in your hash-bang:

#/bin/csh
should be
#!/bin/csh

Otherwise it'll only work if csh is your shell. If you're using bash, then you literally get file.txt:r.mp3 as the output filename. Add the exclamation, and you get a proper file.mp3.

Thanks for the script!



[ Reply to This | # ]
Generate LAME-encoded audio MP3 from text
Authored by: EddEdmondson on Sep 30, '04 01:02:18PM
Careful with /tmp - see this hint

[ Reply to This | # ]
Generate LAME-encoded audio MP3 from text
Authored by: hypert on Sep 30, '04 01:50:32PM
I combined some of the items mentioned here (except for the named pipe - what's the advantage of them?).

#!/bin/csh

set TMP_TXT=`mktemp`
set TMP_AIF=`mktemp`

strings $1 >! $TMP_TXT
say -v Vicki -f $TMP_TXT -o $TMP_AIF
lame $TMP_AIF $1:r.mp3

rm -f $TMP_TXT
rm -f $TMP_AIF
I have one question, though. What does the ":r" modifier do? To "osxpounder", just type "fink install lame" to get lame installed (assuming you have fink installed, which is almost a requirement IMHO).

[ Reply to This | # ]
Generate LAME-encoded audio MP3 from text
Authored by: hypert on Sep 30, '04 02:02:32PM

Regarding the ":r" modifier, I couldn't find it in MacOS's man pages, but found it under Solaris:

Modifiers
After the optional word designator, you can add one of the
following modifiers, preceded by a :.

h Remove a trailing pathname component, leaving the
head.

r Remove a trailing suffix of the form `.xxx', leaving
the basename.

e Remove all but the suffix, leaving the Extension.

etc.



[ Reply to This | # ]
named pipes
Authored by: fracai on Sep 30, '04 03:10:35PM

I think I'll type up a submission for this after work.

named pipes provide piping support for apps that don't support piping.
for example, you can't pipe the output of mplayer to standard out, so it's hard to redirect converted audio to another app without saving the audio to disk first. instead you can creat a named pipe and save the mplayer output to the pipe and have the second app read from the pipe.

for something like this it saves the hassle of some temp files.

another example, I created a script a short time ago that would dump the real audio stream of a radio program, convert the dump to wav, convert the wav to mp3

there is a wasted dump and wav file now. both of which can take up a significant amount of space, the wav especially.
a named pipe (allong with doing the conversion of the audio with mplayer instead of saving the dump and then converting) allowed me to direct the wav output to a pipe that was being read by lame. this eliminated the biggest temp file, the wav.

I'll write up something intelligable with links after work.

---
i am jack's amusing sig file



[ Reply to This | # ]
Generate LAME-encoded audio MP3 from text
Authored by: osxpounder on Sep 30, '04 07:32:49PM

Thanks for the fink tip, but nope, it doesn't work. I just installed fink. When I run the included Pathsetup program, it reports that fink is installed correctly. Nevertheless, I still get:

$ fink install lame
/usr/bin/sudo /sw/bin/fink install lame
Information about 1740 packages read in 1 seconds.
Failed: no package found for specification 'lame'!

... when I type "fink install lame".

---
--
osxpounder



[ Reply to This | # ]
Generate LAME-encoded audio MP3 from text
Authored by: hypert on Oct 01, '04 11:07:57PM

I've never had problems with fink before. Try "fink list" to see a list of available packages (hundreds!) or "fink list lame" to see the 3 different lame packages. If (when) they show up, you'll be installing lame, which will install lame-shlibs with it.

If fink doesn't seem to know about lame (and those list commands don't show anything), it seems like a bad fink install.

Anyone else have an idea?



[ Reply to This | # ]
Skip the /tmp
Authored by: darrinsmart on Sep 30, '04 03:06:42PM

I'm sure you could do this all on one line:

#!/bin/csh
strings $1 | say -v Vicki -o /dev/stdout | lame --quiet - $1:r.mp3

No need to worry about any temporary files, and the three
tasks will operate in parallel, making it all a bit faster.



[ Reply to This | # ]
Skip the /tmp
Authored by: st on Sep 30, '04 05:31:59PM

No, as far as I know, lame only accepts raw PCM as streaming input. Lame needs file-seek access with input in aiff format, which, consequently, cannot be piped in.

I agree with other comments that temporary files should be created exclusively by using mktemp.



[ Reply to This | # ]
Generate LAME-encoded audio MP3 from text
Authored by: oink on Sep 30, '04 09:01:01PM

My copy of LAME comes from LameBrain, I just make a symbolic link to it at /usr/local/bin.

More often than not, I find it easier to use the clipboard for things like this. It would be nice if it can be done in one line and I can use it as a alias instead.

My version:

pbpaste | say -o x.aiff
lame --quiet x.aiff x.mp3



[ Reply to This | # ]
TextToMP3 does this in a droplet app
Authored by: sveinbjornt on Oct 04, '04 02:35:35AM
Inspired by this hint, I used Platypus to create a Mac OS X application that does this.

It's called TextToMP3 -- all you need to do is to drop a text file on the application and it'll automatically churn out an MP3 file of the spoken text.

[ Reply to This | # ]