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
@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;
  }

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