Listen to Blogs...
Adding a listening feature is always an advantage for readers. I am inspired to see this feature in AWS Blogs and thought:
"Why can't I give voice to my blogs!"
There are so many services out there that provide text to speech API. I have this website hosted on AWS, so I'll prefer to use AWS Service. So I have selected Amazon Polly.
Polly has a simple process. We can paste our text, select language, select voice, and we're done! But for long length texts, we can't use console. We need to use CLI and Polly will upload audio file into S3 bucket.
Let's get hands dirty
We are going to create a small python script. First, read out blog's HTML content and convert it into text.
with open(filePath) as f:
htmlContent = f.read()
filterdHtml = htmlContent[start:end]
h = html2text.HTML2Text()
text = h.handle(filterdHtml)
Great! So now, we have our text content ready. We will now pass this to Polly API. Here is the code snippet.
client = boto3.client('polly')
response = client.start_speech_synthesis_task(
OutputFormat='mp3',
OutputS3BucketName=bucket,
Text=text,
VoiceId='Matthew')
taskId = response['SynthesisTask']['TaskId']
print("TaskId: ", taskId)
I have selected voice 'Matthew'. You can select any other voice as per your choice. Once Polly completes the execution, it will upload an mp3 file to S3 bucket with name taskId.mp3 .
Well done! Now this mp3 should have the voice we required.
How can I do this process everytime!
Yeah, I also don't like manual work. That's why I have created a small script so that whenever I will
update the existing blog or add a new one, it will create an mp3 file for me and place it with the
proper name that my blog can directly use.
(I have now more time to relax 😉)
You are still here, so I think you already got the architecture about How AWS Lambda can be automatically triggered whenever I update or upload my blogs. .
You can always reach out to me if you want to know more. I'll try to help you achieve that.
Did you like what you read? Recommend this post to others!
Want to share something? I would Love to hear from you!