将视频上传到 youtube 中的特定播放列表 [英] Upload Video to Specific Playlist in youtube

查看:35
本文介绍了将视频上传到 youtube 中的特定播放列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将视频上传到 youtube 中的特定播放列表.我的频道中有一个播放列表,但是当我上传视频时,它会上传到 channel 而不是 playlist.

I want upload video to specific Playlist in youtube.I have One playlist in my channel but when i upload video it uploads in channel not in playlist.

我想获取我的播放列表的列表 (Title,ID etc) 并将视频上传到我已经创建的 playlist 之一.已阅读有关谷歌的内容,但不明白如何操作.

I want to get list (Title,ID etc) of my playlist and upload video to one of that playlist which i have already created how to do that.I have read this about google but could not understand how to that.

这是当前将视频添加到频道的脚本.

Here is the script which currently add video to channel.

<?php

$key = file_get_contents('text.txt');

set_include_path(get_include_path() . PATH_SEPARATOR . 'YoutubeAPI');
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';

$application_name = 'My APP NAME';
$client_secret = 'SECRET';
$client_id = 'CLIENT ID';
$scope = array('https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube', 'https://www.googleapis.com/auth/youtubepartner');

$videoPath = "Video/vdeo.mp4";
$videoTitle = "Tutorial";
$videoDescription = "A must watch video";
$videoCategory = "22";
$videoTags = array("youtube", "tutorial");


try {
    // Client init
    $client = new Google_Client();
    //$client->setApplicationName($application_name);
    $client->setClientId($client_id);
    $client->setAccessType('offline');
    $client->setAccessToken($key);
    $client->setScopes($scope);
    $client->setClientSecret($client_secret);

    if ($client->getAccessToken()) {

        /**
         * Check to see if our access token has expired. If so, get a new one and save it to file for future use.
         */
        if ($client->isAccessTokenExpired()) {
            $newToken = json_decode($client->getAccessToken());
            $client->refreshToken($newToken->refresh_token);
            file_put_contents('text.txt', $client->getAccessToken());
        }

        $youtube = new Google_Service_YouTube($client);



        // Create a snipet with title, description, tags and category id
        $snippet = new Google_Service_YouTube_VideoSnippet();
        $snippet->setTitle($videoTitle);
        $snippet->setDescription($videoDescription);
        $snippet->setCategoryId($videoCategory);
        $snippet->setTags($videoTags);

        // Create a video status with privacy status. Options are "public", "private" and "unlisted".
        $status = new Google_Service_YouTube_VideoStatus();
        $status->setPrivacyStatus('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;

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

        /**
         * Video has successfully been upload, now lets perform some cleanup functions for this video
         */
        if ($status->status['uploadStatus'] == 'uploaded') {
            // Actions to perform for a successful upload
        }
        print_r($status);
        // If you want to make other calls after the file upload, set setDefer back to false
        $client->setDefer(true);
    } else {
        // @TODO Log error
        echo 'Problems creating the client';
    }
} catch (Google_Service_Exception $e) {
    print "Caught Google service Exception " . $e->getCode() . " message is " . $e->getMessage();
    print "Stack trace is " . $e->getTraceAsString();
} catch (Exception $e) {
    print "Caught Google service Exception " . $e->getCode() . " message is " . $e->getMessage();
    print "Stack trace is " . $e->getTraceAsString();
}

提前致谢.

推荐答案

是的,是正确的.上传视频时,它将重定向到您的频道.您不能将视频直接上传到播放列表中.从频道,您可以将视频放入eplaylist.为此,您必须执行下面列出的 2(两)个步骤:

Yes, it is correct. When uploading video it will redirect to your channel. You cannot upload video directly into a playlist. From channel, you can put video in eplaylist. To do that you have to execute 2(two) steps listed below:

第 1 步:检索适当的播放列表 ID

调用 playlist.list 方法以检索当前已验证用户频道中的播放列表.上面用于检索当前用户播放列表的示例请求演示了此请求.调用 API 的应用程序可以处理 API 响应以显示播放列表列表,使用每个播放列表的 ID 作为键.

Call the playlist.list method to retrieve the playlists in the currently authenticated user's channel. The sample request above for retrieving the current user's playlists demonstrates this request. The application calling the API could process the API response to display a list of playlists, using each playlist's ID as a key.

第 2 步:将视频添加到播放列表

调用 playlistItems.insert 方法将视频添加到播放列表.此请求必须使用 OAuth 2.0 进行授权.请求正文是一个 playlistItem 资源,它至少指定了以下值:

Call the playlistItems.insert method to add a video to the playlist. This request must be authorized using OAuth 2.0. The request body is a playlistItem resource that specifies at least the following values:

snippet.playlistId 标识您要将喜爱的视频添加到的播放列表.这是您在步骤 1 中获得的播放列表 ID.snippet.resourceId.kind 包含值 youtube#video.snippet.resourceId.videoId 标识您添加为收藏的视频.属性值是唯一的 YouTube 视频 ID.

The snippet.playlistId identifies the playlist to which you are adding the favorite video. This is the playlist ID you obtained in step 1. The snippet.resourceId.kind contains the value youtube#video. The snippet.resourceId.videoId identifies the video that you are adding as a favorite. The property value is a unique YouTube video ID.

要在 APIs Explorer 中完成请求,您需要为 snippet.playlistIdsnippet.resourceId.videoId 属性设置值.https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlistItems.insert?part=snippet

To complete the request in the APIs Explorer, you need to set values for the snippet.playlistId and snippet.resourceId.videoId properties. https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlistItems.insert?part=snippet

这篇关于将视频上传到 youtube 中的特定播放列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