Skip to content

Instantly share code, notes, and snippets.

@holman
Created October 19, 2015 01:18
Show Gist options
  • Save holman/745e61a2dbb25b993b1a to your computer and use it in GitHub Desktop.
Save holman/745e61a2dbb25b993b1a to your computer and use it in GitHub Desktop.
Drop the length of a particular audio file and junk into your Jekyll page.
# jekyll_media_length / by @holman
#
# Drop the length of a particular audio file and junk into your Jekyll page.
# (See https://twitter.com/Una/status/655856628198014976 for more context).
#
# INSTALL:
#
# brew install taglib
# gem install taglib-ruby
#
# Toss this file into your _plugins directory.
#
# USAGE:
#
# In your HTML page webzone device, do a liquid tag with the path to the audio
# file, like:
#
# {% media_length media/song.mp3 %}
# # => 4:12
#
# YAY NEATO
#
# Have a cupcake, you've earned it.
#
require 'taglib'
module Jekyll
class MediaLength < Liquid::Tag
def initialize(tag_name, filename, tokens)
super
@filename = filename.strip
end
def render(context)
length = nil
TagLib::FileRef.open(@filename) do |file|
properties = file.audio_properties if file
length = properties.length if properties
end
if length
minutes = length / 60
seconds = length % 60
"#{minutes}:#{"%.2d" % seconds}"
end
end
end
end
Liquid::Template.register_tag('media_length', Jekyll::MediaLength)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment