获取推特视频 mp4 url
Get twitter video mp4 url
问题我现在和 post 包含视频时正在使用 Twitter API。 Twitter returns 只有缩略图 url 而不是主视频文件 url。推特 returns 这样 JSON:
["media_url"]=> string(86) "http://pbs.twimg.com/ext_tw_video_thumb/../pu/img/p1R5iC_7fN-lqNuK.jpg"
["media_url_https"]=> string(87) "https://pbs.twimg.com/ext_tw_video_thumb/.../pu/img/p1R5iC_7fN-lqNuK.jpg"
["url"]=> string(22) "http://t.co/UViL9KykF*"
["expanded_url"]=> string(63) "http://twitter.com/.../status/..../video/1"
前两个是 jpg,其他 url 直接重定向到 post。
当我使用 php 获取所有信息时,对于 vine.co 有这样的代码:
$dom = new DomFinder($media_url);
$video_cell = $dom->find("//meta[@property='twitter:player:stream']", 'content');
但是 twitter 中没有这样的元标记,我找不到访问视频文件的方法。
也许有人知道如何获取 mp4 url?
Twitter 工作人员已声明 here 他们将不支持使用搜索获取视频。您将必须通过对 API.
的另一个调用来获取视频
假设您使用 search/tweets 提取了一条保存在 $tweet
中的推文
// Check if tweet has media
if (!empty($tweet->entities->media)) {
$searchArray = array(
"id" => $tweet->id, // id of the tweet we just fetched
"include_entities" => true // tells twitter API to return videos and stuff
);
// Get extended_entities
$extendedEntities = $connection->get("statuses/show", $searchArray);
foreach($extendedEntities->extended_entities->media as $media){
var_dump($media->video_info->variants);
}
}
示例结果
array (size=6)
0 =>
object(stdClass)[265]
public 'bitrate' => int 832000
public 'content_type' => string 'video/webm' (length=10)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/vid/480x480/gj_fzyk29R9dMPBY.webm' (length=92)
1 =>
object(stdClass)[266]
public 'bitrate' => int 832000
public 'content_type' => string 'video/mp4' (length=9)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/vid/480x480/gj_fzyk29R9dMPBY.mp4' (length=91)
2 =>
object(stdClass)[267]
public 'bitrate' => int 1280000
public 'content_type' => string 'video/mp4' (length=9)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/vid/720x720/S7F4BF2wKR2txCpA.mp4' (length=91)
3 =>
object(stdClass)[268]
public 'content_type' => string 'application/dash+xml' (length=20)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/pl/udxtIM8FytsAE4HQ.mpd' (length=82)
4 =>
object(stdClass)[269]
public 'bitrate' => int 320000
public 'content_type' => string 'video/mp4' (length=9)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/vid/240x240/v5jqpUNnkaeuVZbx.mp4' (length=91)
5 =>
object(stdClass)[270]
public 'content_type' => string 'application/x-mpegURL' (length=21)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/pl/udxtIM8FytsAE4HQ.m3u8' (length=83)
添加选项 tweet_mode:'extended'
以在响应中获取 extended_entities 对象。
响应中的此路径将包含视频网址: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.........'
}
]
问题我现在和 post 包含视频时正在使用 Twitter API。 Twitter returns 只有缩略图 url 而不是主视频文件 url。推特 returns 这样 JSON:
["media_url"]=> string(86) "http://pbs.twimg.com/ext_tw_video_thumb/../pu/img/p1R5iC_7fN-lqNuK.jpg"
["media_url_https"]=> string(87) "https://pbs.twimg.com/ext_tw_video_thumb/.../pu/img/p1R5iC_7fN-lqNuK.jpg"
["url"]=> string(22) "http://t.co/UViL9KykF*"
["expanded_url"]=> string(63) "http://twitter.com/.../status/..../video/1"
前两个是 jpg,其他 url 直接重定向到 post。 当我使用 php 获取所有信息时,对于 vine.co 有这样的代码:
$dom = new DomFinder($media_url);
$video_cell = $dom->find("//meta[@property='twitter:player:stream']", 'content');
但是 twitter 中没有这样的元标记,我找不到访问视频文件的方法。 也许有人知道如何获取 mp4 url?
Twitter 工作人员已声明 here 他们将不支持使用搜索获取视频。您将必须通过对 API.
的另一个调用来获取视频假设您使用 search/tweets 提取了一条保存在 $tweet
中的推文// Check if tweet has media
if (!empty($tweet->entities->media)) {
$searchArray = array(
"id" => $tweet->id, // id of the tweet we just fetched
"include_entities" => true // tells twitter API to return videos and stuff
);
// Get extended_entities
$extendedEntities = $connection->get("statuses/show", $searchArray);
foreach($extendedEntities->extended_entities->media as $media){
var_dump($media->video_info->variants);
}
}
示例结果
array (size=6)
0 =>
object(stdClass)[265]
public 'bitrate' => int 832000
public 'content_type' => string 'video/webm' (length=10)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/vid/480x480/gj_fzyk29R9dMPBY.webm' (length=92)
1 =>
object(stdClass)[266]
public 'bitrate' => int 832000
public 'content_type' => string 'video/mp4' (length=9)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/vid/480x480/gj_fzyk29R9dMPBY.mp4' (length=91)
2 =>
object(stdClass)[267]
public 'bitrate' => int 1280000
public 'content_type' => string 'video/mp4' (length=9)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/vid/720x720/S7F4BF2wKR2txCpA.mp4' (length=91)
3 =>
object(stdClass)[268]
public 'content_type' => string 'application/dash+xml' (length=20)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/pl/udxtIM8FytsAE4HQ.mpd' (length=82)
4 =>
object(stdClass)[269]
public 'bitrate' => int 320000
public 'content_type' => string 'video/mp4' (length=9)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/vid/240x240/v5jqpUNnkaeuVZbx.mp4' (length=91)
5 =>
object(stdClass)[270]
public 'content_type' => string 'application/x-mpegURL' (length=21)
public 'url' => string 'https://video.twimg.com/ext_tw_video/560049056895209473/pu/pl/udxtIM8FytsAE4HQ.m3u8' (length=83)
添加选项 tweet_mode:'extended'
以在响应中获取 extended_entities 对象。
响应中的此路径将包含视频网址: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.........'
}
]