protected function submitCaption(Google_Service_YouTube $youtube, KalturaYouTubeApiCaptionDistributionInfo $captionInfo, $remoteId) { if (!file_exists($captionInfo->filePath)) { throw new KalturaDistributionException("The caption file [{$captionInfo->filePath}] was not found (probably not synced yet), the job will retry"); } $captionSnippet = new Google_Service_YouTube_CaptionSnippet(); $captionSnippet->setVideoId($remoteId); $captionSnippet->setLanguage($captionInfo->language); $captionSnippet->setName($captionInfo->label); $caption = new Google_Service_YouTube_Caption(); $caption->setSnippet($captionSnippet); $chunkSizeBytes = 1 * 1024 * 1024; $youtube->getClient()->setDefer(true); $insertRequest = $youtube->captions->insert('snippet', $caption); $media = new Google_Http_MediaFileUpload($youtube->getClient(), $insertRequest, '*/*', null, true, $chunkSizeBytes); $media->setFileSize(filesize($captionInfo->filePath)); $ingestedCaption = false; $handle = fopen($captionInfo->filePath, "rb"); while (!$ingestedCaption && !feof($handle)) { $chunk = fread($handle, $chunkSizeBytes); $ingestedCaption = $media->nextChunk($chunk); } fclose($handle); $youtube->getClient()->setDefer(false); $remoteMediaFile = new KalturaDistributionRemoteMediaFile(); $remoteMediaFile->remoteId = $ingestedCaption['id']; $remoteMediaFile->version = $captionInfo->version; $remoteMediaFile->assetId = $captionInfo->assetId; return $remoteMediaFile; }
/** * Updates a caption track's draft status to publish it. * Updates the track with a new binary file as well if it is present. (captions.update) * * @param Google_Service_YouTube $youtube YouTube service object. * @param Google_Client $client Google client. * @param string $captionId The id parameter specifies the caption ID for the resource * that is being updated. In a caption resource, the id property specifies the * caption track's ID. * @param $htmlBody - html body. * @param $captionFile caption track binary file. */ function updateCaption(Google_Service_YouTube $youtube, Google_Client $client, $captionId, &$htmlBody, $captionFile) { // Modify caption's isDraft property to unpublish a caption track. $updateCaptionSnippet = new Google_Service_YouTube_CaptionSnippet(); $updateCaptionSnippet->setIsDraft(true); # Create a caption with snippet. $updateCaption = new Google_Service_YouTube_Caption(); $updateCaption->setSnippet($updateCaptionSnippet); $updateCaption->setId($captionId); if ($captionFile == '') { // Call the YouTube Data API's captions.update method to update an existing caption track. $captionUpdateResponse = $youtube->captions->update("snippet", $updateCaption); $htmlBody .= "<h2>Updated caption track</h2><ul>"; $htmlBody .= sprintf('<li>%s(%s) draft status: %s</li>', $captionUpdateResponse['snippet']['name'], $captionUpdateResponse['id'], $captionUpdateResponse['snippet']['isDraft']); $htmlBody .= '</ul>'; } else { // Specify the size of each chunk of data, in bytes. Set a higher value for // reliable connection as fewer chunks lead to faster uploads. Set a lower // value for better recovery on less reliable connections. $chunkSizeBytes = 1 * 1024 * 1024; // Setting the defer flag to true tells the client to return a request which can be called // with ->execute(); instead of making the API call immediately. $client->setDefer(true); // Create a request for the YouTube Data API's captions.update method to update // an existing caption track. $captionUpdateRequest = $youtube->captions->update("snippet", $updateCaption); // Create a MediaFileUpload object for resumable uploads. $media = new Google_Http_MediaFileUpload($client, $captionUpdateRequest, '*/*', null, true, $chunkSizeBytes); $media->setFileSize(filesize($captionFile)); // Read the caption file and upload it chunk by chunk. $status = false; $handle = fopen($captionFile, "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); $htmlBody .= "<h2>Updated caption track</h2><ul>"; $htmlBody .= sprintf('<li>%s(%s) draft status: %s and updated the track with the new uploaded file.</li>', $status['snippet']['name'], $status['id'], $status['snippet']['isDraft']); $htmlBody .= '</ul>'; } }