Retrieving Vimeo ID

April 05, 2014

I have a usecase in my side project wherein I needed to get the thumbnail for a vimeo video. Getting this is relatively easy through vimeo’s Video Request API.

I think it’s trickier to extract the video ID from the vimeo URL. There are multiple valid Vimeo URLs. Based on this link, here are a few of the valid links:

  • http://vimeo.com/12345
  • http://player.vimeo.com/video/12345
  • http://player.vimeo.com/video/12345?test=value1
  • http://player.vimeo.com/video/12345/?test=value1
  • http://vimeo.com/channels/vimeogirls/12345
  • http://vimeo.com/groups/shortfilms/videos/12345

I tailored this gist by wwdboer in Ruby to extract the correct id from a given vimeo URL.

def self.vimeo_id(url)
  match = url.match(/https?:\/\/(?:[\w]+\.)*vimeo\.com(?:[\/\w]*\/?)?\/(?<id>[0-9]+)[^\s]*/)
  match[:id] if match.present?

This method will return the id if present, and nil if not.

blog comments powered by Disqus