Reputation: 161
I am trying to set custom thumbnail though API as soon as the video is uploaded through Data API V3.
I am getting the following error:
Error calling PUT https://www.googleapis.com/upload/youtube/v3/thumbnails/set?videoId=[VIDEOID]&key=[KEY]&uploadType=resumable&upload_id=[UPLOADID]: (400) Invalid request. The number of bytes uploaded is required to be equal or greater than 262144, except for the final request (it's recommended to be the exact multiple of 262144). The received request contained 8192 bytes, which does not meet this requirement.
The uploaded thumbnail is satisfying all the requirements like * Aspect ratio is 16:9 * Size is 1280*720 * Image type is png
$thumbnailFile = $thumbnailFile;
$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);
$client->setAccessToken($accessToken);
$io = new Google_IO_Stream($client);
$io->setTimeout(180);
if ($client->getAccessToken()) {
$imagePath = $thumbnailFile;
$headers = get_headers($imagePath, 1);
$file_size= $headers["Content-Length"];
$mime_type = null;
if($headers['Content-Type'] == 'image/png' || $headers['Content-Type'] == 'image/jpeg'){
$mime_type = $headers['Content-Type'];
}
$chunkSizeBytes = 1 * 1024 * 100;
$client->setDefer(true);
$params = array('videoId' => $videoId);
$setRequest = $youtube->thumbnails->set($videoId);
$media = new Google_Http_MediaFileUpload(
$client,
$setRequest,
$mime_type,
$file_size,
true,
false
);
$media->setFileSize($file_size);
$status = false;
$handle = fopen($imagePath, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
$client->setDefer(false);
$status['code'] = 1;
$status['message'] = 'Thumbnail uploaded successfully';
Upvotes: 2
Views: 1147
Reputation: 706
setFileSize
is expecting to be passed the size of the actual file you are uploading
In your example, you have set $file_size= $headers["Content-Length"];
and then $media->setFileSize($file_size);
Please try changing to $media->setFileSize(filesize($imagePath));
or $file_size = filesize($imagePath);
Upvotes: 1