php - 将视频上传到特定的播放列表

标签 php youtube google-api youtube-api youtube-data-api

我正在使用youtube数据API将视频上传到我的 channel 。效果很好,但是当我上传视频时,它会上传到我的 channel 中。我希望它在给定的播放列表中上传。(我已经创建了一些播放列表)。有什么办法可以在以下代码中给playlistId?我不想使用playlistitem.insert。
提前致谢

    $OAUTH2_CLIENT_ID = 'ds';
    $OAUTH2_CLIENT_SECRET = 'sd';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

$tokenExisted = $this->Token->find('first');

 if(!empty($tokenExisted)){

$token = $tokenExisted['Token']['token'];
$refreshToken = $tokenExisted['Token']['ref_token'];
$client->refreshToken($refreshToken);
$token = $client->getAccessToken();

}





if (isset($token)) {

  $client->setAccessToken($token);

}

if ($client->isAccessTokenExpired()){    

$refreshToken = $tokenExisted['Token']['ref_token'];
$client->refreshToken($refreshToken);
$token = $client->getAccessToken();

}else{

 $token = $client->getAccessToken();
}
$token=json_encode($token);

// Check to ensure that the access token was successfully acquired.
 if ($token) {
 try{
// REPLACE this value with the path to the file you are uploading.
$videoPath = $dest . '/' . $img_name . '.' . $image['1'];


// 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($input['title']);
$snippet->setDescription($input['description']);
//$snippet->playlistId($playlistId);

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

// 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(false);

// 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);



} catch (Google_Service_Exception $e) {
$htmlBody = sprintf('<p>A service error occurred: <code>%s</code></p>',
    htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody = sprintf('<p>An client error occurred: <code>%s</code></p>',
    htmlspecialchars($e->getMessage()));
}


  } else {
 // If the user hasn't authorized the app, initiate the OAuth flow

 $state = mt_rand();
 $client->setState($state);
 $_SESSION['state'] = $state;

 $authUrl = $client->createAuthUrl();

 }

最佳答案

根据此SO answer,您不能将视频直接上传到播放列表。通常,上传视频时,视频会重定向到您的 channel 。将其上传到 channel 后,您现在可以将其放入播放列表中。

如果视频已经上传,则可以遵循此Adding a video to a playlist文档。您可以使用VideoEntry对象将视频添加到播放列表。

这是一个示例代码,它使用已知的条目ID检索VideoEntry对象,然后将其添加到与PlaylistListEntry对象相对应的播放列表中。由于请求未指定视频在播放列表中出现的位置,因此将新视频添加到播放列表的末尾。

$postUrl = $playlistToAddTo->getPlaylistVideoFeedUrl();
// video entry to be added
$videoEntryToAdd = $yt->getVideoEntry('4XpnKHJAok8');

// create a new Zend_Gdata_PlaylistListEntry, passing in the underling DOMElement of the VideoEntry
$newPlaylistListEntry = $yt->newPlaylistListEntry($videoEntryToAdd->getDOM());

// post
try {
  $yt->insertEntry($newPlaylistListEntry, $postUrl);
} catch (Zend_App_Exception $e) {
  echo $e->getMessage();
}

这是一条相关的SO帖子,可能会对您有所帮助:
  • Youtube API (PHP) - how to add (existing) video to existing playlist?
  • 关于php - 将视频上传到特定的播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40514212/

    相关文章:

    php - 一个 Laravel 4 安装的多个应用程序/项目

    php - PHP 5.3 和 5.4 中访问数组的差异或某些配置不匹配?

    google-analytics - 按页面路径过滤年龄范围和性别

    android - Google Spreadsheet API update\edit with protocol

    php - 如何在用户定义的 symfony Bundle 中配置 Doctrine Entity Annotation 自动加载?

    javascript - 如何访问附加到 IFrame 的现有 YouTube 播放器?

    html - Nintendo 3DS YouTube嵌入支持吗?

    python - 在 Python 中使用 Selenium 查找 YouTube 视频中评论数量的 CSS 选择器应该是什么?

    Node.js - 我不断收到以下错误 : Error: ffmpeg stream: write EPIPE

    php - 整个 PHP 代码运行两次