Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486 to your computer and use it in GitHub Desktop.
Save rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486 to your computer and use it in GitHub Desktop.
Example of the YouTube videos URL formats
http://www.youtube.com/watch?v=-wtIMTCHWuI
http://youtube.com/watch?v=-wtIMTCHWuI
http://m.youtube.com/watch?v=-wtIMTCHWuI
https://www.youtube.com/watch?v=lalOy8Mbfdc
https://youtube.com/watch?v=lalOy8Mbfdc
https://m.youtube.com/watch?v=lalOy8Mbfdc
http://www.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail
http://youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail
http://m.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail
https://www.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail
https://youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail
https://m.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail
http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
http://youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
http://m.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
https://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
https://youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
https://m.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
http://youtube.com/watch?v=0zM3nApSvMg#t=0m10s
http://m.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
https://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
https://youtube.com/watch?v=0zM3nApSvMg#t=0m10s
https://m.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel
http://youtube.com/watch?v=cKZDdG9FTKY&feature=channel
http://m.youtube.com/watch?v=cKZDdG9FTKY&feature=channel
https://www.youtube.com/watch?v=oTJRivZTMLs&feature=channel
https://youtube.com/watch?v=oTJRivZTMLs&feature=channel
https://m.youtube.com/watch?v=oTJRivZTMLs&feature=channel
http://www.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub
http://youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub
http://m.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub
https://www.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub
https://youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub
https://m.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub
http://www.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be
http://youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be
http://m.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be
https://www.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be
https://youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be
https://m.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be
http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player
http://m.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player
https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player
https://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player
https://m.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player
http://www.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655
http://youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655
http://m.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655
https://www.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655
https://youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655
https://m.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655
http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
http://youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
http://m.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
https://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
https://youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
https://m.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
http://www.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ
http://youtube.com/watch?app=desktop&v=dQw4w9WgXcQ
http://m.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ
https://www.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ
https://youtube.com/watch?app=desktop&v=dQw4w9WgXcQ
https://m.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ
http://www.youtube.com/watch/-wtIMTCHWuI
http://youtube.com/watch/-wtIMTCHWuI
http://m.youtube.com/watch/-wtIMTCHWuI
https://www.youtube.com/watch/-wtIMTCHWuI
https://youtube.com/watch/-wtIMTCHWuI
https://m.youtube.com/watch/-wtIMTCHWuI
http://www.youtube.com/watch/-wtIMTCHWuI?app=desktop
http://youtube.com/watch/-wtIMTCHWuI?app=desktop
http://m.youtube.com/watch/-wtIMTCHWuI?app=desktop
https://www.youtube.com/watch/-wtIMTCHWuI?app=desktop
https://youtube.com/watch/-wtIMTCHWuI?app=desktop
https://m.youtube.com/watch/-wtIMTCHWuI?app=desktop
http://www.youtube.com/v/dQw4w9WgXcQ
http://youtube.com/v/dQw4w9WgXcQ
http://m.youtube.com/v/dQw4w9WgXcQ
https://www.youtube.com/v/dQw4w9WgXcQ
https://youtube.com/v/dQw4w9WgXcQ
https://m.youtube.com/v/dQw4w9WgXcQ
http://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1
http://youtube.com/v/-wtIMTCHWuI?version=3&autohide=1
http://m.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1
https://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1
https://youtube.com/v/-wtIMTCHWuI?version=3&autohide=1
https://m.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1
http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
http://youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
http://m.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
https://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
https://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
https://youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
https://m.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
http://www.youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player
http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player
http://m.youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player
https://www.youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player
https://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player
https://m.youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player
http://youtu.be/-wtIMTCHWuI
https://youtu.be/-wtIMTCHWuI
http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player
https://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player
http://youtu.be/oTJRivZTMLs?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b
https://youtu.be/oTJRivZTMLs?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b
http://youtu.be/oTJRivZTMLs&feature=channel
https://youtu.be/oTJRivZTMLs&feature=channel
http://youtu.be/lalOy8Mbfdc?t=1
http://youtu.be/lalOy8Mbfdc?t=1s
https://youtu.be/lalOy8Mbfdc?t=1
https://youtu.be/lalOy8Mbfdc?t=1s
http://youtu.be/M9bq_alk-sw?si=B_RZg_I-lLaa7UU-
https://youtu.be/M9bq_alk-sw?si=B_RZg_I-lLaa7UU-
http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json
http://youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json
http://m.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json
https://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json
https://youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json
https://m.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json
http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare
http://youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare
http://m.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare
https://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare
https://youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare
https://m.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare
http://www.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail
http://youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail
http://m.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail
https://www.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail
https://youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail
https://m.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail
http://www.youtube.com/embed/lalOy8Mbfdc
http://youtube.com/embed/lalOy8Mbfdc
http://m.youtube.com/embed/lalOy8Mbfdc
https://www.youtube.com/embed/lalOy8Mbfdc
https://youtube.com/embed/lalOy8Mbfdc
https://m.youtube.com/embed/lalOy8Mbfdc
http://www.youtube.com/embed/nas1rJpm7wY?rel=0
http://youtube.com/embed/nas1rJpm7wY?rel=0
http://m.youtube.com/embed/nas1rJpm7wY?rel=0
https://www.youtube.com/embed/nas1rJpm7wY?rel=0
https://youtube.com/embed/nas1rJpm7wY?rel=0
https://m.youtube.com/embed/nas1rJpm7wY?rel=0
http://www.youtube-nocookie.com/embed/lalOy8Mbfdc?rel=0
https://www.youtube-nocookie.com/embed/lalOy8Mbfdc?rel=0
http://www.youtube.com/e/dQw4w9WgXcQ
http://youtube.com/e/dQw4w9WgXcQ
http://m.youtube.com/e/dQw4w9WgXcQ
https://www.youtube.com/e/dQw4w9WgXcQ
https://youtube.com/e/dQw4w9WgXcQ
https://m.youtube.com/e/dQw4w9WgXcQ
http://www.youtube.com/shorts/j9rZxAF3C0I
http://youtube.com/shorts/j9rZxAF3C0I
http://m.youtube.com/shorts/j9rZxAF3C0I
https://www.youtube.com/shorts/j9rZxAF3C0I
https://youtube.com/shorts/j9rZxAF3C0I
https://m.youtube.com/shorts/j9rZxAF3C0I
http://www.youtube.com/shorts/j9rZxAF3C0I?app=desktop
http://youtube.com/shorts/j9rZxAF3C0I?app=desktop
http://m.youtube.com/shorts/j9rZxAF3C0I?app=desktop
https://www.youtube.com/shorts/j9rZxAF3C0I?app=desktop
https://youtube.com/shorts/j9rZxAF3C0I?app=desktop
https://m.youtube.com/shorts/j9rZxAF3C0I?app=desktop
http://www.youtube.com/live/8hBmepWUJoc
http://youtube.com/live/8hBmepWUJoc
http://m.youtube.com/live/8hBmepWUJoc
https://www.youtube.com/live/8hBmepWUJoc
https://youtube.com/live/8hBmepWUJoc
https://m.youtube.com/live/8hBmepWUJoc
http://www.youtube.com/live/8hBmepWUJoc?app=desktop
http://youtube.com/live/8hBmepWUJoc?app=desktop
http://m.youtube.com/live/8hBmepWUJoc?app=desktop
https://www.youtube.com/live/8hBmepWUJoc?app=desktop
https://youtube.com/live/8hBmepWUJoc?app=desktop
https://m.youtube.com/live/8hBmepWUJoc?app=desktop
https://youtube.com/user/GitHub#p/a/u/1/lalOy8Mbfdc
https://www.youtube.com/user/GitHub#p/u/1/lalOy8Mbfdc
https://www.youtube.com/user/GitHub#p/u/1/lalOy8Mbfdc?rel=0
https://www.youtube.com/user/GitHub#p/a/u/2/lalOy8Mbfdc
https://www.youtube.com/user/GitHub#p/u/11/lalOy8Mbfdc
https://www.youtube.com/user/GitHub#p/u/1/lalOy8Mbfdc
https://www.youtube.com/GitHub?v=lalOy8Mbfdc
http://www.youtube.com/?v=dQw4w9WgXcQ
http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/?v=lalOy8Mbfdc&feature=channel
http://youtube.com/?vi=dQw4w9WgXcQ
http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/?vi=lalOy8Mbfdc&feature=channel
http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ
http://youtube.com/?feature=channel&v=lalOy8Mbfdc
http://youtube.com/watch?vi=dQw4w9WgXcQ
http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/watch?vi=lalOy8Mbfdc&feature=channel
http://youtube.com/vi/dQw4w9WgXcQ
http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player
http://youtube.com/vi/lalOy8Mbfdc&feature=channel
http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0
@rodrigoborgesdeoliveira
Copy link
Author

