如何使用Java从URL获取youtube视频ID?
我想使用Java从YouTube网址获取v = id
&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW
](http://www.youtube.com/watch?v=u8nQa1cJyX8&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW)
//www.youtube.com/watch?v=u8nQa1cJyX8) //youtu.be/0zM3nApSvMg) http://www.youtube.com/watch? / user /
IngridMichaelsonVEVO#p / a / u / 1 / KdwsulMb8EQ
//youtu.be/dQw4w9WgXcQ)
http://www.youtube.com/embed/dQw4w9WgXcQ
http://www.youtube.com/v/dQw4w9WgXcQ
http
://www.youtube.com/e/dQw4w9WgXcQ
//www.youtube.com/watch?v=dQw4w9WgXcQ)
http://www.youtube.com/?v=dQw4w9WgXcQ
http://www.youtube.com /
watch?feature = player_embedded&v = dQw4w9WgXcQ
http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ
http://www.youtube.com/user/IngridMichaelsonVEVO#p/u/11/KdwsulMb8EQ
http://www.youtube-
nocookie.com/v/6L3ZvIMwZFM?version=3&hl=zh_CN&rel=0
或其他任何youtube格式,其中网址中包含视频ID
我正在尝试:-
Pattern compiledPattern = Pattern.compile("(?<=v=).*?(?=&|$)",Pattern.CASE_INSENSITIVE); Matcher matcher = compiledPattern.matcher(sourceUrl);
if(matcher.find()){
setVideoId(matcher.group());
}
它不只对一个URL有效:
//youtu.be/6UW3xuJinEg)
回答:
我找到了解决方案..我扩展了该URL ..及其工作..
public static String expandUrl(String shortenedUrl) { URL url;
String expandedURL = "";
try {
url = new URL(shortenedUrl);
// open connection
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
// stop following browser redirect
httpURLConnection.setInstanceFollowRedirects(false);
// extract location header containing the actual destination URL
expandedURL = httpURLConnection.getHeaderField("Location");
httpURLConnection.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return expandedURL;
}
以上是 如何使用Java从URL获取youtube视频ID? 的全部内容, 来源链接: utcz.com/qa/412266.html