Created
August 5, 2015 16:39
-
-
Save jvanderwee/b30fdb496acff43aef8e to your computer and use it in GitHub Desktop.
Extract video id from YouTube url in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'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
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\-\_]*)"};