PHP:带有同步标志的 YouTube v3 API 字幕上传

标签 php youtube youtube-data-api

在过去的几周里,我和我的同事一直致力于通过 v3 API 为我们客户的 YouTube 视频获取字幕。大约一周后,我们终于能够顺利上传字幕,但是 YouTube 会在 UI 中给我们这条消息“未处理轨道内容”,并且不显示我们上传的字幕.但是,我们可以下载上传的原始格式;所以我们知道文件已成功上传。

我们还能够让同步标志起作用,告诉 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);

谢谢你,
泰勒·斯坦豪斯

最佳答案

您是否尝试过使用 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>';
}

关于PHP:带有同步标志的 YouTube v3 API 字幕上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31408873/

相关文章:

ios - 表格单元格横向模式下的 Youtube 播放器

javascript - JS/Jquery youtube 视频无法播放

java - 如何将YouTube播放列表解析为ArrayList

php - 如何获得两个特定日期之间的YouTube视频观看次数?

google-apps-script - Google Apps 脚本返回错误 "Limit Exceeded: URLFetch URL Length"

PHP:禁用@错误控制运算符

共享服务器上的 PHP 5.4 旧身份验证,无需编辑 mysql 服务器即可修复

android - YouTube 数据 API v3 身份验证错误

javascript - 每次单击按钮时加载相同的 div

php - 避免两个用户同时发帖时ID混淆?