我有一个非常直接的任务 -
鉴于视频的网址,我想提取其相关视频的信息(以及我所说的标题,标签,说明信息)。 我使用Java版的YouTube API,但无法快速了解如何做到这一点。此外,此网站:https://developers.google.com/youtube/2.0/developers_guide_java提供了一些信息。他们提供的代码是:
if (videoEntry.getRelatedVideosLink() != null) {
String feedUrl = videoEntry.getRelatedVideosLink().getHref();
VideoFeed videoFeed = service.getFeed(new URL(feedUrl), VideoFeed.class);
printVideoFeed(videoFeed, true);
}
我不知道如何仅根据URL创建videoEntry。
答案 0 :(得分:2)
您可以使用:
class YouTubeVideoInfo {
private String channel;
private String url;
private long views;
private int comments;
private int ratings;
private int likes;
private int dislikes;
private String thumbnail;
private String title;
.....
}
public static final String YOUTUBE_GDATA_SERVER = "http://gdata.youtube.com";
public static final String USER_FEED_PREFIX = YOUTUBE_GDATA_SERVER + "/feeds/api/users/";
public static final String UPLOADS_FEED_SUFFIX = "/uploads";
...............
public YouTubeVideoInfo getVideoInfo(YouTubeService service, String channel, String url) {
VideoFeed videoFeed = service.getFeed(
new URL(USER_FEED_PREFIX + channel + UPLOADS_FEED_SUFFIX), VideoFeed.class);
List<VideoEntry> videoEntries = videoFeed.getEntries();
for (VideoEntry videoEntry : videoEntries) {
YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
if (mediaGroup != null && mediaGroup.getPlayer() != null && videoEntry.getTitle() != null) {
if (url.equals(mediaGroup.getPlayer().getUrl())) {
String title = videoEntry.getTitle().getPlainText();
MediaKeywords keywords = mediaGroup.getKeywords();
MediaPlayer mediaPlayer = mediaGroup.getPlayer();
final YtStatistics statistics = videoEntry.getStatistics();
final YouTubeVideoInfo videoInfo = new YouTubeVideoInfo(channel,
mediaPlayer.getUrl(), statistics != null
? statistics.getViewCount() : 0);
if (videoEntry.getComments() != null
&& videoEntry.getComments().getFeedLink() != null)
videoInfo.comments =
videoEntry.getComments().getFeedLink().getCountHint();
final Rating rating = videoEntry.getRating();
if (rating != null)
videoInfo.ratings = rating.getNumRaters();
final YtRating ytRating = videoEntry.getYtRating();
if (ytRating != null) {
videoInfo.likes = ytRating.getNumLikes();
videoInfo.dislikes = ytRating.getNumDislikes();
}
final List<MediaThumbnail> thumbnails = mediaGroup.getThumbnails();
if (!thumbnails.isEmpty())
videoInfo.thumbnail = thumbnails.get(thumbnails.size() / 2).getUrl();
if (videoEntry.getTitle() != null)
videoInfo.title = videoEntry.getTitle().getPlainText();
return videoInfo;
}
}
.... // exception handling
答案 1 :(得分:0)
我有办法做我想做的事。我没有要求频道的名称(上传视频的用户名)。我只需要一个URL。 YouTube具有与每个视频对应的ID。例如,对于视频:http://www.youtube.com/watch?v=f3q3JkNUPmI,ID为“f3q3JkNUPmI”(不带引号)。因此,您需要做的就是创建一个包含您想要获取的Feed链接的String。这可以创建为:
String Video_Related_Feed =“https://gdata.youtube.com/feeds/api/videos/f3q3JkNUPmI/related?v=2”(不要用http替换https - 它不起作用) 显然,人们可以自动化这个......我只是给出了硬编码的例子。 现在根据以下地址获取实际Feed:
VideoFeed videoFeed = service.getFeed(新网址(Video_Related_Feed),VideoFeed.class); 此Feed包含相关视频的VideoEntry(对应于我们的原始网址),每个视频都可以通过for循环访问: for(VideoEntry ve:videoFeed.getEntries()){在此处插入您的代码}
参考文献:https://developers.google.com/youtube/2.0/reference andhttps://developers.google.com/youtube/2.0/developers_guide_java (对不起,我不得不写这样的最后一个链接,因为我无法发布超过2个网址)。
我希望这有助于某人。