Upload videos to Youtube on GAS(Google Apps Script)

Hello. I’m Iori-Nitta. I will describe how to use GAS(Google Apps Script) to upload video from Google Drive to Youtube. Thank you in advance.

Using GAS, You can automate various tasks and make then more convenient. It would be a great honor if someone who wants to upload a video from Google Drive to YouTube at some point could read this article. and, I’m planning to write a program soon that uploads video files from GAS to Google Drive. I believe that, when combined with that, we can create a simple video posting site.

※It seems that, from a YouTube API perspective, uploading videos is limited to six per day. Please be mindful of this restriction.

Create GAS(Google Apps Script)

You can create it by selecting “New” -> “More” -> “Google Apps Script” in Google Drive, as shown in the image below.

Add Youtube API Library

Please click on “Services” on the left-hand side, and then click “Add a service.”

After clicking, a screen like the one below will appear. At that time, please select “Youtube Data API v3” and click “Add.” You can find the documentation for the Youtube Data API here.

Write Code

function main() {
  // video id
  const video_id = "<Google Deiveの動画ファイルIDを記載 → 動画ファイルIDの確認方法はページ下に書いておきます。>";

  // get video from id
  const video = DriveApp.getFileById(video_id);
  // video config
  const video_config = {
    snippet: {
      title: "GAS(Google Apps Script)を使用してYoutubeに動画をあげる方法",
      description: "ここに動画の概要欄を記入nnこんにちは。n「新田いおり」といいます。今後もよろしくお願いします。",
      defaultLanguage: "ja",
      categoryId: 22, // 22 is blog
      tags: ["Blog", "GAS", "Youtube", "Nitta_iori"],
    },
    status: {
      privacyStatus: "unlisted", // public private unlisted
    },
  };

  // upload video
  youtubeUpload(video, video_config)
    .then(id => {
      // get youtube id
      const youtube_id = id;
      // create url from youtube id
      const youtube_url = `https://www.youtube.com/watch?v=${youtube_id}`;
      console.log(youtube_url);
    })
    .catch(error => {
      console.error(error);
    });
}

// upload video function
function youtubeUpload(video, config) {
  return new Promise((resolve, reject) => {
    // get video file
    const blob = video.getBlob();

    // upload youtube
    const youtube_upload = YouTube.Videos.insert(config, "snippet,status", blob);

    // get youtube id
    const youtube_id = youtube_upload.id;
    resolve(youtube_id);
  });
}

List of video categories for reference: https://gist.github.com/dgp/1b24bf2961521bd75d6c

Run

Check function name is「main」, and click「Run」.

※During the initial execution, you may receive an access request like the image below. Please grant permission at your own discretion and responsibility

When you run it, a YouTube URL will be displayed, and clicking on it will take you to the posted content.

※Posting on YouTube may take some time, so it’s advisable to wait a bit, especially for larger video files, before checking.

How to get Video id on Google Drive

When opening a video from Google Drive, you should see a screen like the image below.

Click on the “menu” in the upper right corner of the screen and select “Open in a new window.”


Please use the blue portion of the displayed URL.

Leave a Comment