使用Twitter API从tweet获取视频



我试图使用Twitter API从给定的tweet导入视频。然而,当我使用statuses/show端点时,它不会像图像那样返回任何扩展的视频实体,而是返回一个url实体,链接到一些嵌入视频播放器的视频容器,其中包含一个模糊的视频链接。

下面是一个例子:

我正试图在https://twitter.com/NHL/status/633987786018717696导入推文

使用Twitter API的statuses/show端点和tweet id,我得到这个响应:

{
  "created_at": "Wed Aug 19 13:04:01 +0000 2015",
  "id": 633987786018717700,
  "id_str": "633987786018717696",
  "text": "The offseason has us missing all of our fans, even the wacky ones... especially the wacky ones. #IsItOctoberYet?nhttps://t.co/v4UGDQpa61",
  "source": "<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>",
  "truncated": false,
  "in_reply_to_status_id": null,
  "in_reply_to_status_id_str": null,
  "in_reply_to_user_id": null,
  "in_reply_to_user_id_str": null,
  "in_reply_to_screen_name": null,
  "user":  {
    "id": 50004938,
    "id_str": "50004938",
    "name": "NHL",
    "screen_name": "NHL",
    "location": "30 cities across U.S. & Canada",
    "description": "The official source of everything you need and want to know from the National Hockey League. Read before tweeting us: http://t.co/JlyVXSpqMn",
    "url": "http://t.co/VI8RlwuVr9",
    "entities":  {
      "url":  {
        "urls":  [
           {
            "url": "http://t.co/VI8RlwuVr9",
            "expanded_url": "http://www.NHL.com",
            "display_url": "NHL.com",
            "indices":  [
              0,
              22
            ]
          }
        ]
      },
      "description":  {
        "urls":  [
           {
            "url": "http://t.co/JlyVXSpqMn",
            "expanded_url": "http://nhl.com/socialmediapolicy",
            "display_url": "nhl.com/socialmediapol…",
            "indices":  [
              118,
              140
            ]
          }
        ]
      }
    },
    "protected": false,
    "followers_count": 4130811,
    "friends_count": 2646,
    "listed_count": 18479,
    "created_at": "Tue Jun 23 15:24:18 +0000 2009",
    "favourites_count": 909,
    "utc_offset": -14400,
    "time_zone": "Eastern Time (US & Canada)",
    "geo_enabled": true,
    "verified": true,
    "statuses_count": 87436,
    "lang": "en",
    "contributors_enabled": false,
    "is_translator": false,
    "is_translation_enabled": true,
    "profile_background_color": "000000",
    "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/378800000139631457/fd-xWa9G.jpeg",
    "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/378800000139631457/fd-xWa9G.jpeg",
    "profile_background_tile": false,
    "profile_image_url": "http://pbs.twimg.com/profile_images/534776558238437376/yxrm83O7_normal.jpeg",
    "profile_image_url_https": "https://pbs.twimg.com/profile_images/534776558238437376/yxrm83O7_normal.jpeg",
    "profile_banner_url": "https://pbs.twimg.com/profile_banners/50004938/1435502670",
    "profile_link_color": "040CDE",
    "profile_sidebar_border_color": "FFFFFF",
    "profile_sidebar_fill_color": "2E2E2E",
    "profile_text_color": "0F5A80",
    "profile_use_background_image": true,
    "has_extended_profile": false,
    "default_profile": false,
    "default_profile_image": false,
    "following": true,
    "follow_request_sent": false,
    "notifications": false
  },
  "geo": null,
  "coordinates": null,
  "place": null,
  "contributors": null,
  "is_quote_status": false,
  "retweet_count": 865,
  "favorite_count": 1342,
  "entities":  {
    "hashtags":  [
       {
        "text": "IsItOctoberYet",
        "indices":  [
          96,
          111
        ]
      }
    ],
    "symbols":  [],
    "user_mentions":  [],
    "urls":  [
       {
        "url": "https://t.co/v4UGDQpa61",
        "expanded_url": "https://amp.twimg.com/v/2a0210d1-4d39-4665-a749-ea34f8efef08",
        "display_url": "amp.twimg.com/v/2a0210d1-4d3…",
        "indices":  [
          113,
          136
        ]
      }
    ]
  },
      "favorited": false,
      "retweeted": false,
      "possibly_sensitive": false,
      "possibly_sensitive_appealable": false,
      "lang": "en"
    }

