7
votes

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

6
Follow the link. see where it redirects you toMohammad Jafar Mashhadi
How can i get its ID ?Er KK Chopra
if you send a request to that URL it will respnd yout with a 3xx HTTP response that redirects you to the actual Youtube URL. youtube.com/watch?v=..... format! en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_RedirectionMohammad Jafar Mashhadi
Thank You! You are right. but how can i handle it with java code ?Er KK Chopra

6 Answers

19
votes

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();
        }
4
votes

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
1
votes

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;
    }
1
votes
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
0
votes

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>
-1
votes

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