I need to get the duration of all the videos in a Youtube playlist. I know that the API does not show me the duration of each video when doing the search of all, but it does show it if the query is made for a particular video.
Through PHP I tried to collect all the IDs from the playlist and then analyze each ID to get the data from the videos, but the script is too slow, although it should be stressed that it works well, is there any way to optimize it?...
function youtube_automusic($listas, $api_key, $resultados){
$nresultados = $resultados;
$lista_reproduccion_random = $listas;
$lista_reproduccion = $lista_reproduccion_random[array_rand($lista_reproduccion_random)];
$url_playlist = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&fields=items(snippet(resourceId(videoId)))&type=video&videoCategoryId=10&maxResults=".$nresultados."&playlistId=".$lista_reproduccion."&key=".$api_key;
$data = dlPage($url_playlist);
$data_decode = json_decode($data, true);
$number_song = 1;
$respuesta = array();
foreach ($data_decode as $items){
foreach ($items as $item){
$lista_ids =$item['snippet']['resourceId']['videoId'];
$url_video = "https://www.googleapis.com/youtube/v3/videos?id=".$lista_ids."&part=snippet,contentDetails&fields=items(etag,id,snippet(publishedAt,title,thumbnails(default(url)),tags),contentDetails(duration))&key=".$api_key;
$data_video = dlPage($url_video);
$data_video_decode = json_decode($data_video, true);
foreach ($data_video_decode as $items_videos){
foreach ($items_videos as $item_video){
$data_final = array(
'etag' => $item_video['etag'],
'idvideo' => $item_video['id'],
'titulovideo' => $item_video['snippet']['title'],
'thumbnail' => $item_video['snippet']['thumbnails']['default']['url'],
'duracion' => $item_video['contentDetails']['duration'],
'videoplay' => $number_song++
);
array_push($respuesta, $data_final);
}
}
}
}
return json_encode($respuesta);
}
The snippet object contains basic details about the video, such as its title, description, and category. The date and time that the video was published. Note that this time might be different than the time that the video was uploaded.
With your code on a 50 item playlist it would take 51 API calls.
Instead of doing a single videos request for each video in the playlist, get all the video IDs in the playlist first and then make videos requests for up to 50 at a time (the ID parameter takes a comma-separated list of up to 50 items).
Then a 50 item playlist would only take 2 API calls.
Should be much faster.
I just ran a test here
Request:
GET https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails&id=Ks-_Mh1QhMc&fields=items(etag%2Cid%2Csnippet(publishedAt%2Ctitle%2Cthumbnails(default(url))%2Ctags)%2CcontentDetails(duration))&key={YOUR_API_KEY}
Results:
{
"items": [
{
"etag": "\"RmznBCICv9YtgWaaa_nWDIH1_GM/aCBUdsaX0W34z3It8a8FCh5uteo\"",
"id": "Ks-_Mh1QhMc",
"snippet": {
"publishedAt": "2012-10-01T15:27:35.000Z",
"title": "Your body language may shape who you are | Amy Cuddy",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/Ks-_Mh1QhMc/default.jpg"
}
},
"tags": [
"Amy Cuddy",
"TED",
"TEDTalk",
"TEDTalks",
"TED Talk",
"TED Talks",
"TEDGlobal",
"brain",
"business",
"psychology",
"self",
"success"
]
},
"contentDetails": {
"duration": "PT21M3S"
}
}
]
}
I suggest that you run the same request using the Google APIs Explorer using the video id that you are having an issue with to verify that its not an issue with the API not returning your duration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With