Thanks for this list, it's come in handy for a couple of little projects 🙂

🙂

@wu-lee
Copy link

wu-lee commented Jan 30, 2023

Are the URLs like this one valid?

https://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o

In that, yes, they go to a You Tube user's channel, but I suspect everything after the # is ignored, so the video ID isn't used. In fact that anchor suffix could be anything at all, and it would be correct not to parse the video ID.

@wu-lee
Copy link

wu-lee commented Jan 30, 2023

Likewise, I wonder about these URLs with a vi in place of a v. They all either go to a 404-not-found or are redirected to youtube.com's front page. Even if I insert an ID I know works with other URLs - so it's not simply the video has been taken down.

http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player
http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player
http://youtube.com/vi/oTJRivZTMLs&feature=channel
http://youtube.com/watch?vi=oTJRivZTMLs&feature=channel

@wu-lee
Copy link

wu-lee commented Jan 30, 2023

And URLs like this, with a v= parameter and no watch:

http://youtube.com/?v=XXXXXXXXXXX&feature=youtube_gdata_player
http://www.youtube.com/?v=XXXXXXXXXXX
http://www.youtube.com/?feature=player_embedded&v=XXXXXXXXXXX
http://youtube.com/?v=XXXXXXXXXXX&feature=channel
http://youtube.com/?feature=channel&v=XXXXXXXXXXX

