16
9

More than 3 years have passed since last update.

Youtube Data APIでNode.jsから動画をアップロードする

Posted at

Youtubeへの動画アップロードの自動化を行いたいなと思って調べてましたが、ある程度まとまってきたので記事にしてみます。

調べてもあまり実装サンプルが見当たらないですね、Youtubeに動画をアップロードするサンプルは調べてすぐ出てくるものが古いものが多くて調べるのに苦労した感あります苦笑

未来の自分やどなたかの参考になれば幸いです。

Youtube Data API

Youtubeは

  • Player API
  • Data API
  • Analytics API
  • Live Streaming API

などに大きく分けられるみたいです。

参考: YouTube Developer Documentation

その中でもData APIが動画アップロードに関連します。

こちらがYoutube Data API(v3)のページです。

環境

  • Node.js v16.0.0

出たばかりですね〜

https://twitter.com/bethgriggs_/status/1384540641007448065?s=21

実装手順

基本的には他のGoogle API系と同じです。

Google DriveのAPIをNode.jsから触るメモ

credentials.jsonを取得

チュートリアルを参考にAPIを有効にし、credentials.jsonを手元に保存しましょう。

https://developers.google.com/youtube/v3/quickstart/nodejs

執筆時点のこちらのチュートリアルだと、client_secret.jsonというファイル名になっていますが、credentials.jsonにリネームします。

google-auth-token-generatorでtoken.jsonを作成

$ npx google-auth-token-generator@0.0.11

Youtubeを選択します。

スクリーンショット 2021-04-21 22.21.51.png

youtube.readlonly以外を選択します。(アップロードするときはreadonlyにチェックを入れるとパーミッションエラーで怒られます。)

スクリーンショット 2021-04-21 22.23.30.png

認証用のURLが発行されるのでブラウザでアクセスしましょう。

スクリーンショット 2021-04-21 22.24.59.png

ブラウザでアカウントなどを選択して進み、許可ボタンを押します。

スクリーンショット 2021-04-21 22.26.14.png

コードが発行されるのでコピーして、ターミナルに戻ってコードを貼り付けます。

スクリーンショット 2021-04-21 22.27.20.png

そうするとtoken.jsonが発行されます。

動画をアップロードするコード

公式のupload.jsサンプルはこちらです。これを参考にサンプルを作成しています。

https://github.com/googleapis/google-api-nodejs-client/blob/master/samples/youtube/upload.js

upload.js
'use strict';

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');

const googleAuth = () => {
    const CREDENTIALS_PATH = 'credentials.json';
    const TOKEN_PATH = 'token.json';  
    const credentials = JSON.parse(fs.readFileSync(CREDENTIALS_PATH, 'utf8'));
    const token = JSON.parse(fs.readFileSync(TOKEN_PATH, 'utf8'));

    const {client_secret, client_id, redirect_uris} = credentials.installed;
    const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
    oAuth2Client.setCredentials(token);

    return oAuth2Client;
};

(async () => {
    const auth = googleAuth();
    const youtube = google.youtube({version: 'v3', auth});
    console.log(youtube.channels)
    try {
        const fileName = 'tanaka.mov'; //アップロードする動画ファイル名

        const fileSize = fs.statSync(fileName).size;
        const res = await youtube.videos.insert(
            {
              part: 'id,snippet,status',
              notifySubscribers: false,
              requestBody: {
                snippet: {
                  title: 'Node.js YouTube Upload Test',
                  description: 'Testing YouTube upload via Google APIs Node.js Client',
                },
                status: {
                  privacyStatus: 'private',
                },
              },
              media: {
                body: fs.createReadStream(fileName),
              },
            },
            {
              // Use the `onUploadProgress` event from Axios to track the
              // number of bytes uploaded to this point.
              onUploadProgress: evt => {
                const progress = (evt.bytesRead / fileSize) * 100;
                readline.clearLine(process.stdout, 0);
                readline.cursorTo(process.stdout, 0, null);
                process.stdout.write(`${Math.round(progress)}% complete`);
              },
            }
          );
          console.log('\n\n');
          console.log(res.data);
          return res.data;

    } catch (error) {
        console.log('The API returned an error: ' + error);
    }
})();

実行

$ node upload.js

無事にアップロードできた

タイトルだったりは適宜コードを変更すればいけそうですね。

所感

チュートリアルのサンプルが、Google Driveなどのサンプルより絶妙に古いのと、APIを有効にした際のウィザードが少し他のAPIとフローが違っていて、YoutubeのAPIの独特さを感じました。

元々別だったけど統合していこう、、、みたいな流れなのかマージ途中な雰囲気を感じるという苦笑

16
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
9