java - YouTube API : How to obtain text of Related videos, 给定了视频的 URL?

标签 java youtube

我有一个非常简单的任务 -

Given the URL of a video, I want to extract information of its Related videos (and by information I mean their Title, Tags, Description). I use Java version of the YouTube API and couldn't find out how to do that quickly. Also, this site: https://developers.google.com/youtube/2.0/developers_guide_java provides some information. The code they provide is:

if (videoEntry.getRelatedVideosLink() != null) {
  String feedUrl = videoEntry.getRelatedVideosLink().getHref();

  VideoFeed videoFeed = service.getFeed(new URL(feedUrl), VideoFeed.class);
  printVideoFeed(videoFeed, true);
}

我不知道如何创建仅给出 URL 的 videoEntry..

最佳答案

你可以使用这个:

        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

关于java - YouTube API : How to obtain text of Related videos, 给定了视频的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9897635/

相关文章:

java - DQL 在 DFC 中删除 group_name

python - 有没有比ffmpeg更合适的方式在Python中合并视频和音频文件?

java - YouTubeThumbnailView 示例

css - 强制 YouTube 播放列表以 HTML5 播放

api - 从YouTube视频中获得歌手的名字类型

javascript - Youtube iframe javascript,跟踪观看视频的百分比?

java - 为什么这个不会投?

Java Eclipse 文本编辑器

java - 捆绑的 Java OS X 应用程序默认为 MacRoman 编码

java - 为什么列表中的 toString() 之后要添加逗号?