Skip to content

Instantly share code, notes, and snippets.

@yangshun
Last active June 17, 2026 21:06
Show Gist options
  • Select an option

  • Save yangshun/9892961 to your computer and use it in GitHub Desktop.

Select an option

Save yangshun/9892961 to your computer and use it in GitHub Desktop.
YouTube Vimeo URL Parser
function parseVideo (url) {
// - Supported YouTube URL formats:
// - http://www.youtube.com/watch?v=My2FRPA3Gf8
// - http://youtu.be/My2FRPA3Gf8
// - https://youtube.googleapis.com/v/My2FRPA3Gf8
// - Supported Vimeo URL formats:
// - http://vimeo.com/25451551
// - http://player.vimeo.com/video/25451551
// - Also supports relative URLs:
// - //player.vimeo.com/video/25451551
url.match(/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/);
if (RegExp.$3.indexOf('youtu') > -1) {
var type = 'youtube';
} else if (RegExp.$3.indexOf('vimeo') > -1) {
var type = 'vimeo';
}
return {
type: type,
id: RegExp.$6
};
}
function createVideo (url, width, height) {
// Returns an iframe of the video with the specified URL.
var videoObj = parseVideo(url);
var $iframe = $('<iframe>', { width: width, height: height });
$iframe.attr('frameborder', 0);
if (videoObj.type == 'youtube') {
$iframe.attr('src', '//www.youtube.com/embed/' + videoObj.id);
} else if (videoObj.type == 'vimeo') {
$iframe.attr('src', '//player.vimeo.com/video/' + videoObj.id);
}
return $iframe;
}
function getVideoThumbnail (url, cb) {
// Obtains the video's thumbnail and passed it back to a callback function.
var videoObj = parseVideo(url);
if (videoObj.type == 'youtube') {
cb('//img.youtube.com/vi/' + videoObj.id + '/maxresdefault.jpg');
} else if (videoObj.type == 'vimeo') {
// Requires jQuery
$.get('http://vimeo.com/api/v2/video/' + videoObj.id + '.json', function(data) {
cb(data[0].thumbnail_large);
});
}
}
@chonz0

chonz0 commented Jul 22, 2015

Copy link
Copy Markdown

Very nice job, thanks a lot! :) cheers!

@koyal13

koyal13 commented Mar 5, 2018

Copy link
Copy Markdown

Really useful! Good job, thanks ;)

@kostasx

kostasx commented Mar 15, 2018

Copy link
Copy Markdown

First of all, thank you for sharing the code!

Maybe you want to update the regexp with an additional Dailymotion support?
// - DailyMotion
// - http://www.dailymotion.com/video/x6ga7eg

url.match(/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com)|dailymotion.com)\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/);

iframe and thumbnail URIs are:

iframe -> https://www.dailymotion.com/embed/video/VIDEO_ID?PARAMS
thumbnail -> https://www.dailymotion.com/thumbnail/video/{video_id}

@kostasx

kostasx commented Mar 17, 2018

Copy link
Copy Markdown

Update: I've created a forked version which includes support for DailyMotion. @yangshun

@aurora-darkstorm

Copy link
Copy Markdown

Hi everyone,

I am totally new at coding. And I think this is what I am looking for. To get the Vimeo address for a streaming video that is water falling with java script.

How do I run this script? I would appreciate if anyone can just point me in the direction of a tutorial or something online to learn.

Thank you.

@vinisxs

vinisxs commented Apr 2, 2018

Copy link
Copy Markdown

Awesome! Thank you!

@riteshkhatri

Copy link
Copy Markdown

I had used this its realy helpfull thanx.

@RicardoBer

Copy link
Copy Markdown

Very usefull, many thanks for share

ghost commented May 16, 2018

Copy link
Copy Markdown

I was actually searching for this to separate video url based on their types and this was really helpful in obtaining the type. Thanks a lot

@BacancyPratik

Copy link
Copy Markdown

Very useful, many thanks

@circa1983

Copy link
Copy Markdown

Nice parser @yangshun, 😎

Thank you, it’s just saved me a headache!

I noticed urls that don’t include any protocol eg: www.youtube.com or youtube.com aren’t covered
I changed the first capture group to include // and it worked a charm.
For anyone else that wants it, just change:
(http:|https:|)\/\/(player.|www.)?
to:
(https?:\/\/|\/\/|)(player.|www.)?

@luizoliveira-hotmart

Copy link
Copy Markdown

Thanks a lot!! <3

@huyitan

huyitan commented Aug 31, 2018

Copy link
Copy Markdown

Thanks for share.

@hemangshah

Copy link
Copy Markdown

Can someone would convert it to Swift?

@purva-surana-qp

Copy link
Copy Markdown

Can someone share pattern just for Vimeo

@luziferAzazel

luziferAzazel commented Feb 1, 2019

Copy link
Copy Markdown

I made a shorter version, with some improvements
^.*(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([^#\&\?]*).*

But I haven't been able to find a fix for this url:
https://vimeo.com/ondemand/somegirls/69246426

@anish2690

Copy link
Copy Markdown

This one not handling https://vimeo.com/369301154/459dfc68d3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment