Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:YouTube v3 API标题上载(带有同步标志)_Php_Youtube_Youtube Data Api - Fatal编程技术网

PHP:YouTube v3 API标题上载(带有同步标志)

PHP:YouTube v3 API标题上载(带有同步标志),php,youtube,youtube-data-api,Php,Youtube,Youtube Data Api,在过去的几周里,我的同事和我一直在努力通过V3API在客户的YouTube视频上获取字幕。大约一周后,我们终于可以上传字幕了,但YouTube会在UI中给我们发送这样一条消息:“未处理曲目内容”,并且不会显示我们上传的字幕。但是,我们可以下载上传的原始格式;因此,我们知道该文件已成功上载 我们还能够让sync标志生效,它告诉YouTube浏览成绩单并为视频设置时间,但实际上它不起作用。它返回告诉我们它正在同步,但当我们转到视频的UI时,它只显示标题曲目名称,并给我们消息“未处理曲目内容”。。我们

在过去的几周里,我的同事和我一直在努力通过V3API在客户的YouTube视频上获取字幕。大约一周后,我们终于可以上传字幕了,但YouTube会在UI中给我们发送这样一条消息:“未处理曲目内容”,并且不会显示我们上传的字幕。但是,我们可以下载上传的原始格式;因此,我们知道该文件已成功上载

我们还能够让sync标志生效,它告诉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);
#插入视频标题。
#创建带有视频id、语言、名称和草稿状态的标题片段。
$captionSnippet=新的Google_服务_YouTube_captionSnippet();
$CAPTIONSHIPET->setVideoId($videoId);
$captionsnipet->setLanguage($captionLanguage);
$CAPTIONSNIPET->setName($captionName);
$CAPTIONSHIPET->setIsDraft(正确);
#使用代码段创建标题。
$caption=新的Google_服务_YouTube_caption();
$caption->setsnipet($captionsnipet);
//将defer标志设置为true会告诉客户端返回一个可以调用的请求
$client->setDefer(false);
//获取上传文件的文件内容
$file=file_get_contents($captionFile['tmp_name']);
//创建API标题的请求。插入方法以创建和上载标题。
$insertRequest=$youtube->captions->insert(“代码段”),$caption,数组(
“sync”=>true,
“数据”=>$file,
'mimeType'=>'应用程序/八位字节流',
'uploadType'=>'multipart')
); 
回声';打印请求($insertRequest);回声';
////读取标题文件并逐块上传。
$status=$insertRequest;
fclose($handle);
//如果要在文件上载后进行其他调用,请将setDefer设置回false
$client->setDefer(false);
谢谢你,

泰勒·斯坦豪斯

你是否尝试过使用谷歌发布的功能来实现你想要的

以下摘自

/**
*以草稿状态上载与API请求参数匹配的标题曲目。
*(标题.插入)
*
*@param Google_Service_YouTube$YouTube YouTube服务对象。
*@param Google_Client$Client Google Client。
*@param$videoId应为其设置API的视频的YouTube视频ID
*返回字幕曲目。
*@param$captionLanguage字幕曲目的语言。
*@param$captionName标题曲目的名称。
*@param$captionFile字幕跟踪二进制文件。
*@param$htmlBody html body。
*/
函数uploadCaption(谷歌搜索服务YouTube$YouTube,谷歌搜索客户端$Client,$videoId,
$captionFile、$captionName、$captionLanguage和$htmlBody){
#插入视频标题。
#创建带有视频id、语言、名称和草稿状态的标题片段。
$captionSnippet=新的Google_服务_YouTube_captionSnippet();
$CAPTIONSHIPET->setVideoId($videoId);
$captionsnipet->setLanguage($captionLanguage);
$CAPTIONSNIPET->setName($captionName);
#使用代码段创建标题。
$caption=新的Google_服务_YouTube_caption();
$caption->setsnipet($captionsnipet);
//以字节为单位指定每个数据块的大小。为
//可靠的连接,因为数据块越少,上传速度越快。设置一个较低的
//在可靠性较低的连接上实现更好恢复的价值。
$chunkSizeBytes=1*1024*1024;
//将defer标志设置为true会告诉客户端返回一个可以调用的请求
//使用->execute();而不是立即进行API调用。
$client->setDefer(true);
//创建API标题的请求。插入方法以创建和上载标题。
$insertRequest=$youtube->caption->insert(“代码段”,$caption);
//为可恢复上载创建MediaFileUpload对象。
$media=新的Google\u Http\u MediaFileUpload(
$client,
$insertRequest,
'*/*',
无效的
是的,
$chunkSizeBytes
);
$media->setFileSize(filesize($captionFile));
//阅读标题文件并逐块上传。
$status=false;
$handle=fopen($captionFile,“rb”);
while(!$status&&!feof($handle)){
$chunk=fread($handle,$chunkSizeBytes);
$status=$media->nextChunk($chunk);
}
fclose($handle);
//如果要在文件上载后进行其他调用,请将setDefer设置回false
$client->setDefer(false);
$htmlBody.=“为
    插入视频字幕曲目”; $captionSnippet=$status['snippet']; $htmlBody.=sprintf(“
  • %s(%s)使用%s语言,%s状态。
  • ”, $CAPTIONSHIPET['name']、$status['id']、$CAPTIONSHIPET['language'], $captionSnippet['status']); $htmlBody.='
'; }
我能够重现这个问题,并找到了一个可能的解决方案。密钥是上传的标题文件的内容。其中的暗示是:

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

如果您正在上载没有时间代码的成绩单,或者如果您怀疑文件中的时间代码不正确且不正确,则应将
sync
参数设置为
true
/**
 * 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>';
}
This is a sample video to test the YouTube API captioning system.
00:00:00,00:00:00
This is a sample video to test the YouTube API captioning system.
0:00:00.000,0:00:04.266
This is a sample video to test the YouTube
API captioning system.