Get video id from Vimeo url

一笑奈何 提交于 2019-12-02 18:03:18
Martin Ender

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

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 )

var r = /(videos|video|channels|\.com)\/([\d]+)/,
    a = "https://vimeo.com/album/2222222/video/11111111";

console.log(a.match(r)[2]); // 11111111;

http://jsbin.com/asuqic/7/edit

The following regex will return the id on group 1 for those who only want to retrieve this information. It works with all the example URLs given in the question. It also works without http:// and https://. It also works with URLs like player.vimeo.com/...

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

For php, below code works for following urls

i.e. :

http://vimeo.com/88888888
http://player.vimeo.com/video/88888888
http://vimeo.com/channels/staffpicks/88888888
http://vimeo.com/groups/groupsname/88888888

PHP Code :

    $vimeoUrl = 'http://vimeo.com/channels/channelsName/11111111';
    $fetchVimeoIdArr = explode('/', $vimeoUrl);
    $idCounter = count($fetchVimeoIdArr) - 1;
    $vimeoId = $fetchVimeoIdArr[$idCounter];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!