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

4
  • Follow the link. see where it redirects you to Commented Sep 8, 2014 at 6:16
  • How can i get its ID ? Commented Sep 8, 2014 at 6:19
  • 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_Redirection Commented Sep 8, 2014 at 6:25
  • Thank You! You are right. but how can i handle it with java code ? Commented Sep 8, 2014 at 6:27

6 Answers 6

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();
        }
Sign up to request clarification or add additional context in comments.

2 Comments

@Stephan thanks i will check the link which you have sent.
I think think there is an error how special characters are ignored.
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

3 Comments

any other option ? i need to change with that code only >Pattern.compile("(?<=v=).*?(?=&|$)",Pattern.CASE_INSENSITIVE);
if there is no v=... just get string between / and ? or end of string.
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
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;
    }

4 Comments

Isn't it what i wrote in my answer? :))
@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.
@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
@CodeJockey you are right . i got it from your answer but i have to explain complete .. that what i need .
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

Comments

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>

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.