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.
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.
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.
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
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.
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
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.
Contribute to Anwesh43/audio-book-with-python development by creating an account on GitHub.
If you the reader wants to go deep into the libraries mentioned above then you should go through the below links
gTTS - gTTS documentation
( Google Text-to-Speech), a Python library and CLI tool to interface with Google Translate's text-to-speech API. Writes…
Requests: HTTP for Humans™ - Requests 2.25.1 documentation
Release v2.25.1. ( Installation ) Requests is an elegant and simple HTTP library for Python, built for human beings…
So that’s it for now. I hope you learnt something from this post. Until next time.