7

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

Example Youtube URL formats:

http://www.youtube.com/watch?v=u8nQa1cJyX8&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW http://www.youtube.com/watch?v=u8nQa1cJyX8 http://youtu.be/0zM3nApSvMg http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/KdwsulMb8EQ 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/?v=dQw4w9WgXcQ http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ http://www.youtube.com/user/IngridMichaelsonVEVO#p/u/11/KdwsulMb8EQ http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0

or any other youtube format what contains a video id in the url

I am Trying with that :-

  Pattern compiledPattern = Pattern.compile("(?<=v=).*?(?=&|$)",Pattern.CASE_INSENSITIVE);      
        Matcher matcher = compiledPattern.matcher(sourceUrl);
        if(matcher.find()){
            setVideoId(matcher.group());
        }

It is not working only for one URL :-

http://youtu.be/6UW3xuJinEg

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Er KK Chopra
  • 1,834
  • 8
  • 31
  • 55

6 Answers6

21

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();
        }
Jakki
  • 349
  • 4
  • 6
4

6UW3xuJinEg (i mean the string after youtu.be/) is the ID most of the time. But for being more sure you can send HTTP GET request to that URL and it will respond you with a HTTP302 redirect response where you can find the actual redirection URL. you can parse that URL your previous code.

enter image description here

To send and recieve that request and response you can use libraries like jsoup. but because it's just a simple GET request you can simply use java sockets.

Connect to youtube.be on 80 port and write this in output stream:

GET /6UW3xuJinEg HTTP/1.1



# Don't forget the blank lines
Mohammad Jafar Mashhadi
  • 4,102
  • 3
  • 29
  • 49
  • any other option ? i need to change with that code only >Pattern.compile("(?<=v=).*?(?=&|$)",Pattern.CASE_INSENSITIVE); – Er KK Chopra Sep 08 '14 at 06:48
  • 1
    if there is no `v=...` just get string between `/` and `?` or end of string. – Mohammad Jafar Mashhadi Sep 08 '14 at 06:50
  • Good point - kinda like validating an email address - regex might be fairly good and cover lots of cases, but there's only one way to really validate an email address... ask the server responsible for it – Code Jockey Sep 23 '14 at 17:14
1

I found solution for this .. i expand that URL.. and its working ..

public static String expandUrl(String shortenedUrl)  {
        URL url;
        String expandedURL = "";
        try {
            url = new URL(shortenedUrl);
            // open connection
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); 
            // stop following browser redirect
            httpURLConnection.setInstanceFollowRedirects(false);
            // extract location header containing the actual destination URL
            expandedURL = httpURLConnection.getHeaderField("Location");
            httpURLConnection.disconnect();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    
         return expandedURL;
    }
Er KK Chopra
  • 1,834
  • 8
  • 31
  • 55
  • 1
    Isn't it what i wrote in my answer? :)) – Mohammad Jafar Mashhadi Sep 08 '14 at 13:23
  • 1
    @CodeJockey Is this piece of code that important? besides he said HttpConnection isn't what he wants but he answered his own question with Http solution and accepted it. – Mohammad Jafar Mashhadi Sep 23 '14 at 15:37
  • @MJafarMash I can't speak for krishan, but it seemed like he was seeking actual code in Java - perhaps arguably in a somewhat do-this-for-me tone (?) - but unlike sooo many people asking such questions, he found code that answered his question, brought it back, and added it as an answer. Your answer was definitely useful, in that it gave anyone reading it good information and a different path to take, but I could see someone saying it doesn't "answer the question" technically – Code Jockey Sep 23 '14 at 17:13
  • 1
    @CodeJockey you are right . i got it from your answer but i have to explain complete .. that what i need . – Er KK Chopra Feb 16 '15 at 06:49
1
private String getYouTubeId(String youTubeUrl) {
        String pattern = "https?://(?:[0-9A-Z-]+\\.)?(?:youtu\\.be/|youtube\\.com\\S*[^\\w\\-\\s])([\\w\\-]{11})(?=[^\\w\\-]|$)(?![?=&+%\\w]*(?:['\"][^<>]*>|</a>))[?=&+%\\w]*";

        Pattern compiledPattern = Pattern.compile(pattern,
                Pattern.CASE_INSENSITIVE);
        Matcher matcher = compiledPattern.matcher(youTubeUrl);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
}

