Crazy Idea #1: Converting My Blog Posts In Audio Files

Yesterday, I discovered the say command on my Mac. Its aim is to convert text to audible speech, and it works really well. Then, I had the idea to create audio files from my blog posts, because it’s fun, and because it could improve accessibility.

The say command generates aiff audio files, which is not really understandable by the audio html5 tag. So, I had to convert this output in a mp3 file. I used the well-known ffmpeg tool, and that’s all!

Please, welcome Speaker, my fun work of last evening. Speaker aims to convert my blog posts in markdown syntax to a mp3 file. This is a tiny shell script I enjoyed to write.

USAGE:
  ./speaker [-h] [-d <output directory>] <filename>

I used roundup to test it. I love shell scripts, but to write them without any tests is a pain, there is often a condition which is not good, a typo, or something else. That often makes me crazy! roundup helps you write strong shell scripts. Here is my test suite output:

$ ./speaker-test.sh
speaker
it_shows_help_with_no_argv:                      [PASS]
it_shows_help_with_h_option:                     [PASS]
it_creates_mp3_file:                             [PASS]
it_creates_mp3_file_in_existing_directory:       [PASS]
it_creates_mp3_file_in_new_directory:            [PASS]
=========================================================
Tests:    5 | Passed:   5 | Failed:   0

To sanitize the text to speech, I used some regular expressions. It probably needs some improvements but it works pretty well:

# Sanitize markdown content
content=`echo "$content" | sed -e 's/[\*_]//g'`
content=`echo "$content" | sed -e 's/\[\(.*\)\](\(.*\))/\1/g'`
content=`echo "$content" | sed -e 's/:[p|D]/./g'`
...

For the other parts, check the code :)

As I wanted to provide my blog posts as audio files, I tweaked my templates to integrate an audio html5 tag. And I used html5media to render this tag in all major browsers (don’t know if there is a better solution).

<audio src="/mp3/my-blog-title.mp3" controls preload></audio>

And to automagically generate audio files, I wrote a pre-commit script to build the audio file when I commit blog posts. That’s all folks!

By the way, if you found a typo, please fork and edit this post. Thank you so much! This post is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

If you like this post or if you use one of the Open Source projects I maintain, say hello by email. There is also my Amazon Wish List. Thank you ♥

Comments

Fork me on GitHub