Created
October 7, 2013 11:32
-
-
Save sthawali/6866375 to your computer and use it in GitHub Desktop.
Extract source and video id from Youtube and Vimeo url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Extract vido id and source from Youtube and Vimeo | |
*****************************************************/ | |
function extractVideoUrl(url) { | |
var vimeoRegEx = /https?:\/\/(?:www\.)?vimeo.com\/(?:channels\/|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|)(\d+)(?:$|\/|\?)/; | |
var youtubeRegEx = /(?:https?:\/{2})?(?:w{3}\.)?youtu(?:be)?\.(?:com|be)(?:\/watch\?v=|\/)([^\s&]+)/; | |
function getId(url, type) { | |
var match = url.match((type === 'youtube') ? youtubeRegEx : vimeoRegEx); | |
alert(match); | |
if(match && type === 'youtube' && match[1]) return match[1]; | |
else if(match && type === 'vimeo' && match[3]) return match[3]; | |
} | |
if(url.indexOf("youtube.com/watch") != -1 || url.indexOf("youtu.be") != -1) | |
return { provider: 'youtube', id: getId(url, 'youtube') }; | |
if(url.indexOf("vimeo.com") != -1) | |
return { provider: 'vimeo', id: getId(url, 'vimeo') }; | |
return {}; | |
} | |
/* How to use | |
***************/ | |
// var video = extractVideoUrl("http://www.youtube.com/watch?v=jo_B4LTHi3I"); | |
// console.log(video); | |
// var video = extractVideoUrl("http://vimeo.com/52691590"); | |
// console.log(video); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment