Peril
Peril

Reputation: 1599

getting the youtube id from a link

I got this code to get the youtube id from the links like www.youtube.com/watch?v=xxxxxxx

  URL youtubeURL = new URL(link);
  youtubeURL.getQuery();

basically this will get me the id easily v=xxxxxxxx

but I noticed sometime youtube links will be like this

http://gdata.youtube.com/feeds/api/videos/xxxxxx

I am getting the links from a feed so do I need to build a regex for that or theres a parser to get that for me ?

Upvotes: 25

Views: 21074

Answers (10)

MBH
MBH

Reputation: 16609

Here is the kotlin version that will support many of Youtube urls including Shorts:

private fun getVideoId(youtubeUrl: String): String {
    var videoId = ""
    val regex = "/(?:watch|\\w+\\?(?:feature=\\w+.\\w+&)?v=|(videos/)|v/|e/|embed/|live/|shorts/|user/(?:[\\w#]+/)+)([^&#?\\n]+)"
    val pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE)
    val matcher = pattern.matcher(youtubeUrl)
    if(matcher.find()){
        videoId = matcher.group(2).orEmpty()
    }
    return videoId
}

Here are some samples to I've tested against:

https://youtube.com/watch?v=olij8GbEHJ0
www.youtube.com/watch?v=olij8GbEHJ0
https://youtu.be/shorts/fMlV1dGb1cI?feature=share
http://gdata.youtube.com/feeds/api/videos/xxxxxx
http://www.youtube.com/embed/Woq5iX9XQhA?html5=1
http://www.youtube.com/watch?v=384IUU43bfQ
http://gdata.youtube.com/feeds/api/videos/xTmi7zzUa-M&whatever

Try it on regex101.com choosing Java on the left menu

Upvotes: 0

Nabeel K
Nabeel K

Reputation: 6128

Got a better solution from this link.

Use the following method to get the videoId from the link.

YoutubeHelper.java

import com.google.inject.Singleton; 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Singleton 
public class YouTubeHelper { 

    final String youTubeUrlRegEx = "^(https?)?(://)?(www.)?(m.)?((youtube.com)|(youtu.be))/";
    final String[] videoIdRegex = { "\\?vi?=([^&]*)","watch\\?.*v=([^&]*)", "(?:embed|vi?)/([^/?]*)", "^([A-Za-z0-9\\-]*)"};

    public String extractVideoIdFromUrl(String url) {
        String youTubeLinkWithoutProtocolAndDomain = youTubeLinkWithoutProtocolAndDomain(url);

        for(String regex : videoIdRegex) {
            Pattern compiledPattern = Pattern.compile(regex);
            Matcher matcher = compiledPattern.matcher(youTubeLinkWithoutProtocolAndDomain);

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

        return null; 
    } 

    private String youTubeLinkWithoutProtocolAndDomain(String url) {
        Pattern compiledPattern = Pattern.compile(youTubeUrlRegEx);
        Matcher matcher = compiledPattern.matcher(url);

        if(matcher.find()){
            return url.replace(matcher.group(), "");
        } 
        return url;
    } 
} 

Hope this helps.

Upvotes: 3

Bahu
Bahu

Reputation: 1596

This was worked for me

public static String getYoutubeVideoId(String youtubeUrl) {
    String videoId = "";
    if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http")) {

        String expression = "^.*((youtu.be"+ "/)" + "|(v/)|(/u/w/)|(embed/)|(watch\\?))\\??v?=?([^#&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
        Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(youtubeUrl);
        if (matcher.matches()) {
            String groupIndex1 = matcher.group(7);
            if(groupIndex1!=null && groupIndex1.length()==11)
                videoId = groupIndex1;
        }

    }
    return videoId;
}

Source link

Upvotes: 1

OneCoder
OneCoder

Reputation: 21

That pattern worked for me:

"http(?:s?)://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\-]+)(&(amp;)?[\w\?=‌​]*)?"

source: Regular expression for youtube links

Upvotes: 2

saravanan
saravanan

Reputation: 1092

This will work me and simple

public static String getVideoId(@NonNull String videoUrl) {
    String reg = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})";
    Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(videoUrl);

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

Upvotes: 0

BullyWiiPlaza
BullyWiiPlaza

Reputation: 19185

This doesn't use a regex but should still do the job.

/**
 * Returns the video id of a YouTube watch link.
 */
public static String getVideoId(String watchLink)
{
    return watchLink.substring(watchLink.length() - 11);
}

Upvotes: 0

Code Jockey
Code Jockey

Reputation: 6721

Without knowing the complete specification for all the possible YouTube URLs, this seems to work for the examples you provided:

//*EDIT* - fixed to hopefully support more recent youtube link styles/formats:
(?<=watch\?v=|/videos/|/embed/|youtu.be/)[^&#?]*

... matches PjDw3azfZWI from either of these URLS:

http://www.youtube.com/watch?v=PjDw3azfZWI#t=31m08s
http://gdata.youtube.com/feeds/api/videos/PjDw3azfZWI

You would need a little more to get that particular info if you did not know that these were from youtube, though that's a pretty quick check

Keep in mind that if you are trying to use only the result of the getQuery() method, it will not be possible to extract the result from the http://gdata.youtube.com/feeds/api/videos/PjDw3azfZWI URL, as this URL does not have a query part to it...

Java Example:

Pattern rex = Pattern.compile("(?<=watch\\?v=|/videos/)[^&#]*");
Matcher m = rex.matcher(link);
String YouTubeVideoID = m.group();

Upvotes: 1

samirprogrammer
samirprogrammer

Reputation: 438

public static String getYoutubeVideoId(String youtubeUrl)
 {
 String video_id="";
  if (youtubeUrl != null && youtubeUrl.trim().length() > 0 && youtubeUrl.startsWith("http"))
 {

String expression = "^.*((youtu.be"+ "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
 CharSequence input = youtubeUrl;
 Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
 Matcher matcher = pattern.matcher(input);
 if (matcher.matches())
 {
String groupIndex1 = matcher.group(7);
 if(groupIndex1!=null && groupIndex1.length()==11)
 video_id = groupIndex1;
 }
 }
 return video_id;
 }

Upvotes: 7

max
max

Reputation: 1266

Tried the other ones but failed in my case - adjusted the regex to fit for my urls

String pattern = "(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*";
    
    Pattern compiledPattern = Pattern.compile(pattern);
    Matcher matcher = compiledPattern.matcher(url);

    if(matcher.find()){
        return matcher.group();
    }

This one works for: (you could also implement a security check youtubeid length = 11 )

http://www.youtube.com/embed/Woq5iX9XQhA?html5=1

http://www.youtube.com/watch?v=384IUU43bfQ

http://gdata.youtube.com/feeds/api/videos/xTmi7zzUa-M&whatever

Woq5iX9XQhA

384IUU43bfQ

xTmi7zzUa-M

Upvotes: 48

Marcus
Marcus

Reputation: 12586

This regex would do the trick:

(?<=videos\/|v=)([\w-]+)

This means that we're first looking for video/ or v= then captures all the following characters that can be in word (letters, digits, and underscores) and hyphens.

Example in java:

public static void main(String[] args) {

    String link = "http://gdata.youtube.com/feeds/api/videos/xTmi7zzUa-M&whatever";
    String pattern = "(?:videos\\/|v=)([\\w-]+)";

    Pattern compiledPattern = Pattern.compile(pattern);
    Matcher matcher = compiledPattern.matcher(link);

    if(matcher.find()){
        System.out.println(matcher.group());
    }
}

Output:

xTmi7zzUa-M

Upvotes: 6

Related Questions