I've not made an exhaustive test, but it does seem to find a lot of questionable URLs. It's quite possible these work for someone else, but has anyone evidence these URLs ever did link to a video?

@NebularNerd
Copy link

I've tried a couple and it seems some of the weirder looking ones may or do not work, I've left them in my regex for potential matches if I ever find one in the wild and want to quickly get the ID

@rodrigoborgesdeoliveira
Copy link
Author

Thanks for reporting these @wu-lee. I'm going to do some tests over the weekend and leave some comments in the list for any of the links that don't work anymore. It might be the case that YouTube has deprecated these URLs.

@wu-lee
Copy link

wu-lee commented Jan 31, 2023

I think these URLs with vi components may be for thumbnail images associated with videos. For example, this is one:

https://i.ytimg.com/vi/XXXXXXXXXXX/default.jpg

@Revnoplex
Copy link

Are query strings that use & first instead of ? valid?
because its on lines 118 and 119, and python's urllib parsing library did not recognise it and made the query strings part of the path.
eg:

from urllib import parse

components = parse.urlparse("https://youtu.be/oTJRivZTMLs&feature=channel")
print(components.path)

results in an output of

oTJRivZTMLs&feature=channel

while normally

from urllib import parse

components = parse.urlparse("https://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player")
print(components.path)

results in just

dQw4w9WgXcQ

So either this is a mistake on the urls listed or a bug with the python urllib library

@rodrigoborgesdeoliveira
Copy link
Author

It's neither a mistake nor a bug. The &feature=channel is indeed part of the path, not a query string parameter. However, YouTube's url shortener service (the youtu.be) will do the job of parsing the path parameter into what youtube.com needs, and so it'll translate and redirect https://youtu.be/oTJRivZTMLs&feature=channel into something like https://www.youtube.com/watch?v=oTJRivZTMLs&feature=channel&feature=youtu.be.

