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;
 }
Example #2
0
/**
 * Uploads a caption track in draft status that matches the API request parameters.
 * (captions.insert)
 *
 * @param Google_Service_YouTube $youtube YouTube service object.
 * @param Google_Client $client Google client.
 * @param $videoId the YouTube video ID of the video for which the API should
 *  return caption tracks.
 * @param $captionLanguage language of the caption track.
 * @param $captionName name of the caption track.
 * @param $captionFile caption track binary file.
 * @param $htmlBody html body.
 */
function uploadCaption(Google_Service_YouTube $youtube, Google_Client $client, $videoId, $captionFile, $captionName, $captionLanguage, &$htmlBody)
{
    # Insert a video caption.
    # Create a caption snippet with video id, language, name and draft status.
    $captionSnippet = new Google_Service_YouTube_CaptionSnippet();
    $captionSnippet->setVideoId($videoId);
    $captionSnippet->setLanguage($captionLanguage);
    $captionSnippet->setName($captionName);
    # Create a caption with snippet.
    $caption = new Google_Service_YouTube_Caption();
    $caption->setSnippet($captionSnippet);
    // 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 API's captions.insert method to create and upload a caption.
    $insertRequest = $youtube->captions->insert("snippet", $caption);
    // Create a MediaFileUpload object for resumable uploads.
    $media = new Google_Http_MediaFileUpload($client, $insertRequest, '*/*', 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>Inserted video caption track for</h2><ul>";
    $captionSnippet = $status['snippet'];
    $htmlBody .= sprintf('<li>%s(%s) in %s language, %s status.</li>', $captionSnippet['name'], $status['id'], $captionSnippet['language'], $captionSnippet['status']);
    $htmlBody .= '</ul>';
}