How do I get the YouTube video ID from a URL?

前端 未结 30 2340
北恋
北恋 2020-11-22 03:06

I want to get the v=id from YouTube’s URL with JavaScript (no jQuery, pure JavaScript).

Example YouTube URL formats

http://www.youtube.c

相关标签:
30条回答
  • 2020-11-22 03:30

    Well, the way to do it with simple parsing would be: get everything starting after the first = sign to the first & sign.

    I think they had a similiar answer here:

    Parsing a Vimeo ID using JavaScript?

    0 讨论(0)
  • 2020-11-22 03:31

    Java Code: (Works for all the URLs:

    1. http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
    2. http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
    3. http://youtube.googleapis.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
    4. http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
    5. http://www.youtube.com/embed/0zM3nApSvMg?rel=0"
    6. http://www.youtube.com/watch?v=0zM3nApSvMg
    7. http://youtu.be/0zM3nApSvMg
    8. http://www.youtube.com/watch?v=0zM3nApSvMg/
    9. http://www.youtube.com/watch?feature=player_detailpage&v=8UVNT4wvIGY

    )

        String url = "http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index";
    
        String regExp = "/.*(?:youtu.be\\/|v\\/|u/\\w/|embed\\/|watch\\?.*&?v=)";
        Pattern compiledPattern = Pattern.compile(regExp);
        Matcher matcher = compiledPattern.matcher(url);
        if(matcher.find()){
            int start = matcher.end();
            System.out.println("ID : " + url.substring(start, start+11));
    
        }
    

    For DailyMotion:

    String url = "http://www.dailymotion.com/video/x4xvnz_the-funny-crash-compilation_fun";
    
        String regExp = "/video/([^_]+)/?";
        Pattern compiledPattern = Pattern.compile(regExp);
        Matcher matcher = compiledPattern.matcher(url);
        if(matcher.find()){
            String match = matcher.group();
            System.out.println("ID : " + match.substring(match.lastIndexOf("/")+1));
    
        }
    
    0 讨论(0)
  • 2020-11-22 03:31

    Slightly stricter version:

    ^https?://(?:www\.)?youtu(?:\.be|be\.com)/(?:\S+/)?(?:[^\s/]*(?:\?|&)vi?=)?([^#?&]+)
    

    Tested on:

    http://www.youtube.com/user/dreamtheater#p/u/1/oTJRivZTMLs
    https://youtu.be/oTJRivZTMLs?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b
    http://www.youtube.com/watch?v=oTJRivZTMLs&feature=youtu.be
    https://youtu.be/oTJRivZTMLs
    http://youtu.be/oTJRivZTMLs&feature=channel
    http://www.youtube.com/ytscreeningroom?v=oTJRivZTMLs
    http://www.youtube.com/embed/oTJRivZTMLs?rel=0
    http://youtube.com/v/oTJRivZTMLs&feature=channel
    http://youtube.com/v/oTJRivZTMLs&feature=channel
    http://youtube.com/vi/oTJRivZTMLs&feature=channel
    http://youtube.com/?v=oTJRivZTMLs&feature=channel
    http://youtube.com/?feature=channel&v=oTJRivZTMLs
    http://youtube.com/?vi=oTJRivZTMLs&feature=channel
    http://youtube.com/watch?v=oTJRivZTMLs&feature=channel
    http://youtube.com/watch?vi=oTJRivZTMLs&feature=channel
    
    0 讨论(0)
  • 2020-11-22 03:31

    This can get video id from any type of youtube links

    var url= 'http://youtu.be/0zM3nApSvMg';
    var urlsplit= url.split(/^.*(youtu.be\/|v\/|embed\/|watch\?|youtube.com\/user\/[^#]*#([^\/]*?\/)*)\??v?=?([^#\&\?]*).*/);
    console.log(urlsplit[3]);
    
    
    0 讨论(0)
  • 2020-11-22 03:31

    This definitely requires regex:

    Copy into Ruby IRB:

    var url = "http://www.youtube.com/watch?v=NLqASIXrVbY"
    var VID_REGEX = /(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/
    url.match(VID_REGEX)[1]
    

    See for all test cases: https://gist.github.com/blairanderson/b264a15a8faaac9c6318

    0 讨论(0)
  • 2020-11-22 03:33

    Try this one -

    function getYouTubeIdFromURL($url) 
    {
      $pattern = '/(?:youtube.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu.be/)([^"&?/ ]{11})/i';
      preg_match($pattern, $url, $matches);
    
      return isset($matches[1]) ? $matches[1] : false;
    }
    
    0 讨论(0)
提交回复
热议问题