@Snailedlt
Copy link

Here's a python regex that matches all of the links in this gist:

^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})

Python 3 implementation:

def get_youtube_video_id_by_url(url):
    regex = r"^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})"
    match = re.match(regex, url, re.IGNORECASE)
    if match:
        return (
            match.group("video_id_1")
            or match.group("video_id_2")
            or match.group("video_id_3")
        )
    else:
        return None

It matches everything in the gist, but can probably be improved upon for performance, so feel free to suggest improvements!
Just test them here first: https://regex101.com/

@Revnoplex
Copy link

I've written a regexless version in python using its built-in url parser. Full code here. You will also notice the version that works with playlists. That one works with the following urls

def extract_video_id(url: str) -> Optional[str]:
    """
    This should work for every url listed here:
    https://gist.github.com/rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486#file-activeyoutubeurlformats-txt
    and more.
    Args:
        url (str): The url to strip the id from
    Returns:
        Optional[str]: The video id with the rest of the url removed
    """
    components = parse.urlparse(url.replace("&", "?", 1) if "?" not in url else url)
    queries = parse.parse_qs(components.query)
    encoded_query_matches = {'u', 'url'}.intersection(set(queries.keys()))
    if 'v' in queries:
        return queries["v"][0]
    elif encoded_query_matches:
        return extract_video_id(parse.unquote(queries[encoded_query_matches.pop()][0]))
    elif components.netloc == "i.ytimg.com":
        return pathlib.Path(components.path).parts[2]
    elif pathlib.Path(components.path).name not in ["playlist"]:
        return pathlib.Path(components.path).name

@tacerus
Copy link

tacerus commented Aug 26, 2023

Hi,

recently links generated using the "Share" button under a video will contain some additional ?si= query parameter:

https://youtu.be/_UuD60EZGkg?si=eP5r-sAojFjjVYCY

Not sure what it is for, probably more tracking?

@rodrigoborgesdeoliveira
Copy link
Author

Oh, interesting. Thanks, @tacerus!

@heartlog
Copy link

Hi,

recently links generated using the "Share" button under a video will contain some additional ?si= query parameter:

https://youtu.be/_UuD60EZGkg?si=eP5r-sAojFjjVYCY

Not sure what it is for, probably more tracking?

Yea, most probably it's true.
And have you noticed that every time you share a URL, the tracking ID changes, even for the same video on the same device? If it's a tracking parameter, then it can track the sender and receiver of the YouTube URL. In short, Google can know who your close relatives and friends are. This is surely a messed up thing.

@ChristopherGabba
Copy link

For those using javascript/typescript, I have a function and regex working for all combinations above plus youtube shorts:

