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