通过Youtube Api v3发送视频会导致服务器冻结

时间:2016-03-08 06:21:45

标签: php youtube-api

我想创建网站,用户可以将视频发送到Youtube。

使用Google文档,我编写了以下代码:

$youtube = new \Google_Service_YouTube($this->client);
        // REPLACE this value with the path to the file you are uploading.
        $videoPath = $movieEntity->getAbsoluteMoviePath();
        // Create a snippet with title, description, tags and category ID
        // Create an asset resource and set its snippet metadata and type.
        // This example sets the video's title, description, keyword tags, and
        // video category.
        $snippet = new \Google_Service_YouTube_VideoSnippet();
        $snippet->setTitle( $movieEntity->getTitle() );
        $snippet->setDescription( $movieEntity->getDescription() );
        $snippet->setTags( $movieEntity->getTags() );
        // 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.
        $this->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(
            $this->client,
            $insertRequest,
            'video/*',
            null,
            true,
            $chunkSizeBytes
        );

        $filesize = filesize($videoPath);
        $media->setFileSize($filesize);
        // 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
        $this->client->setDefer(false);

它工作正常,但在转移过程中我的服务器完全冻结 - 直到转移结束我无法打开任何其他网站子页面。我试过我的本地机器和专用服务器,我不知道,这有什么不对......

你能给我一些提示吗?

最好的问候

0 个答案:

没有答案
相关问题