export function getVideoIdFromYoutubeUrl(url: string) {
  const regex = /(?:youtu\.be\/|youtube\.com(?:\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=|shorts\/)|youtu\.be\/|embed\/|v\/|m\/|watch\?(?:[^=]+=[^&]+&)*?v=))([^"&?\/\s]{11})/gm;
  return regex.exec(url)[1];
}
export function validateYoutubeTestUrls() {
  const youtubeTestUrls: { key: number; value: string }[] = [
    { key: 1, value: "http://www.youtube.com/watch?v=-wtIMTCHWuI" },
    { key: 2, value: "http://youtube.com/watch?v=-wtIMTCHWuI" },
    { key: 3, value: "http://m.youtube.com/watch?v=-wtIMTCHWuI" },
    { key: 4, value: "https://www.youtube.com/watch?v=lalOy8Mbfdc" },
    { key: 5, value: "https://youtube.com/watch?v=lalOy8Mbfdc" },
    { key: 6, value: "https://m.youtube.com/watch?v=lalOy8Mbfdc" },
    { key: 7, value: "http://www.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail" },
    { key: 8, value: "http://youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail" },
    { key: 9, value: "http://m.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail" },
    { key: 10, value: "https://www.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail" },
    { key: 11, value: "https://youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail" },
    { key: 12, value: "https://m.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail" },
    { key: 13, value: "http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index" },
    { key: 14, value: "http://youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index" },
    { key: 15, value: "http://m.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index" },
    { key: 16, value: "https://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index" },
    { key: 17, value: "https://youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index" },
    { key: 18, value: "https://m.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index" },
    { key: 19, value: "http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s" },
    { key: 20, value: "http://youtube.com/watch?v=0zM3nApSvMg#t=0m10s" },
    { key: 21, value: "http://m.youtube.com/watch?v=0zM3nApSvMg#t=0m10s" },
    { key: 22, value: "https://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s" },
    { key: 23, value: "https://youtube.com/watch?v=0zM3nApSvMg#t=0m10s" },
    { key: 24, value: "https://m.youtube.com/watch?v=0zM3nApSvMg#t=0m10s" },
    { key: 25, value: "http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel" },
    { key: 26, value: "http://youtube.com/watch?v=cKZDdG9FTKY&feature=channel" },
    { key: 27, value: "http://m.youtube.com/watch?v=cKZDdG9FTKY&feature=channel" },
    { key: 28, value: "https://www.youtube.com/watch?v=oTJRivZTMLs&feature=channel" },
    { key: 29, value: "https://youtube.com/watch?v=oTJRivZTMLs&feature=channel" },
    { key: 30, value: "https://m.youtube.com/watch?v=oTJRivZTMLs&feature=channel" },
    { key: 31, value: "http://www.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub",},
    { key: 32, value: "http://youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub", },
    { key: 33, value: "http://m.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub" },
    { key: 34, value: "https://www.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub", },
    { key: 35, value: "https://youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub" },
    { key: 36, value: "https://m.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub",},
    { key: 37, value: "http://www.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be" },
    { key: 38, value: "http://youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be" },
    { key: 39, value: "http://m.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be" },
    { key: 40, value: "https://www.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be" },
    { key: 41, value: "https://youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be" },
    { key: 42, value: "https://m.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be" },
    { key: 43, value: "http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player" },
    { key: 44, value: "http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player" },
    { key: 45, value: "http://m.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player" },
    { key: 46, value: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player" },
    { key: 47, value: "https://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player" },
    { key: 48, value: "https://m.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player" },
    { key: 49,  value: "http://www.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655", },
    { key: 50, value: "http://youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655", }, { key: 51,value: "http://m.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655", }, { key: 52, value: "https://www.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655", },
    { key: 53, value: "https://youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655", },
    { key: 54, value: "https://m.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655",  },
    { key: 55, value: "http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ" },
    { key: 56, value: "http://youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ" },
    { key: 57, value: "http://m.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ" },
    { key: 58, value: "https://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ" },
    { key: 59, value: "https://youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ" },
    { key: 60, value: "https://m.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ" },
    { key: 61, value: "http://www.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ" },
    { key: 62, value: "http://youtube.com/watch?app=desktop&v=dQw4w9WgXcQ" },
    { key: 63, value: "http://m.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ" },
    { key: 64, value: "https://www.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ" },
    { key: 65, value: "https://youtube.com/watch?app=desktop&v=dQw4w9WgXcQ" },
    { key: 66, value: "https://m.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ" },
    { key: 67, value: "http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index" },
    { key: 69, value: "http://www.youtube.com/v/0zM3nApSvMg?fs=1&amp;hl=en_US&amp;rel=0" },
    { key: 70, value: "http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s" },
    { key: 71, value: "http://www.youtube.com/embed/0zM3nApSvMg?rel=0" },
    { key: 72, value: "http://www.youtube.com/watch?v=0zM3nApSvMg" },
    { key: 73, value: "http://youtu.be/0zM3nApSvMg" },
    { key: 74, value: "https://youtube.com/shorts/0dPkkQeRwTI?feature=share" },
    { key: 75, value: "https://youtube.com/shorts/0dPkkQeRwTI" },
    { key: 76, value: "https://youtu.be/59kf86v_Cpc?si=Cj_xm4a6BX71d1yd" },
  ]
  youtubeTestUrls.forEach((keyValuePair) => {
    const videoId = getVideoIdFromYoutubeUrl(keyValuePair.value)
    const isElevenCharacters = videoId?.length === 11
    console.log("id for", keyValuePair.key, "is: ", videoId, "| valid:", isElevenCharacters)
  })

}

OUTPUT:

 LOG  id for 1 is:  -wtIMTCHWuI | isvalid: true
 LOG  id for 2 is:  -wtIMTCHWuI | isvalid: true
 LOG  id for 3 is:  -wtIMTCHWuI | isvalid: true
 LOG  id for 4 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 5 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 6 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 7 is:  yZv2daTWRZU | isvalid: true
 LOG  id for 8 is:  yZv2daTWRZU | isvalid: true
 LOG  id for 9 is:  yZv2daTWRZU | isvalid: true
 LOG  id for 10 is:  yZv2daTWRZU | isvalid: true
 LOG  id for 11 is:  yZv2daTWRZU | isvalid: true
 LOG  id for 12 is:  yZv2daTWRZU | isvalid: true
 LOG  id for 13 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 14 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 15 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 16 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 17 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 18 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 19 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 20 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 21 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 22 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 23 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 24 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 25 is:  cKZDdG9FTKY | isvalid: true
 LOG  id for 26 is:  cKZDdG9FTKY | isvalid: true
 LOG  id for 27 is:  cKZDdG9FTKY | isvalid: true
 LOG  id for 28 is:  oTJRivZTMLs | isvalid: true
 LOG  id for 29 is:  oTJRivZTMLs | isvalid: true
 LOG  id for 30 is:  oTJRivZTMLs | isvalid: true
 LOG  id for 31 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 32 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 33 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 34 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 35 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 36 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 37 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 38 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 39 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 40 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 41 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 42 is:  lalOy8Mbfdc | isvalid: true
 LOG  id for 43 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 44 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 45 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 46 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 47 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 48 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 49 is:  ishbTyLs6ps | isvalid: true
 LOG  id for 50 is:  ishbTyLs6ps | isvalid: true
 LOG  id for 51 is:  ishbTyLs6ps | isvalid: true
 LOG  id for 52 is:  ishbTyLs6ps | isvalid: true
 LOG  id for 53 is:  ishbTyLs6ps | isvalid: true
 LOG  id for 54 is:  ishbTyLs6ps | isvalid: true
 LOG  id for 55 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 56 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 57 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 58 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 59 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 60 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 61 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 62 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 63 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 64 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 65 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 66 is:  dQw4w9WgXcQ | isvalid: true
 LOG  id for 67 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 69 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 70 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 71 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 72 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 73 is:  0zM3nApSvMg | isvalid: true
 LOG  id for 74 is:  0dPkkQeRwTI | isvalid: true
 LOG  id for 75 is:  0dPkkQeRwTI | isvalid: true
 LOG  id for 76 is:  59kf86v_Cpc | isvalid: true

@mlatushko
Copy link

I have a function and regex

  • Thank you.

And these:

————————————

V█(?-is)^(?:(?!https?[:\/]|www\.|m(?:usic)?\.yo|youtu\.?be[\/\.]|watch[\/\?]|embed\/|YTv(?:[=: ]+)|YouTube: )\V)*?(?:(?:https?[:\/]+|www\.|m\.|music\.)+youtu(?<1>\.)?be(?:\.com\/|watch|live(?=\/)|o?embed(?:\/|\?url=\S+?)?|\?app=\w+|shorts|attribution_link\?[&\w\-=]*[au]=\/?|ytsc\w+|[\?&]*(?:li)?[ve]i?\b[\/=]|\?feature=[\-a-z_\.]+|[\?&]t(?:ime_continue)?=\d+|-nocookie|%[23][56FD]|\.com\/shorts\/)*(?(<1>)\/|)|YTv(?:[=: ]+)|YouTube: )(?(?<=watch)\/|)([a-zA-Z\d\-_]{11}\b)(?>[\?&#][&#%a-zA-Z\-\d=;_\.]*|&feature=[\-a-z_\.]+)?(?=\s|\Z)

█\2

————————————

@Revnoplex
Copy link

Revnoplex commented Nov 11, 2023

For some reason, youtube blocks access to the site via the 11th one. It brings up the "our systems have detected unusual traffic from your computer network" message but with no captcha

@ZGorlock
Copy link

ZGorlock commented Mar 9, 2024

Here is a java version of regex that matches all the urls the list:

(?i)^(?:https?://)?(?:(?:www|m)\.)?youtu(?:\.be|be(?:-nocookie)?\.com)/(?:(?:oembed|attribution_link)\?.+?(?:/|%2F)(?=watch))?(?:(?:watch)?(?:\?|%3F)|(?:vi?|live|e(?:mbed)?)/)?(?:(?:[\w\-]+(?:=|%3D)[^=&#]+(?:&(?:amp;)?|%26))*?vi?(?:=|%3D))?(?<id>[\w\-]+)(?:(?:(?:\?|%3F)|(?:&(?:amp;)?|%26)|(?:#|%23))[\w\-]+(?:=|%3D)[^=&#]+)*$

String QM = "(?:\\?|%3F)";
String EQ = "(?:=|%3D)";
String AS = "(?:&(?:amp;)?|%26)";
String OT = "(?:#|%23)";
String FS = "(?:/|%2F)";

String PARAM_HEAD = "(?:" + QM + "|" + AS + "|" + OT + ")";
String PARAM_KEY = "[\\w\\-]+";
String PARAM_VAL = "[^=&#]+";
String PARAM = "(?:" + PARAM_KEY + EQ + PARAM_VAL + ")";

Pattern p = Pattern.compile("(?i)^" +
        "(?:https?://)?" +
        "(?:(?:www|m)\\.)?" +
        "youtu(?:\\.be|be(?:-nocookie)?\\.com)/" +
        "(?:(?:oembed|attribution_link)\\?.+?" + FS + "(?=watch))?" +
        "(?:(?:watch)?" + QM + "|" + "(?:vi?|live|e(?:mbed)?)/)?" +
        "(?:(?:" + PARAM + AS + ")*?" + "(?:vi?" + EQ + "))?" +
        "(?<id>" + PARAM_KEY + ")" +
        "(?:" + PARAM_HEAD + PARAM + ")*" +
        "$");

IOUtils.readLines(new BufferedReader(new InputStreamReader(
                new URL("https://gist.githubusercontent.com/rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486/raw/b15ee49a60d42df67b0d6383517ef3b57e25c9c3/ActiveYouTubeURLFormats.txt")
                        .openConnection().getInputStream()))).stream()
        .filter(StringUtils::isNotBlank)
        .map(p::matcher).filter(Matcher::matches)
        .forEachOrdered(m -> System.out.printf("%-15s %s%n", m.group("id"), m.group()));

@Kaligula0
Copy link

@ZGorlock: doesn't match music.youtube.com/watch?v=U9t-slLl30E, youtube.com/watch/U9t-slLl30E, youtube.com/shorts/sMbxjePPmkw and attribution_links (youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare and youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail). 🤔

@Kaligula0
Copy link

@rodrigoborgesdeoliveira Please add links youtube.com/watch/U9t-slLl30E, youtube.com/shorts/U9t-slLl30E and youtube.com/live/U9t-slLl30E. Here's my RegExp that catches a wide spectrum of YT URLs → https://gist.github.com/Kaligula0/1ff5f4e2cf1f351daeca3450f71fdcb5

@rodrigoborgesdeoliveira
Copy link
Author

Thanks, @Kaligula0!

@meeeet-dev
Copy link

@mplungjan
Copy link

mplungjan commented May 13, 2024

Here is a JavaScript version - it returns the 13 IDs found in the list you posted

const delParms = ['a', 'app', 'list', 'feature', 'rel', 'si'];

const ids = new Set(
  list.map(url => {
    // Normalize URL
    url = url.replace(/(attribution_link|uploademail|youtube-nocookie)/gi, 'x');
    // Parse URL
    let urlObject = new URL(url);
    // Check for embedded URL and parse it if present
    const embed = urlObject.searchParams.get('url');
    if (embed) urlObject = new URL(embed);
    // Extract 'v' parameter if present
    const v = urlObject.searchParams.get('v');
    if (v) return v;
    // Remove unwanted parameters
    delParms.forEach(parm => urlObject.searchParams.delete(parm));
    // Convert URL to string and replace encoded '='
    url = urlObject.toString().replace(/%3D/gi, '=');
    // Extract ID from URL
    const idMatch = url.match(/[\-_a-z0-9]{10,15}/i);
    const id = idMatch ? idMatch[0] : `No: ${url}`;
    return id;
  })
);
console.log(ids);

@jagdish1o1
Copy link

Here's a python regex that matches all of the links in this gist:

^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})

Python 3 implementation:

def get_youtube_video_id_by_url(url):
    regex = r"^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})"
    match = re.match(regex, url, re.IGNORECASE)
    if match:
        return (
            match.group("video_id_1")
            or match.group("video_id_2")
            or match.group("video_id_3")
        )
    else:
        return None

It matches everything in the gist, but can probably be improved upon for performance, so feel free to suggest improvements! Just test them here first: https://regex101.com/

Thank you for sharing this snippet. Working great.

@felipesilva-plank
Copy link

felipesilva-plank commented Jul 4, 2024

Just FYI both these functions do not work for URLs in the following format.

http://www.youtube.com/watch/-wtIMTCHWuI
http://youtube.com/watch/-wtIMTCHWuI
http://m.youtube.com/watch/-wtIMTCHWuI
https://www.youtube.com/watch/-wtIMTCHWuI
https://youtube.com/watch/-wtIMTCHWuI
https://m.youtube.com/watch/-wtIMTCHWuI

For those using javascript/typescript, I have a function and regex working for all combinations above plus youtube shorts:

export function getVideoIdFromYoutubeUrl(url: string) {
  const regex = /(?:youtu\.be\/|youtube\.com(?:\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=|shorts\/)|youtu\.be\/|embed\/|v\/|m\/|watch\?(?:[^=]+=[^&]+&)*?v=))([^"&?\/\s]{11})/gm;
  return regex.exec(url)[1];
}

Here's a python regex that matches all of the links in this gist:

^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})

Python 3 implementation:

def get_youtube_video_id_by_url(url):
    regex = r"^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})"
    match = re.match(regex, url, re.IGNORECASE)
    if match:
        return (
            match.group("video_id_1")
            or match.group("video_id_2")
            or match.group("video_id_3")
        )
    else:
        return None

to cover these edge cases, you can use the following regex:

/(?:https?:\/\/)?(?:www\.)?(?:m\.)?youtube\.com\/watch\/([a-zA-Z0-9_-]+)/

and in typescript:

export function getVideoIdFromYoutubeUrl(url: string) {
  const regex =
    /(?:youtu\.be\/|youtube\.com(?:\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=|shorts\/)|youtu\.be\/|embed\/|v\/|m\/|watch\?(?:[^=]+=[^&]+&)*?v=))([^"&?\/\s]{11})/gm;

  const edgeCasesRegex = /(?:https?:\/\/)?(?:www\.)?(?:m\.)?youtube\.com\/watch\/([a-zA-Z0-9_-]+)/;

  let result = regex.exec(url);

  if (!result) result = edgeCasesRegex.exec(url);

  return result[1];
}

@XxxGHOSTX
Copy link

@joshigolu08
Copy link

joshigolu08 commented Jul 23, 2024

if you are using JS, TS, or Angular u can use this works fine for me

getYoutubeVideoId(url) {
    // Regular expression to match different YouTube URL formats, including shorts
    const regExp = /(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?|shorts)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/i;
    const match = url.match(regExp);
    console.log(match && match[1] ? match[1] : null, "id", url); // For debugging purposes
    return match && match[1] ? match[1] : null;
  }

@ccauvang
Copy link

ccauvang commented Nov 1, 2024

If you trying to find a regex to match all format YT links try this

I write it for myself, it's works for me.
I use Js.

@khchin93
Copy link

I love this when rickroll is covered

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