I'm using the following function to get video IDs from YouTube URLs.
static String getVideoIdFromUrl(String url) {
String regex = "http(?:s)?://(?:www\\.)?youtu(?:\\.be/|be\\.com/(?:watch\\?v=|v/\u200C\u200B|embed/|user/(?:[\\w#\u200C\u200B]+/)+))([^&#?\\n]+)";
String id = null;
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(url);
if (matcher.matches()) {
id = matcher.group(1);
}
return id;
}
This works for getting the video ID for almost all formats. However, it returns null when provided with URLs with a timestamp.
http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s
https://www.youtube.com/watch?v=Br5xdYVbcWw&t=50
How do I write a function that returns both the video ID and the timestamp?