如何使用 youtube API 和视频 URL 上传视频

问题描述 投票:0回答:1

是否可以将视频 URL 而不是物理视频文件上传到 YouTube(通过 YouTube 的插入 API)?如果是的话,怎么办?

或者是否需要物理视频文件。有什么解决办法吗?谢谢

youtube youtube-api
1个回答
0
投票

我假设您已经安装了 google youtube 服务(如果您使用 PHP 开发,则使用 Composer)

composer require google/apiclient:^2.0
,您可以使用以下
file_get_contents
从 URL 上传,下面是代码的粗略草图

    $client = new \Google_Client();
    $client->setApplicationName('APP_NAME');
    $client->setClientId('ClientID'); 
    $client->setClientSecret('SecretKEY'); 


    $videoPath ='https://justinstolpe.com/sandbox/bubblesbath.mp4'; 

      $arr_data = array(
        'title' => 'Test2 title',
        'summary' => 'Test2 description',
        'video_path' => $videoPath,
    );


    $client->setAccessToken('$yourToken');
    // Check to ensure that the access token was successfully acquired.
    if ($client->getAccessToken()) {
      try{

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

        $youtube = new Google_Service_YouTube($client);
        $snippet = new Google_Service_YouTube_VideoSnippet();
        $snippet->setTitle($arr_data['title']);
        $snippet->setDescription($arr_data['summary']);
        $snippet->setTags(array("tag1", "tag2"));
  
        // Numeric video category. See
        // https://developers.google.com/youtube/v3/docs/videoCategories/list 
        $snippet->setCategoryId("22");
  
        // 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);
        
        $response = $youtube->videos->insert(
            'snippet,status',
            $video,
            array(
                'data' => file_get_contents($arr_data['video_path']),
                'mimeType' => 'video/*',
                'uploadType' => 'multipart'
            )
        );
        return "Video uploaded successfully. Video ID is ". $response->id;
  
       
  
      } catch (Google_Service_Exception $e) {
       return($e->getMessage());
      } catch (Google_Exception $e) {
       return($e->getMessage());
      }
  
    } else {
      // If the user hasn't authorized the app, initiate the OAuth flow
      return 'Authorization Required. authorize access ,before proceeding';

    }
© www.soinside.com 2019 - 2024. All rights reserved.