To take the title and duration in the parameter part=
it is necessary to pass the snippet
and contentDetails
, should look something like:
https://www.googleapis.com/youtube/v3/videos?id={ID DO VIDEO}&part=snippet,contentDetails&key={YOUR_API_KEY}
It will return something like:
{
"kind": "youtube#videoListResponse",
"etag": "\"<etag>\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"<etag>\"",
"id": "<id>",
"snippet": {
"publishedAt": "2012-10-01T15:27:35.000Z",
"channelId": "UCAuUUnT6oDeKwE6v1NGQxug",
"title": "foo bar baz titulo",
"description": "foo bar descrição",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/<id>/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/<id>/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/<id>/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/<id>/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/<id>/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "FOOBARBAZ",
"tags": [
"foo",
"bar",
"baz"
],
"categoryId": "22",
"liveBroadcastContent": "none",
"defaultLanguage": "en",
"localized": {
"title": "foo bar baz titulo",
"description": "foo bar descrição"
},
"defaultAudioLanguage": "en"
},
"contentDetails": {
"duration": "PT21M3S",
"dimension": "2d",
"definition": "hd",
"caption": "true",
"licensedContent": true,
"projection": "rectangular"
}
}
]
}
then you can use file_get_contents
(if HTTP is allowed on your server) or curl
, example:
$id = '<id do video>';
$api_key = '<sua chave>';
$url = 'https://www.googleapis.com/youtube/v3/videos?id=' . $id . '&part=snippet,contentDetails&key=' . $api_key;
$resposta = file_get_contents($url);
if ($resposta) {
$api_data = json_decode($resposta);
} else {
die('Falha na resposta');
}
foreach ($api_data->items as $video) {
echo 'Titulo: ', $video->title, '<br>';
echo 'Titulo: ', $video->description, '<br>';
echo 'Duração: ', $video->contentDetails->duration, '<br>';
}
As stated in the reply, the time is formatted in ISO 8601, so you can use DateInterval
to easily convert to the desired output format, example:
$di = new DateInterval($video->contentDetails->duration);
echo $di->format('%H:%I:%S');
In the foreach
:
foreach ($api_data->items as $video) {
$di = new DateInterval($video->contentDetails->duration);
echo 'Titulo: ', $video->title, '<br>';
echo 'Titulo: ', $video->description, '<br>';
echo 'Duração: ', $di->format('%H:%I:%S'), '<br>';
}
In what language? Any one?
– Renan Gomes
Can be Python or PHP.
– Harever