1

我在下面创建了将视频上传到 youtube 的 php 脚本,并且上传正常。

我希望能够为上传的视频设置高级设置,如下所示:

  • 允许评论应该是“批准”而不是“全部”
  • 我还需要能够设置视频位置(这将来自我网站的数据库)
  • 我还应该能够设置录制日期(上传日期)
  • 我也不希望视频统计信息公开

我环顾四周,但我无法从谷歌找到任何关于此的文档,而且我认为谷歌没有提供完整的 API 文档。

任何链接也可以。谢谢

require_once '../google_api/Google_Client.php';
require_once '../google_api/contrib/Google_YouTubeService.php';
$OAUTH2_CLIENT_ID = '514750847005.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = '9mvQL0NPv1zEOty0tZw71O4t';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL);
$client->setRedirectUri("{$redirect}");

$youtube = new Google_YoutubeService($client);

if (isset($_GET['code'])) {
            if (strval($_SESSION['state']) !== strval($_GET['state'])) {
                die('The session state did not match.');
            }

            $client->authenticate();
            $_SESSION['token'] = $client->getAccessToken();
            header('Location: ' . $redirect);
        }

        if (isset($_SESSION['token'])) {
            $client->setAccessToken($_SESSION['token']);
        }
        if ($client->getAccessToken()) {
            try {
                $snippet = new Google_VideoSnippet();
                $snippet->setTitle($title);
                $snippet->setDescription($desc);
                $snippet->setTags($tags);
                $snippet->setCategoryId(22);

                $status = new Google_VideoStatus();
                $status->privacyStatus = "public";

                $today = date("Y-m-d\TH:i:s");
                $recordingDetails = new Google_VideoRecordingDetails();
                $recordingDetails->setRecordingDate($today);

                $video = new Google_Video();
                $video->setSnippet($snippet);
                $video->setStatus($status);
                $video->setRecordingDetails($recordingDetails);

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

                // Create a MediaFileUpload with resumable uploads
                $media = new Google_MediaFileUpload('video/*', null, true, $chunkSizeBytes);
                $media->setFileSize(filesize($vd_file));

                // Create a video insert request
                $insertResponse = $youtube->videos->insert("status,snippet", $video, array('mediaUpload' => $media));

                $uploadStatus = false;

                // Read file and upload chunk by chunk
                $handle = fopen($vd_file, "rb");
                while (!$uploadStatus && !feof($handle)) {
                    $chunk = fread($handle, $chunkSizeBytes);
                    $uploadStatus = $media->nextChunk($insertResponse, $chunk);
                }

                fclose($handle);
                msg_add('suces', 'message', 'Youtube video upload was successful');
            } catch (Google_ServiceException $e) {
                $message = sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
                msg_add('err', 'message', $message);
            } catch (Google_Exception $e) {
                $message = sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
                msg_add('err', 'message', $message);
            }
4

3 回答 3

0

我遇到了类似的情况,我试图在插入时设置位置,但它不起作用。我让我的代码通过在参数中包含“recordingDetails”来正确设置位置,该part参数是一个逗号分隔的列表(文档)。我正在使用 C#,所以对我来说它看起来像这样:

var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status,recordingDetails", fileStream, "video/*");

从您的示例来看,在 php 中这将是

$insertResponse = $youtube->videos->insert("status,snippet,recordingDetails", $video, array('mediaUpload' => $media));
于 2014-09-18T21:32:18.373 回答
0

您所做的只是一个视频->插入呼叫。您正在编辑片段、状态和记录详细信息。您可以通过将其他字段添加到VIDEOS资源来执行更多操作。

在插入之前填写VIDEOS资源的任何字段,应该这样做。

于 2013-10-02T16:31:12.057 回答
0

允许评论应该是“批准”而不是“全部”

这似乎是一个 youtube 错误:

https://code.google.com/p/gdata-issues/issues/detail?id=7664

于 2016-03-04T16:05:04.333 回答