Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: YouTube v3 API Captions Upload with Sync Flag

For the past couple weeks my co workers and me have been working on trying to get captions on our clients YouTube video's through the v3 API. After about week we were finally able to get the captions to upload just fine but, YouTube would give us this message in the UI "Track content is not processed" and doesn't display the caption's that we upload. However, we can download the original format that was upload; so we know the file was uploaded successfully.

We also were able to get the sync flag to work that tells YouTube to run through the transcript and set timings for the video but, it doesn't actually work. It returns telling us that it is syncing but when we go to the UI for the video it just shows the caption track name and give's us the message "Track content is not processed.". We've used up all the hours that we had and we're now working on our own time to solve this problem but still no luck.

Has anyone ran into this problem before? If so, what were you able to do to get this to work?

I will post a snippet of my code below that shows the upload portion of our script.

# 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);
$captionSnippet->setIsDraft( true );

# Create a caption with snippet.
$caption = new Google_Service_YouTube_Caption();
$caption->setSnippet($captionSnippet);

// Setting the defer flag to true tells the client to return a request which can be called
$client->setDefer(false);

// Get the file content's of the uploaded file
$file = file_get_contents( $captionFile['tmp_name'] );

// Create a request for the API's captions.insert method to create and upload a caption.
$insertRequest = $youtube->captions->insert("snippet", $caption, array( 
  'sync' => true, 
  'data' => $file, 
  'mimeType' => 'application/octet-stream', 
  'uploadType' => 'multipart' )  
); 

echo '<pre>'; print_r( $insertRequest ); echo '</pre>';

// // Read the caption file and upload it chunk by chunk.
$status = $insertRequest;
fclose($handle);

// If you want to make other calls after the file upload, set setDefer back to false
$client->setDefer(false);

Thank you,
Tyler Steinhaus

like image 794
Tyler Steinhaus Avatar asked Jul 14 '15 14:07

Tyler Steinhaus


1 Answers

Have you tried to achieve what you want using the functions Google have posted themselves?

Below taken from https://developers.google.com/youtube/v3/code_samples/php

/**
 * 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>';
}
like image 106
Professor Abronsius Avatar answered Oct 02 '22 10:10

Professor Abronsius