点击URL后,视频标签的来源为https://amp.twimg.com/amplify-web-player/prod/source.html?vmap_url=https%3A%2F%2Famp.twimg.com%2Fprod%2Fmultibr_v_1%2Fvmap%2F2015%2F08%2F20%2F13%2F609fc2af-1d06-4894-80be-1c231f97557a%2Fa69baa90-58de-4d1d-b2dc-2c3ef1ab9b35.vmap&duration=91.958&image_src=https%3A%2F%2Famp.twimg.com%2Fprod%2Fdefault%2F2015%2F08%2F20%2F13%2Fe8f0b317-ba48-4cec-bf2c-da4598e2b46b_poster-67227.jpg&content_id=609fc2af-1d06-4894-80be-1c231f97557a&page=amplify_card

如果他们不为它提供external_entity,我如何从tweet中提取此视频文件?

Twitter API现在已经更改,视频存储在extended_entities对象中。根据比特率的不同,可能有多个源。这是如何检索具有最高比特率的一个:

var bitrate = 0;
var hq_video_url;
for (var j=0; j<tweet.extended_entities.media[0].video_info.variants.length; j++) {
    if (tweet.extended_entities.media[0].video_info.variants[j].bitrate) {
        if (tweet.extended_entities.media[0].video_info.variants[j].bitrate > bitrate) {
            bitrate = tweet.extended_entities.media[0].video_info.variants[j].bitrate;
            hq_video_url = tweet.extended_entities.media[0].video_info.variants[j].url;
        }
    }
}

gif的解决方案在这里!

在Twitter API V2中,目前不可能获取GIF和视频url。我知道这太愚蠢了。但是一个变通方法是获取内容的预览图像并手工构建媒体URL。

假设我们想通过V2 API获取以下tweet的GIF URL: https://twitter.com/FloodSocial/status/870042717589340160

当我们使用以下URL获取tweet时https://api.twitter.com/2/tweets/870042717589340160?tweet.fields=attachments,author_id,created_at,entities,id,text&media.fields=preview_image_url,url&expansions=attachments.media_keys(当然使用您的承载令牌),您将看到响应包含一个包含https://pbs.twimg.com/tweet_video_thumb/DBMDLy_U0AAqUWP.jpg

preview_image_url

所以这里我们可以从URL中提取DBMDLy_U0AAqUWP部分,并手动构建真正的GIF URL,它应该是https://video.twimg.com/tweet_video/DBMDLy_U0AAqUWP.mp4

好了。您只需要编写提取器函数

当使用statuses/show端点时,添加tweet_mode:'extended'选项以获取extended_entities对象。响应中的这个路径将包含视频url: extended_entities.media[0].video_info.variants例子:

[
  {
    content_type: 'application/x-mpegURL',
    url: 'https://video.twimg.com/ext_tw_video/1358226.........'
  },
  {
    bitrate: 832000,
    content_type: 'video/mp4',
    url: 'https://video.twimg.com/ext_tw_video/1358226.........'
  },
  {
    bitrate: 256000,
    content_type: 'video/mp4',
    url: 'https://video.twimg.com/ext_tw_video/1358226.........'
  }
]

嗯,这取决于你使用的是什么平台…

  1. 直接连接URL
  2. 使用二进制视频数据
  3. 将二进制数据(例如字节流)通过一些显示它的小部件泵送。

如何做到这一点的细节将根据平台的不同而有很大的不同&您使用的语言

最新更新