使用twitter4j从twitter获取视频网址

时间:2017-10-11 18:32:53

标签: java video twitter twitter4j

是否有人可以通过twitter4j帮助我在Twitter帖子中找到视频网址?

我无法使用 getExtendedMediaEntities() 方法找到视频网址。

修改

ExtendedMediaEntity[] extendedMediaEntities = status.getExtendedMediaEntities();
                        for (int i = 0; i < extendedMediaEntities.length; i++) {
                            ExtendedMediaEntity extendedMediaEntity = extendedMediaEntities[i];
                            System.out.println("extendedMediaEntity type : "+extendedMediaEntity.getType());
                            String url = extendedMediaEntity.getURL();
                            System.out.println("extended url: "+ extendedMediaEntity.getMediaURL());
                        }

谢谢

1 个答案:

答案 0 :(得分:1)

您需要使用ExtendedMediaEntities上的Variant来检索视频网址。这将为您提供多种格式的多个URL。 getExpandedURL()会为您提供视频帖子的直接网址。

请使用变体找到以下示例:

    Paging paging = new Paging();
    paging.setCount(200);

    List<Status> statuses = twitter.getHomeTimeline(paging);
    for (Status status : statuses) {
        MediaEntity[] media = status.getMediaEntities();
        ExtendedMediaEntity[] extendedMediaEntities = status.getExtendedMediaEntities();
        for (int i = 0; i < extendedMediaEntities.length; i++) {
            ExtendedMediaEntity extendedMediaEntity = extendedMediaEntities[i];
            ExtendedMediaEntity.Variant[] variant = extendedMediaEntity.getVideoVariants();
            if (extendedMediaEntity.getType().equals("video")) {
                for (int j = 0; j < variant.length; j++) {
                    System.out.println("variant url: " + variant[j].getUrl());
                }
                String url = extendedMediaEntity.getURL();
                System.out.println("extended url: " + extendedMediaEntity.getExpandedURL());
            }
        }
    }

这将给出如下输出:

  

变种网址:   https://video.twimg.com/amplify_video/829771724442959873/pl/jKd0KfMseDh88K0s.m3u8   变体网址:

     

https://video.twimg.com/amplify_video/829771724442959873/vid/1280x720/QtF1QLexBBjcpt_L.mp4   变种网址:   https://video.twimg.com/amplify_video/829771724442959873/vid/320x180/b-bsY76n5VkngoAo.mp4   变种网址:   https://video.twimg.com/amplify_video/829771724442959873/vid/640x360/YjWDVec8gD19LHOE.mp4   扩展网址:   https://twitter.com/mashable/status/918201793208762368/video/1