PHP:使用同步标志上传 YouTube v3 API 字幕 [英] PHP: YouTube v3 API Captions Upload with Sync Flag

查看:48
本文介绍了PHP:使用同步标志上传 YouTube v3 API 字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几周里,我和我的同事一直在努力通过 v3 API 为我们客户的 YouTube 视频获取字幕.大约一周后,我们终于能够正常上传字幕,但是,YouTube 会在用户界面中向我们显示此消息曲目内容未处理",并且不显示我们上传的字幕.但是,我们可以下载上传的原始格式;这样我们就知道文件上传成功了.

我们还能够让同步标志起作用,它告诉 YouTube 运行脚本并为视频设置时间,但它实际上不起作用.它返回告诉我们它正在同步,但是当我们转到视频的 UI 时,它只显示字幕轨道名称并向我们提供消息轨道内容未处理.".我们已经用完了所有的时间,现在我们正在用自己的时间解决这个问题,但仍然没有运气.

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

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

# 插入视频标题.# 创建带有视频 ID、语言、名称和草稿状态的字幕片段.$captionSnippet = new Google_Service_YouTube_CaptionSnippet();$captionSnippet->setVideoId($videoId);$captionSnippet->setLanguage($captionLanguage);$captionSnippet->setName($captionName);$captionSnippet->setIsDraft(true);# 创建带有片段的标题.$caption = new Google_Service_YouTube_Caption();$caption->setSnippet($captionSnippet);//将 defer 标志设置为 true 告诉客户端返回一个可以调用的请求$client->setDefer(false);//获取上传文件的文件内容$file = file_get_contents( $captionFile['tmp_name'] );//为 API 的 captions.insert 方法创建请求以创建和上传标题.$insertRequest = $youtube->captions->insert("snippet", $caption, array('同步' =>真的,'数据' =>$文件,'mimeType' =>'应用程序/八位字节流','上传类型' =>'多部分'));echo '

';print_r( $insertRequest );echo '</pre>';////读取字幕文件并逐块上传.$status = $insertRequest;fclose($handle);//如果要在文件上传后进行其他调用,将setDefer 设置回false$client->setDefer(false);

谢谢,
泰勒施泰因豪斯

解决方案

您是否尝试过使用 Google 自己发布的功能来实现您想要的?

以下摘自 https://developers.google.com/youtube/v3/code_samples/php

/*** 上传与 API 请求参数匹配的草稿状态的字幕轨道.* (captions.insert)** @param Google_Service_YouTube $youtube YouTube 服务对象.* @param Google_Client $client 谷歌客户端.* @param $videoId API 应该针对的视频的 YouTube 视频 ID* 返回字幕轨道.* @param $captionLanguage 字幕轨道的语言.* @param $captionName 字幕轨道的名称.* @param $captionFile 字幕轨道二进制文件.* @param $htmlBody html 正文.*/函数uploadCaption(Google_Service_YouTube $youtube, Google_Client $client, $videoId,$captionFile, $captionName, $captionLanguage, &$htmlBody) {# 插入视频标题.# 创建带有视频 ID、语言、名称和草稿状态的字幕片段.$captionSnippet = new Google_Service_YouTube_CaptionSnippet();$captionSnippet->setVideoId($videoId);$captionSnippet->setLanguage($captionLanguage);$captionSnippet->setName($captionName);# 创建带有片段的标题.$caption = new Google_Service_YouTube_Caption();$caption->setSnippet($captionSnippet);//指定每个数据块的大小,以字节为单位.设置更高的值//可靠的连接,因为更少的块会导致更快的上传.设置一个较低的//在不太可靠的连接上更好地恢复的值.$chunkSizeBytes = 1 * 1024 * 1024;//将 defer 标志设置为 true 告诉客户端返回一个可以调用的请求//使用 ->execute();而不是立即进行 API 调用.$client->setDefer(true);//为 API 的 captions.insert 方法创建请求以创建和上传标题.$insertRequest = $youtube->captions->insert("snippet", $caption);//为可恢复的上传创建一个 MediaFileUpload 对象.$media = 新 Google_Http_MediaFileUpload($客户,$插入请求,'*/*',空值,真的,$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 .= "<h2>为</h2><ul>插入了视频字幕轨道";$captionSnippet = $status['snippet'];$htmlBody .= sprintf('
  • %s(%s) in %s language, %s status.</li>',$captionSnippet['name'], $status['id'], $captionSnippet['language'],$captionSnippet['status']);$htmlBody .= '</ul>';}
  • 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

    解决方案

    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>';
    }
    

    这篇关于PHP:使用同步标志上传 YouTube v3 API 字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