PHP:使用同步标记上传的YouTube v3 API标题

时间:2015-07-14 14:00:43

标签: php youtube youtube-data-api

过去几周,我的同事和我一直在努力通过v3 API尝试为我们的客户提供YouTube视频的标题。大约一周后,我们终于能够将字幕上传得很好但是,YouTube会在用户界面中显示此消息“跟踪内容未处理”,并且不会显示我们上传的字幕。但是,我们可以下载上传的原始格式;所以我们知道该文件已成功上传。

我们还能够获得同步标记,告诉YouTube运行成绩单并设置视频的时间,但实际上并不起作用。它返回告诉我们它正在同步,但是当我们转到视频的UI时,它只显示字幕轨道名称,并向我们发送消息“跟踪内容未处理。”。我们用完了所有的时间,现在我们正在努力解决这个问题,但仍然没有运气。

以前有人遇到过这个问题吗?如果是这样,你能做些什么才能让它发挥作用?

我将在下面发布我的代码片段,其中显示了我们脚本的上传部分。

# 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);

谢谢你,
泰勒斯坦豪斯

2 个答案:

答案 0 :(得分:5)

您是否尝试使用Google自行发布的功能实现您的目标?

以下摘自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>';
}

答案 1 :(得分:3)

我能够重现这个问题,并找到了可能的修复方法。关键是上传的字幕文件的内容。提示是它在the documentation中所说的内容:

  

同步参数指示YouTube是否应自动将字幕文件与视频的音轨同步。如果您将值设置为true YouTube将忽略上传字幕文件中的所有时间码,并为字幕生成新的时间码

     

如果您要上传没有时间码的记录,或者您怀疑文件中的时间码不正确并且希望YouTube尝试使用,请将sync参数设置为true解决它们。

使它适用于我的调整是添加一些我知道不正确的虚拟时间码,并设置'sync' => 'true',以便YouTube服务纠正它们。例如,以下是 NOT 工作的.sbv文件:

This is a sample video to test the YouTube API captioning system.

当我使用此文件时,我遇到了同样的错误,即Track content is not processed,但是当我将其更改为此时,它有效:

00:00:00,00:00:00
This is a sample video to test the YouTube API captioning system.

当我从YouTube下载处理过的.sbv文件时,它看起来像这样:

0:00:00.000,0:00:04.266
This is a sample video to test the YouTube
API captioning system.

当然,我只是为a VERY trivial video尝试了这个,我并不认为他们在时间上做得很好,但希望它可以扩展到你的系统。