当前位置: 首页 > 知识库问答 >
问题:

使用Youtube API V3和PHP将视频上传到Youtube

韶和璧
2023-03-14

我正试图用PHP上传一个视频到Youtube。我用的是Youtube API v3,我用的是最新的谷歌API PHP客户端库的源代码。< br >我使用< br > https://code.google.com/p/google-api-php-client/上给出的示例代码来执行身份验证。身份验证顺利通过,但当我尝试上传视频时,我得到< code > Google _ service exception ,错误代码为500,消息为null。

我看了一下之前提出的以下问题:使用php客户端库v3将视频上传到youtube但接受的答案没有描述如何指定要上传的文件数据。
我发现了另一个类似的问题使用YouTubeAPI v3和PHP上传文件,其中在评论中提到分类ID是强制性的,因此我尝试在片段中设置分类ID,但它仍然给出了相同的异常。

我还参考了文档站点上的Python代码(https://developers.google.com/youtube/v3/docs/videos/insert),但我在客户端库中找不到函数next_chunk。但是我试图放一个循环(在代码片段中提到)来重试获取错误代码500,但在所有10次迭代中,我都得到了相同的错误。

以下是我正在尝试的代码片段:

$youTubeService = new Google_YoutubeService($client);
if ($client->getAccessToken()) {
    print "Successfully authenticated";
    $snippet = new Google_VideoSnippet();
    $snippet->setTitle = "My Demo title";
    $snippet->setDescription = "My Demo descrition";
    $snippet->setTags = array("tag1","tag2");
    $snippet->setCategoryId(23); // this was added later after refering to another question on stackoverflow

    $status = new Google_VideoStatus();
    $status->privacyStatus = "private";

    $video = new Google_Video();
    $video->setSnippet($snippet);
    $video->setStatus($status);

    $data = file_get_contents("video.mp4"); // This file is present in the same directory as the code
    $mediaUpload = new Google_MediaFileUpload("video/mp4",$data);
    $error = true;
    $i = 0;

    // I added this loop because on the sample python code on the documentation page
    // mentions we should retry if we get error codes 500,502,503,504
    $retryErrorCodes = array(500, 502, 503, 504);
    while($i < 10 && $error) {
        try{
            $ret = $youTubeService->videos->insert("status,snippet", 
                                                   $video, 
                                                   array("data" => $data));

            // tried the following as well, but even this returns error code 500,
            // $ret = $youTubeService->videos->insert("status,snippet", 
            //                                        $video, 
            //                                        array("mediaUpload" => $mediaUpload); 
            $error = false;
        } catch(Google_ServiceException $e) {
            print "Caught Google service Exception ".$e->getCode()
                  . " message is ".$e->getMessage();
            if(!in_array($e->getCode(), $retryErrorCodes)){
                break;
            }
            $i++;
        }
    }
    print "Return value is ".print_r($ret,true);

    // We're not done yet. Remember to update the cached access token.
    // Remember to replace $_SESSION with a real database or memcached.
    $_SESSION['token'] = $client->getAccessToken();
} else {
    $authUrl = $client->createAuthUrl();
    print "<a href='$authUrl'>Connect Me!</a>";
}

是我做错了什么吗?

共有3个答案

牛迪
2023-03-14

我也意识到这是旧的,但是当我从GitHub克隆最新版本的php-client时,我遇到了< code > Google _ Service _ YouTube _ Videos _ Resource::insert()-方法的问题。

我将传递一个带有< code>"data" =的数组

调试和阅读我在\Google\Service\Resource中找到的谷歌代码。php检查了(第179-180行)一个数组键<code>的“uploadType”,该键将启动Google_Http_MediaFielUpload对象。

$part = 'status,snippet';
$optParams = array(
    "data" => file_get_contents($filename),
    "uploadType" => "media",  // This was needed in my case
    "mimeType" => "video/mp4",
);
$response = $youtube->videos->insert($part, $video, $optParams);

如果我没记错的话,在PHPAPI的0.6版本中,不需要uploadType参数。这可能仅适用于直接上传方式,而不适用于任何一天的答案中显示的可恢复上传。

锺威
2023-03-14

我意识到这已经很旧了,但这是文档的答案:

    // REPLACE this value with the path to the file you are uploading.
    $videoPath = "/path/to/file.mp4";

    $snippet = new Google_Service_YouTube_VideoSnippet();
    $snippet->setTitle("Test title");
    $snippet->setDescription("Test description");
    $snippet->setTags(array("tag1", "tag2"));

    // Numeric video category. See
    // https://developers.google.com/youtube/v3/docs/videoCategories/list 
    $snippet->setCategoryId("22");

    // Set the video's status to "public". Valid statuses are "public",
    // "private" and "unlisted".
    $status = new Google_Service_YouTube_VideoStatus();
    $status->privacyStatus = "public";

    // Associate the snippet and status objects with a new video resource.
    $video = new Google_Service_YouTube_Video();
    $video->setSnippet($snippet);
    $video->setStatus($status);

    // 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 videos.insert method to create and upload the video.
    $insertRequest = $youtube->videos->insert("status,snippet", $video);

    // Create a MediaFileUpload object for resumable uploads.
    $media = new Google_Http_MediaFileUpload(
        $client,
        $insertRequest,
        'video/*',
        null,
        true,
        $chunkSizeBytes
    );
    $media->setFileSize(filesize($videoPath));


    // Read the media file and upload it chunk by chunk.
    $status = false;
    $handle = fopen($videoPath, "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);
万俟玉书
2023-03-14

我能够使用以下代码使上传工作正常:

if($client->getAccessToken()) {
    $snippet = new Google_VideoSnippet();
    $snippet->setTitle("Test title");
    $snippet->setDescription("Test descrition");
    $snippet->setTags(array("tag1","tag2"));
    $snippet->setCategoryId("22");

    $status = new Google_VideoStatus();
    $status->privacyStatus = "private";

    $video = new Google_Video();
    $video->setSnippet($snippet);
    $video->setStatus($status);

    $error = true;
    $i = 0;

    try {
        $obj = $youTubeService->videos->insert("status,snippet", $video,
                                         array("data"=>file_get_contents("video.mp4"), 
                                        "mimeType" => "video/mp4"));
    } catch(Google_ServiceException $e) {
        print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage(). " <br>";
        print "Stack trace is ".$e->getTraceAsString();
    }
}
 类似资料:
  • 我试图使用API接口将视频上传到S3存储桶,我遵循了预签名的URL过程,下面是我的lambda函数,它返回预签名的URL(它正确地返回了预签名的URL,看起来): 当我尝试上传一个像这样卷曲的mp4视频时,例如: curl-X PUT-F'data=@ch01_00000100055009702.mp4'https://redacted-bucket-instance.s3.amazonaws.c

  • 我在MVC Web应用程序中使用YouTubeAPI V3。目的是让我的Web用户使用MY OAuth凭据将视频上传到MYYouTube频道。在用户将他们的视频上传到我的Web服务器后,我需要在我的Web服务器和YouTube之间自动上传,无需我自己的用户干预。 我的(初稿)代码如下。我的问题是: > < li> 更新-已在下面解决。每当我试图删除一个视频,我得到一个“未经授权”的错误。当我检查令

  • 我正在通过共享按钮将视频上传到youtube。如果我点击上传按钮,它会显示社交图标,比如whatsapp、facebook、youtube。当我点击youtube时,它应该被上传到youtube。 下面是我的代码: 选择视频后,我可以选择youtube图标。然后,您的Tube窗口会自动关闭。任何建议都是非常可观的。谢谢!

  • 这里有一个非常简单的问题,我今天遇到了,这让我非常沮丧:假设我正在上传一个视频文件,通过超文本标记语言'选择文件'输入,我有一个提交按钮,它调用一个点击java脚本函数,将视频发送到一个PHP将有关视频的数据归档并返回到原始页面,(简单的方法如echo$_FILES[$myvideo oupload]['name'])全部通过XMLhttp pRequest()。 这有可能吗? 就像现在一样,视频

  • 接口说明 上传视频文件 API地址 POST /api/marker/1.0.0/uploadVideo 是否需要登录 是 请求字段说明 参数 类型 请求类型 是否必须 说明 dataGuid string form 是 场景GUID file string form 是 视频文件 响应字段说明 参数 类型 说明 mp4UploadPath String 视频文件上传地址 响应成功示例 { "

  • 用户通过视频上传、管理视频、获取代码,实现本地视频在制定网站播放。 2.1视频上传 进入视频页面,点击上传视频 按钮,在弹出的页面点击添加视频 : 1)选择视频“分类”,添加视频“标签”(选填); 2)点击【添加视频】或者【选择文件并上传】按钮选择本地一个或多个视频,点击确认即开始视频上传;或者在本地选择一个或多个视频,将视频拖拽到视频上传区,即可进行视频上传; 3)上传过程中点击视频上传或者取消