As Jeffrey mentioned, LAME can do that, but it's not true that you have to run it as an executable, you can build it as a library. You can even use it in commercial software as long as you dynamically link it.
http://lame.sourceforge.net/about.php
There are numerous open-source packages that use LAME which you can use as examples:
http://lame.sourceforge.net/links.php#OpenSource