Use this method, it works in most case that return Null in above answers. cases tested:

https://m.youtube.com/watch?feature=youtu.be&v=ROkXM3csNWY
https://www.youtube.com/watch?v=rie69P0W668
https://m.youtube.com/watch?feature=youtu.be&v=JqyzwbpYYqc
https://www.youtube.com/watch?v=YPln3JP_gKs&feature=youtu.be
Kyo Huu
  • 520
  • 7
  • 13
1

You can use regex I've created:

 public static String  YOUTUBE_PATTERN_ID = "^(?:(?:\\w*.?://)?\\w*.?\\w*-?.?\\w*/(?:embed|e|v|watch|.*/)?\\??(?:feature=\\w*\\.?\\w*)?&?(?:v=)?/?)([\\w\\d_-]+).*";

Pattern matcher = Pattern.compile(YOUTUBE_PATTERN_ID).matcher(url)
  if (matcher.find()) {
        return matcher.group(1)
  }

https://regex101.com/r/b0yMMd/1

Used snippet base from this answer: https://stackoverflow.com/a/35436389/7138308

var regex = /^(?:(?:\w*.?:\/\/)?\w*.?\w*\-?.?\w*\/(?:embed|e|v|watch|.*\/)?\??(?:feature=\w*\.?\w*)?\&?(?:v=)?\/?)([\w\d_-]+).*/i;

// An array of all the youtube URLs
var youtubeLinks = [
    'http://www.youtube.com/watch?v=u8nQa1cJyX8&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW ',
    'http://www.youtube.com/watch?v=u8nQa1cJyX-8  ',
    'http://youtu.be/0zM3nApSvMg ',
    'http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/KdwsulMb8EQ  ',
    '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/?v=dQw4w9WgXcQ  ',
    'http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ  ',
    'http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ  ',
    'http://www.youtube.com/user/IngridMichaelsonVEVO#p/u/11/KdwsulMb8EQ  ',
    'http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0 ',
    'https://m.youtube.com/watch?feature=youtu.be&v=ROkXM3csNWY ',
    'https://www.youtube.com/watch?v=rie69P0W668 ',
    'https://m.youtube.com/watch?feature=youtu.be&v=JqyzwbpYYqc ',
    'https://www.youtube.com/watch?v=YPln3JP_gKs&feature=youtu.be ',
    'https://www.youtube.com/watch?v=l-kX8Z4u0Kw&list=PLhml-dmiPOedRDLV8n1ro_OTdzKjOdlyp'
];

// An object to store the results
var youtubeIds = {};

// Iterate over the youtube URLs
youtubeLinks.forEach(function(url) {
    // Get the value of second captured group to extract youtube ID
    var id = "<span class='youtubeId'>" + (url.match(regex) || [0, 0, 'No ID present'])[1] + "</span>";

    // Add the URL and the extracted ID in the result object
    youtubeIds[url] = id;
});

// Log the object in the browser console
console.log(youtubeIds);

// To show the result on the page
document.getElementById('output').innerHTML = JSON.stringify(youtubeIds, 0, 4);
.youtubeId {
    color: green;
    font-weight: bold;
}
<pre id="output"></pre>
Alex Smith
  • 616
  • 2
  • 7
  • 16
-1

Try this code here.

// (?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})
final static String reg = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})";

public static String getVideoId(String videoUrl) {
    if (videoUrl == null || videoUrl.trim().length() <= 0)
        return null;

    Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(videoUrl);

    if (matcher.find())
        return matcher.group(1);
    return null;
}

You can find my whole parser code from here https://github.com/TheFinestArtist/YouTubePlayerActivity/blob/master/library/src/main/java/com/thefinestartist/ytpa/utils/YoutubeUrlParser.java

This is useful open source I made to play Youtube Video. https://github.com/TheFinestArtist/YouTubePlayerActivity

The Finest Artist
  • 3,150
  • 29
  • 34