Codecasting

The best way to explain code to a group of people is by walking through it. If they’re far away in space or time, then a video is the next best thing.

The trouble with videos, though, is that they’re big. I can’t host them on my server – I’d need YouTube. Editing them is tough. You can’t copy & paste code from videos. And so on.

One interesting alternative is to use presentations with audio. Slideshare, for instance, lets you share slides and sync it with audio. That almost works. But it’s still not good enough. I’d like code to be stored as code.

What I really need is codecasting: a YouTube or Slideshare for code. The closest I’ve seen until day-before was etherpad or ttyrec – but neither support audio.

Enter Popcorn. It’s a Javascript library from Mozilla that, among other things, can fire events when an audio/video element reaches a particular point.

Watch a demo of how I used it for codecasting

 

A look at the code will show you that I’m using two libraries: SyntaxHighlighter to highlight the code, and Popcorn. The meat of the code I’ve written is in this subtitle function.

function subtitle(media_node, pre_node, events) {
  var pop = Popcorn(media_node);
  for (var i=0, l=events.length; i<l; i++) {
    for (var j=0, line_selector=[], line_no; line_no=events[i][1][j]; j++) {
      line_selector.push(pre_node + ' .number' + line_no)
    }
    var start = events[i][0]
      , end = i<l-1 ? events[i+1][0] : events[i][0]+999;
    (function(start, end, selector) {
      pop.code({start: start, end:end,
        onStart: function(o) { $(selector).addClass('highlighted'); },
        onEnd: function(o) { $(selector).removeClass('highlighted'); }
      })
    })(start, end, line_selector.join(','));
  }
}

When called like this:

subtitle('#audio', 'pre', [
  [ 1, [1,2,3]],
  [ 5, [4,5,6]],
  [ 9, [7,8]],
])

… it takes the #audio element, when it plays to 1 second, highlights lines 1,2,3; at 5 seconds, highlights lines 4,5,6; and so on.

Another thing that helped was that my iPad has a much better mic than my laptop, and ClearRecord is a really simple way to create recordings with minimal noise. [Note to self: sampling at 16KHz and saving as a VBR MP3 (45-85kbps) seems the best trade-off.]

With these tools, my time to prepare a tutorial went down from 4 hours to half an hour!

  1. Veera says:

    Great idea for an web app/platform!

  2. S Anand says:

    Want to try building one? :-)

  3. Tanu says:

    Good one..
    Want to experiment one..

  4. vivek says:

    Excellent! Will start using this right away! Thanks Anand.