Example #1
0
 /**
  * Upload Video.
  *
  * @param \Userdesk\Submission\Classes\SubmissionVideoItem $item;
  *
  * @return \Userdesk\Submission\Classes\SubmissionResult;
  */
 public function uploadVideo(SubmissionVideoItem $item)
 {
     $google = $this->providerFromToken($this->token);
     try {
         $youtube = new \Google_Service_YouTube($google);
         $videoPath = $item->getVideo();
         if (!empty($videoPath)) {
             // Create a snipet with title, description, tags and category id
             $snippet = new \Google_Service_YouTube_VideoSnippet();
             $snippet->setTitle($item->getTitle());
             $snippet->setDescription($item->getDescription());
             $snippet->setTags(explode(',', $item->getKeywords()));
             // Numeric video category. See
             // https://developers.google.com/youtube/v3/docs/videoCategories/list
             $snippet->setCategoryId("22");
             $status = new \Google_Service_YouTube_VideoStatus();
             $status->privacyStatus = "public";
             // Create a YouTube video with snippet and status
             $video = new \Google_Service_YouTube_Video();
             $video->setSnippet($snippet);
             $video->setStatus($status);
             // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
             // for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
             $chunkSizeBytes = 1 * 1024 * 1024;
             $google->setDefer(true);
             $insertRequest = $youtube->videos->insert("status,snippet", $video);
             // Create a MediaFileUpload with resumable uploads
             $media = new \Google_Http_MediaFileUpload($google, $insertRequest, 'video/*', null, true, $chunkSizeBytes);
             $media->setFileSize(filesize($videoPath));
             // Create a video insert request
             $uploadStatus = false;
             // Read file and upload chunk by chunk
             $handle = fopen($videoPath, "rb");
             while (!$uploadStatus && !feof($handle)) {
                 $chunk = fread($handle, $chunkSizeBytes);
                 $uploadStatus = $media->nextChunk($chunk);
             }
             fclose($handle);
             $google->setDefer(false);
             if (!empty($uploadStatus) && !empty($uploadStatus->id)) {
                 $url = "http://www.youtube.com/watch?v=" . $uploadStatus->id;
                 return new SubmissionResult(true, '', $url);
             }
             return new SubmissionResult(false, 'Video Upload Failed');
         }
     } catch (\Google_ServiceException $e) {
         throw new InvalidUploadException($e->getMessage());
     } catch (\Google_Exception $e) {
         throw new InvalidUploadException($e->getMessage());
     } catch (Exception $e) {
         throw new InvalidUploadException($e->getMessage());
     }
 }