How to get youtube video id from URL with java?

前端 未结 6 2006
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 06:51

I want to get the v=id from youtube\'s URL with java

Example Youtube URL formats:

http://www.youtube.com/watch?v=u8nQa1cJyX8&

6条回答
  •  遥遥无期
    2020-12-16 07:46

    The code below will extract the video ids for the following type of urls.

    http://www.youtube.com/watch?v=dQw4w9WgXcQ&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW
    http://www.youtube.com/watch?v=dQw4w9WgXcQ 
    http://youtu.be/dQw4w9WgXcQ 
    http://www.youtube.com/embed/dQw4w9WgXcQ
    http://www.youtube.com/v/dQw4w9WgXcQ 
    http://www.youtube.com/e/dQw4w9WgXcQ
    http://www.youtube.com/watch?v=dQw4w9WgXcQ
    http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
    http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0
    
    String pattern = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
    
            Pattern compiledPattern = Pattern.compile(pattern);
            Matcher matcher = compiledPattern.matcher(url); //url is youtube url for which you want to extract the id.
            if (matcher.find()) {
                 return matcher.group();
            }
    

提交回复
热议问题