Skip to content

Instantly share code, notes, and snippets.

@gouravd
Forked from jvanderwee/YouTubeHelper.java
Created January 29, 2018 17:28
Show Gist options
  • Save gouravd/3efe1e3c886e17af8559329c3da7edeb to your computer and use it in GitHub Desktop.
Save gouravd/3efe1e3c886e17af8559329c3da7edeb 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));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment