Reputation: 17616
How to correctly add a thumbnail to a youtube video upon insert?
In the google-api-php-client exists a service called
$snippet = new \Google_Service_YouTube_VideoSnippet();
Through this I can set the Thumbnail like so:
$snippet->setThumbnails($thumbnail_details);
However it asks for a parameter of instance type:
$thumbnail_details = new \Google_Service_YouTube_ThumbnailDetails();
I figured this was the class where I could set the thumbnail URL, however it wasn't the case. This class has a method called "setDefault" where I can put in parameter another instance of type:
$thumbnail_service = new \Google_Service_YouTube_Thumbnail();
Which has a method called:
$thumbnail_service->setUrl($thumbnail_url);
Which is what I have done. The url works fine (ex: here). Even after setting the url and uploading the video, the wrong thumbnail is set. (I suppose that if there is no thumbnail, it chooses a random frame in the video as a thumbnail)
$thumbnail_service = new \Google_Service_YouTube_Thumbnail();
$thumbnail_details = new \Google_Service_YouTube_ThumbnailDetails();
$thumbnail_service->setUrl($thumbnail_url);
$thumbnail_details->setDefault($thumbnail_service);
$snippet = new \Google_Service_YouTube_VideoSnippet();
/*
Other video info added to snippet
*/
$snippet->setThumbnails($thumbnail_details);
Upvotes: 2
Views: 660
Reputation: 305
use this code to resolve the problem
$video_id=$v->getId();
$url="image.jpg";
$client->setDefer(true);
$thumb = $youtube->thumbnails->set($video_id);
$media = new \Google_Http_MediaFileUpload(
$client,
$thumb,
'image/png',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($url));
// Read the media file and upload it chunk by chunk.
$status = false;
$handle = fopen($url, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
// If you want to make other calls after the file upload, set setDefer back to false
$client->setDefer(false);
Upvotes: 1
Reputation: 17616
I was not able to find how to upload a thumbnail while uploading the youtube video. After each upload video, I would take the youtube video id from the response returned after uploading, and prepare a second request to upload the thumbnail to that specific video.
private function UploadThumbnail($video_id, $url){
$this->client->setDefer(true);
$thumb = $this->youtube->thumbnails->set($video_id);
$media = new \Google_Http_MediaFileUpload(
$this->client,
$thumb,
'image/png',
null,
true,
$this->chunkSizeBytes
);
$filesize = DataRetrievalService::remote_filesize($url);
$this->MediaUpload($media, $url, $filesize);
}
private function MediaUpload($media, $path, $filesize = null){
if(is_null($filesize)){
$filesize = filesize($path);
}
$media->setFileSize($filesize);
$status = false;
$handle = fopen($path, "rb");
while(!$status && !feof($handle)){
$chunk = fread($handle, $this->chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
$this->client->setDefer(false);
return $status;
}
If any specific questions about the code I wrote, leave a comment. I'll explain in detail.
Upvotes: 1