Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get video id from Vimeo url

I'm trying to find the best regexp to fetch vimeo video id from URL.

Example urls :

https://vimeo.com/11111111
http://vimeo.com/11111111
https://www.vimeo.com/11111111
http://www.vimeo.com/11111111
https://vimeo.com/channels/11111111
http://vimeo.com/channels/11111111
https://vimeo.com/groups/name/videos/11111111
http://vimeo.com/groups/name/videos/11111111
https://vimeo.com/album/2222222/video/11111111
http://vimeo.com/album/2222222/video/11111111
https://vimeo.com/11111111?param=test
http://vimeo.com/11111111?param=test

My current regexp which doesn't work:

/http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/

Playground & Test here : http://jsbin.com/asuqic/1/edit?javascript,live

like image 829
l2aelba Avatar asked Nov 08 '12 10:11

l2aelba


People also ask

How do you copy a video URL from Vimeo?

First, go to Vimeo and navigate to the video you want to save. When you've found the page, press Ctrl + L on your keyboard to highlight the URL in the address bar, and then Ctrl + C to copy the Internet address. Once the URL is copied, paste it in the text field below by clicking inside the box and pressing Ctrl + V .

What is video ID number?

The video ID will be located in the URL of the video page, right after the v= URL parameter. In this case, the URL of the video is: https://www.youtube.com/watch?v=aqz-KE-bpKQ.


2 Answers

Update: I notice that this answer is getting some attention every now and then (through votes or comments). The answer is more than two years old, and likely the URL types supported are no longer up to date. I will not actively maintain this regex - it is merely intended as an answer to the question and hence only takes care of the formats listed there. Use this at your own risk, or - even better - use it merely as a starting point to develop your own regex based on an up-to-date and comprehensive list of URL formats.

See @l2aelba's answer for API solution https://stackoverflow.com/a/37695721/622813


This would be the full regex, which also ensures that the format is correct:

/https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/ 

You can now retrieve the group name in capturing group 1 (if present), the album ID in capturing group 2 (if present) and the video ID in capturing group 3 (always).

Demo

like image 135
Martin Ender Avatar answered Sep 20 '22 05:09

Martin Ender


Since 2016. And @Martin Ender said no longer up to date


So here is a API solution : (without regexp but API caller and safe!)

jQuery :

function GetVimeoIDbyUrl(url) {
  var id = false;
  $.ajax({
    url: 'https://vimeo.com/api/oembed.json?url='+url,
    async: false,
    success: function(response) {
      if(response.video_id) {
        id = response.video_id;
      }
    }
  });
  return id;
}

Minify :

function GetVimeoIDbyUrl(e){var i=!1;return $.ajax({url:"https://vimeo.com/api/oembed.json?url="+e,async:!1,success:function(e){e.video_id&&(i=e.video_id)}}),i}

Pure/Native JS : (IE9+ & Modern browsers)

function GetVimeoIDbyUrl(url) {
  var id = false;
  var request = new XMLHttpRequest();
  request.open('GET', 'https://vimeo.com/api/oembed.json?url='+url , false);
  request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
      var response = JSON.parse(request.responseText);
      if(response.video_id) {
        id = response.video_id;
      }
    }
  };
  request.send();
  return id;
}

Minify :

function GetVimeoIDbyUrl(e){var t=!1,o=new XMLHttpRequest;return o.open("GET","https://vimeo.com/api/oembed.json?url="+e,!1),o.onload=function(){if(o.status>=200&&o.status<400){var e=JSON.parse(o.responseText);e.video_id&&(t=e.video_id)}},o.send(),t}

Demo : https://jsbin.com/mevejoxudo/1/edit?js,output

Some type of url doesn't support ? : https://vimeo.com/help/contact#tech-api ( Tell them, dont tell me hehe :D )

like image 25
l2aelba Avatar answered Sep 20 '22 05:09

l2aelba