Skip to content

Instantly share code, notes, and snippets.

@jvanderwee
Created August 5, 2015 16:39
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jvanderwee/b30fdb496acff43aef8e to your computer and use it in GitHub Desktop.
Save jvanderwee/b30fdb496acff43aef8e to your computer and use it in GitHub Desktop.
Extract video id from YouTube url in 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;
}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class YouTubeHelperTest {
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "youtube.com/v/vidid" },
{ "youtube.com/vi/vidid" },
{ "youtube.com/?v=vidid" },
{ "youtube.com/?vi=vidid" },
{ "youtube.com/watch?v=vidid" },
{ "youtube.com/watch?vi=vidid" },
{ "youtu.be/vidid" },
{ "youtube.com/embed/vidid" },
{ "youtube.com/embed/vidid" },
{ "www.youtube.com/v/vidid" },
{ "http://www.youtube.com/v/vidid" },
{ "https://www.youtube.com/v/vidid" },
{ "youtube.com/watch?v=vidid&wtv=wtv" },
{ "http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related" },
{ "https://m.youtube.com/watch?v=vidid" }
});
}
private String url;
public YouTubeHelperTest(String url) {
this.url= url;
}
private YouTubeHelper youTubeHelper = new YouTubeHelper();
@Test
public void extractingVideoIdFromUrlShouldReturnVideoId() {
assertEquals("Unable to extract correct video id from url " + url, "vidid", youTubeHelper.extractVideoIdFromUrl(url));
}
}
@VikasKesharvani
Copy link

if url is https://youtu.be/8d_a1j1MPe0 then videoIdRegex not work
update videoIdRegex
final String[] videoIdRegex = {"\?vi?=([^&])", "watch\?.v=([^&])", "(?:embed|vi?)/([^/?])", "^([A-Za-z0-9\-\_]*)"};

@voghDev
Copy link

voghDev commented Feb 20, 2018

@VikasKesharvani's regex fails with this url: https://www.youtube.com/watch?v=aFnGjnKBzRM
I'll try to find a regex that suits for all cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment