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

时间:2016-11-09 19:05:32

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

我正在使用youtube data api将视频上传到我的频道。这很好用,但是当我上传视频时,它会在我的频道中上传。我希望它在给定的播放列表中上传。(我已经创建了一些播放列表)。有什么办法可以在下面的代码中给出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();

 }

1 个答案:

答案 0 :(得分:0)

根据此SO answer,您无法将视频直接上传到播放列表中。上传视频时,它会重定向到您的频道,这是正常的。一旦将其上传到频道,您现在可以将其放入播放列表中。

如果视频已上传,您可以按照此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帖子,可能有所帮助: