Search code examples
c#youtubevimeo

Get youtube or Vimeo video Id from Video link


Vimeo share video link looks like:

http://vimeo.com/89491724

YouTube share video link looks like:

http://youtu.be/GioRM0kU5m0

I want people to enter this into my website and I'll embed it using their embed code and just replacing the last (id) part with their values (that gets stored in a DB).

I can parse these out by taking the last values after the last slash "/". I can tell what site they are from based on if "vimeo" is in the string. My question is, am I opening myself up to any security issues by doing this? Is there anything a person could do to exploit this system that anyone can think of?

Are there any libraries out there for validation that maybe I'm not aware of?


Solution

  • Yes I have a same problem few months ago and I have made this function to get video id for youtube and Vimeo..

    Here is your Solution..

    function Getvideoid(videolink)
    
    var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
    var match = videolink.match(regExp);
    
      if (match && match[7].length == 11) 
        {
          alert("youtube video id : "+ match[7]);      
          return;
        }
        regExp = "vimeo\\.com/(?:.*#|.*/videos/)?([0-9]+)";
        match = videolink.match(regExp);
        if(match)
          {
           var videoid = videolink.split('/')[videolink.split('/').length - 1];
            alert("vimeo video id :"+videoid);
          }
    
     else 
      {
        alert("Unknown url");
      }
    

    and here is JSBIN Link http://jsbin.com/zoxicoko/2/