How I created my own audiobook with few lines of python.

Recently, I came across this awesome python awesome library known as gTTS(Google text-to-speech). The purpose of this library is to convert written text to audio in mp3 format. The usage is also very easy just install the library from https://pypi.org/project/gTTS/. Then import the class from the package, create an object using constructor and save it as a mp3 file.

Installing gTTS.
saving “hello world” to hello.mp3

Now here I am creating a function so that I can pass any text and can save to any filename. This will come handy when we are trying to create our own audiobook.

Now only gTTS is not enough we need a generic data source from which we can get some paragraph of text which will be more interesting to hear. To take advantage of this to hear some generic data we need to call api. For that we have another great library called requests. The usage is also pretty simple, based on the type of call post or get we can use the particular method.

using requests library

Now there are very limited libraries from we can get paragraphs of text to hear. But there are certain workarounds to get paragraphs of text from internet through python but it very much depends on what we want to hear. If we want to hear tweets then you can use Tweepy to fetch tweets of your favourite account.

Here I want to hear some blogs or poems. So what I want to do here is I will have a simple console app where you will just enter the link of your favourite blog and the filename in command line, the app will fetch your text from blog, parse the text and save it to a file which you have mentioned.

For the above requirement we will use a popular technique called Web scrapping. We have a popular library for web scrapping as well called beatuifulsoup. Now beautiful soup offers many methods for working around different types of html elements. But for now we will just extract text.

Using beautiful soup

Now since we got all the tools we can go through the entire process but before that we can list all our dependencies in a requirements.txt file

listing all dependencies and installing it with pip.

So now we have all our dependencies the overall process will be to download raw html data using requests library parsing the data to get text from the html data using beautifulsoup and passing text to gTTS so that we can save it to the audio file. Now putting it all together in a single file as an application known as app.py.

Contents of app.py

Now the above script expects you to pass two arguments first one the filename and the second one the url of the blog you want to read oops sorry you want to hear. We can catch arguments passed from command line using sys package. So generally the arguments will be present in sys.args array. The first argument will be filename of the script we are executing in this case app.py

Running app.py

After running this command you may expect some delay which will depend on size of the blog you are trying to fetch.

But there are few limitations to this code. I wanted to make a generic audio book creator which can take url of any blog like medium, wordpress or any other site and create your blog. This won’t be possible, well you will get the content but with the that you will get some useless data as well. To avoid that you need to use more of beautiful soup to extract the actual data from a particular site. As in some blogs the actual content may be present in a certain p tag or a div tag. Which will be very difficult to predict. We can also see if use nltk to summarise certain paragraphs. But that I may cover in some future topic.

Here is the github repo.

If you the reader wants to go deep into the libraries mentioned above then you should go through the below links

https://www.crummy.com/software/BeautifulSoup/bs4/doc/

So that’s it for now. I hope you learnt something from this post. Until next time.