Skip to content
Advertisement

Video upload stop working – Youtube API

I use a PHP script to upload some daily videos to a Youtube channel (based on this code sample: https://developers.google.com/youtube/v3/code_samples/php#resumable_uploads)

The problem is after this loop:

// Read the media file and upload it chunk by chunk.
$status = false;
$handle = fopen($videoPath, "rb");
while (!$status && !feof($handle)) {
  $chunk = fread($handle, $chunkSizeBytes);
  $status = $media->nextChunk($chunk);
}

Normally the $status variable has the video id ($status[‘id’]) after the upload is complete but since mid January all the uploads failed with one of this errors:

  • $status variable value stills as “false”
  • Inside the catch, the Google_Service_Exception message is like “A service error occurred: Error calling PUT https://www.googleapis.com/upload/youtube/v3/videos?part=status%2Csnippet&uploadType=resumable&upload_id=xxx: (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 nnn bytes, which does not meet this requirement.”, where nnn is less than 262144 and seems to be the last request.

When I access the Youtube channel I can see the new videos with a status “Preparing upload” or stuck with a fixed percentage.

My code has not changed for months but now I can’t upload any new video.

Anyone can please help me to know what’s wrong? Thanks in advance!

Advertisement

Answer

Maybe you can try like that and then let MediaFileUpload cut the chunk itself:

$media = new Google_Http_MediaFileUpload(
   $client,
   $insertRequest,
   'video/*',
   file_get_contents($pathToFile),  <== put file content instead of null
   true,
   $chunkSizeBytes
);
$media->setFileSize($size);

$status = false;
while (!$status) {
   $status = $media->nextChunk();
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement