1

Is there any way to get video id from youtube link.

I have a way in php script, but I need it in javascript.

Here is my php script

function get_youtube($url){
  parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
  return $my_array_of_vars['v']; 
}

For example I have a youtube video link

http://www.youtube.com/watch?v=fWNaR-rxAic&list=RD029I9Ar6upx34

then I get content of V variable = fWNaR-rxAic

Riyanto Wibowo
  • 364
  • 1
  • 6
  • 13
  • 1
    possible duplicate of [Javascript REGEX: How to get youtube video id from URL?](http://stackoverflow.com/questions/3452546/javascript-regex-how-to-get-youtube-video-id-from-url) – T.Todua Jan 20 '15 at 14:15

4 Answers4

8

The below function will return video id of the video if it is a valid youtube url or return false.

function matchYoutubeUrl(url){
var p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
 return (url.match(p)) ? RegExp.$1 : false ;
}
atul singh
  • 664
  • 5
  • 11
3

Try this piece of code

    /*Function youtubeLinkParser parses youtube link for Video ID*/
function youtubeLinkParser(url) {
    var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|\?v=)([^#\&\?]*).*/;
    var match = url.match(regExp);
    if (match && match[2].length == 11) {
        return match[2];
    } else {
        return null;
    }
}
iJade
  • 23,144
  • 56
  • 154
  • 243
1

The format of a Youtube link is the following:

http://www.youtube.com/watch?parameter=video_id&bunch_of_other_stuff

Now, I have, wrongly, assumed that all videos had a watch?v=

It turns out there can be a watch?list= and the v= comes near the end.

parameter can be v or list or something else for all I know. It doesn't matter.

So this excludes the ?v= (This was pointed out on the #jetpack channel on irc.mozilla.org:8443 by freaktechnik). We're left with v=

Another point is that pretty much every piece dealing with this assumes a length of 11 characters. Why ? Just because it is now ?

Let's say you have your URL and it is video_url. You can achieve getting your video_id using just two splits. I can do it in two splits because I spent just few hours working with JavaScript, but I'm sure someone more experienced can do it better.

EDIT:

video_id = video_url.split("v=")[1];
ampersand_pos = video_id.indexOf("&");
if (ampersand_pos != -1) {
video_id = video_id.substring(0, ampersand_pos)
}

Try it. It takes into account this:

youtube(dot)com/watch?list=WL2358B031ED8642DE&v=FyUrrAqso-M

And also this:

youtube(dot)com/watch?v=hUgCuFiZozc&feature=c4-overview&list=UUyNpwxYpMdYpw8CNqqv3Bdw

Jugurtha Hadjar
  • 441
  • 3
  • 7
0

I found a simple way of doing it without using regex. Note: this works with all types of playable urls.

I made a function which does it for you:

function getLink(url){
    fetch('www.youtube.com/oembed?url=' + url).then(res => {
     var thumbnailUrl = res.thumbnail_url;
     var id = thumbnail_url.split('vi/')[1].substring(0, 11);
     return id;}
      )
}

console.log(getLink(your_url));
// here replace 'your_url' with your specified youtube url.

All this does is, it uses youtube api and passes your url as an parameter, and youtube take cares of the type of the url, so you dont have to worry about it. The next thing is, the function then takes the 'thumbnail_url' data from that api, and then splits the thumbnail's url accordingly to find the ID of the video.