回心誌

日々是回心

動画ファイルをYoutubeに自動アップロードするPythonコード

作ったコードの簡単な説明

Python 3系で動作します。
YoutubeAPIYoutube Data API v3)を使用します。

指定のフォルダに置かれている動画(mp4、mkvファイル)を順次アップロードしていきます。
アップロードしたファイルは、アップロード処理完了時に別フォルダに移動させます。
また、指定した再生リストに自動で追加します。

フォルダーパスや説明欄の記載、カテゴリー、公開範囲、再生リストIDを引数で渡します。

背景

なんでこれ作ったの?というと、手動でアップロードするのがちょっと手間だったから。

自分はゲーム動画のストレージとしてYoutubeを使ってます。
毎回同じ再生リスト、同じ公開範囲でアップロードするのに、いちいち選択するのって無駄だな〜と。

あと、APIを使う勉強もかねて。

で、書いてる途中に気付いたんだけど、複数の動画を選択してアップロード画面にドラッグ&ドロップすれば、複数一気にアップロードできるっぽい。公開範囲もあらかじめデフォルトの設定をしておくことで個別の選択は不要みたい。

なんならYoutube公式で複数アップロードする方法について案内がある。最初からこれでいいっすね。。。
アップロード方法の選択 - YouTube ヘルプ

ま、勉強になったからいいけど。

準備

APIを使うための準備がけっこう面倒くさかった。
色々手探りだったし記憶が曖昧なので、合ってるか自信がない。

とりあえずリストアップするとこんな感じ。

  1. アップロード用のYoutubeアカウントを用意する(無料でできる)
  2. APIを使うためのGoogle Cloud Platformアカウントを用意する(これも無料アカウントで大丈夫)
  3. Youtube Data API v3を有効化
  4. 認証情報を作成し、クライアントシークレットのjsonファイルを保管
  5. OAuth同意画面の設定
  6. 追加先の再生リストを用意して、再生リストIDを調べる

それぞれ細かくみていく。

1. アップロード用のYoutubeアカウントを用意する

Youtubeのアップロード用アカウントを用意するだけ。

持ってない人は作れば良い。
ググれば色々出てくる。

YouTube でアカウントを作成する - パソコン - YouTube ヘルプ
YouTubeのアカウント作成方法やメリットについて徹底解説。注意点も紹介 | テックキャンプ ブログ

2. APIを使うためのGoogle Cloud Platformアカウントを用意する

これも元々持ってればそれを使いまわしてもいい。
Googleのアカウントが必要っぽい。
ググると出てくる。

GCP(Google Cloud Platform)のアカウントを作成してみた|ITエンジニアとして経験・学習したこと

3. Youtube Data API v3を有効化

Google Cloud Platformの画面で
画面左のサイドメニューを開く→APIとサービス→ライブラリを開く

f:id:interferobserver:20220115143005p:plain
APIを有効化

続くライブラリの検索画面で「Youtube」などで検索。
f:id:interferobserver:20220115151416p:plain

検索されてくるYoutube Data API v3を選択し、「有効にする」(有効化かも?)のボタンで有効化する。

検索しなくてもこのURLから見れるかも。
https://console.cloud.google.com/apis/library/youtube.googleapis.com


まあこの辺もググると色々出てくるから。
Kids tube

4. 認証情報を作成し、クライアントシークレットのjsonファイルを保管

これも上の記事で紹介されてた。
Kids tube

手順3で有効化したAPIの管理画面に移動する。
(下のURLからもいけるはず)
https://console.cloud.google.com/apis/api/youtube.googleapis.com/overview

メニューから認証情報を開き、認証情報を作成→OAuthクライアントID
f:id:interferobserver:20220115152714p:plain

アプリケーションの種類:デスクトップアプリケーション
名前:(適当に分かりやすい名前で)
f:id:interferobserver:20220115152843p:plain

一覧に認証情報が追加されるので、右側からJSONファイルをダウンロードし、保管する。
f:id:interferobserver:20220115153034j:plain

この認証情報が流出すると悪用される恐れがあるので、気を付けて保管しましょう。
間違ってgithubなどにアップロードしないようにしましょう。

5. OAuth同意画面の設定

ここの設定はググってもあんまり出てこなかった。

まずは認証情報の詳細設定画面に移動。
f:id:interferobserver:20220115153409p:plain

OAuth同意画面→アプリを編集
f:id:interferobserver:20220115153537p:plain

①OAuth同意画面
②スコープ
③テストユーザー
④概要
の順で設定していく。

①OAuth同意画面 については、メールアドレスなど必須事項を適当に埋めて、保存して次へをクリックしてください。
f:id:interferobserver:20220115153953p:plain

②スコープ は、必要な権限を設定する必要があります。
私が作成したコードは再生リストへの追加を行いますが、これを行うにはyoutubepartnerの権限が最低でも必要なので、ここでyoutubepartnerを選択して追加します。
スコープを追加または削除 → フィルタ:youtubepartner と設定 → Youtube Data API v3のyoutubepartnerを選択 → 保存して次へ
f:id:interferobserver:20220115154145p:plain

ここで設定するべきスコープは、どのAPIを使うかで変わってきます。
リファレンスを確認すればわかります。

③テストユーザー では、手順1で用意したアップロード用のYoutubeアカウントのメールアドレスを追加します。

ちなみに、メールアドレスがわからない場合、YoutubeのプロフィールアイコンからGoogleアカウントを管理をクリックして、管理画面を開きます。
f:id:interferobserver:20220115154616p:plain

個人情報 → 連絡先情報 に記載されています。
f:id:interferobserver:20220115154732p:plain

メールアドレスが分かったら、「+ADD USERS」→メールアドレス欄に記入 → 追加 → 保存して次へ
f:id:interferobserver:20220115154822p:plain

④概要画面をざっと確認したら、ダッシュボードに戻ります。

使い方

こんな感じで引数にフォルダパスとか動画の設定とかを入れれます。

python upload_movies.py \
    --folder="C:\Users\username\Videos\Apex Legends" \
    --description="Apex Legends" \
    --category="20" \
    --privacyStatus="unlisted" \
    --playlistId="PLwHRMjJWFKxQIa54HsBZZ4MqskgeQfO70" \
    --uploadedFolder="C:\Users\username\Videos\Apex Legends\Uploaded"

各引数は次の通りになっています。

  • --folder:動画が格納されているフォルダパス。ここにある動画(mp4、mkv)を全部アップロードしようとします。
  • --description:動画の説明欄。
  • --category:動画のカテゴリID。20はゲーム。そのほかはこんな感じ
  • --privacyStatus:公開範囲。unlistedは限定公開。privateが非公開。publicが公開*1
  • --uploadedFolder:アップロードした後の動画ファイルを移動させるようにしてるんだけど、その移動先のフォルダー。

また、初めて動かすときはブラウザーが起動して、(Youtubeに未ログインの場合は)ログインしてAPIを使ってもいいか承認する画面が出るので、「続行」などを押して進んでください。
久しぶりに動かすときはこの認証が期限切れになっているのでエラーになることがありますが、その場合はもう1回動かすと承認画面が出るので、初めての時と同じく「続行」で進んでください。

コード

#!/usr/bin/python

import http.client
import httplib2
import os
import random
import sys
import time
import shutil
import glob

from apiclient.discovery import build
from apiclient.errors import HttpError
from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow


# Explicitly tell the underlying HTTP transport library not to retry, since
# we are handling retry logic ourselves.
# リトライの機構は実装済みなので、ここは明示的に1とする
httplib2.RETRIES = 1

# Maximum number of times to retry before giving up.
# 最大リトライ回数
MAX_RETRIES = 10

# Always retry when these exceptions are raised.
## 以下の例外が発生した場合に常にリトライ
RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, http.client.NotConnected,
  http.client.IncompleteRead, http.client.ImproperConnectionState,
  http.client.CannotSendRequest, http.client.CannotSendHeader,
  http.client.ResponseNotReady, http.client.BadStatusLine)

# Always retry when an apiclient.errors.HttpError with one of these status
# codes is raised.
## 以下のステータスコードが発生した場合にも常にリトライ
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]

# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the Google API Console at
# https://console.developers.google.com/.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
#   https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
#   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
## クライアントシークレットファイル変数は、OAuth 2.0情報ファイル(クライアントID、クライアントシークレットを含む)のファイルパス。
## 以下のウェブサイトでOAuth 2.0クライアントIDとクライアントシークレットを取得できる
##   https://console.developers.google.com/

CLIENT_SECRETS_FILE = "C:\\Users\\username\\Documents\\secrets\\client_secrets.json" # ここはクライアントシークレットファイルのパスを自分で書いて。

# This OAuth 2.0 access scope allows an application to upload files to the
# authenticated user's YouTube channel, but doesn't allow other types of access.
## このOAuth2.0アクセススコープで許可されたユーザーのYoutubeチャンネルにファイルをアップロードできるが
## それ以外のタイプのアクセスは許可されない。
#YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtube.upload"
## 再生リストに動画を追加するために、より強力な権限を使用する
YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtubepartner"

YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

# This variable defines a message to display if the CLIENT_SECRETS_FILE is
# missing.
# 以下の変数はクライアントシークレットファイルが見つからなかった場合に表示するメッセージを定義するものです。
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this code run you will need to populate the client_secrets.json file
found at:

   %s

with information from the API Console
https://console.developers.google.com/

For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                   CLIENT_SECRETS_FILE))

VALID_PRIVACY_STATUSES = ("public", "private", "unlisted")


def get_authenticated_service(args):
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    scope=YOUTUBE_UPLOAD_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

  storage = Storage("%s-oauth2.json" % sys.argv[0])
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage, args)

  return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))

def upload_movies(youtube, options):
  mp4files = glob.glob(os.path.join(options.folder, "*.mp4"))
  mkvfiles = glob.glob(os.path.join(options.folder, "*.mkv"))
  files = mp4files + mkvfiles
  if len(files) == 0:
    print("No file exists in folder '%s'" % options.folder)
  else:
    print("%d files exists in %s" % (len(files), options.folder))
  for file in files:
    initialize_upload(youtube, options, file)

def initialize_upload(youtube, options, file):
  tags = None
  if options.keywords:
    tags = options.keywords.split(",")

  ## ファイル名(拡張子なし)をタイトルにする
  basename = os.path.basename(file)
  title = os.path.splitext(basename)[0]

  body=dict(
    snippet=dict(
      title=title,
      description=options.description,
      tags=tags,
      categoryId=options.category
    ),
    status=dict(
      privacyStatus=options.privacyStatus
    )
  )

  # Call the API's videos.insert method to create and upload the video.
  ## APIの videos.insert を呼び出し、動画をアップロードし作成する。
  insert_request = youtube.videos().insert(
    part=",".join(body.keys()),
    body=body,
    # The chunksize parameter specifies the size of each chunk of data, in
    # bytes, that will be uploaded at a time. Set a higher value for
    # reliable connections as fewer chunks lead to faster uploads. Set a lower
    # value for better recovery on less reliable connections.
    ## chunksizeは一度にアップロードするデータの各チャンクのサイズをバイトで特定するパラメータ。
    ## 接続が安定している場合は値を大きくすることで、チャンクの数を少なくし、早くアップロードすることができる。
    ## 接続が安定しない場合には小さくすることで復帰しやすくすることができる。
    #
    # Setting "chunksize" equal to -1 in the code below means that the entire
    # file will be uploaded in a single HTTP request. (If the upload fails,
    # it will still be retried where it left off.) This is usually a best
    # practice, but if you're using Python older than 2.6 or if you're
    # running on App Engine, you should set the chunksize to something like
    # 1024 * 1024 (1 megabyte).
    ## "chunksize"を-1に設定した場合、単一のHTTPリクエストでファイルをアップロードする。
    ## (アップロードが失敗した場合、中断したところから再試行される。)
    ## これは通常はベストプラクティスとなるが、Python 2.6未満を使用している場合、
    ## またはApp Engine上で動作させている場合、chunksizeを1024 * 1024(1メガバイト)などに設定とよい。
    media_body=MediaFileUpload(file, chunksize=-1, resumable=True)
  )

  print("Uploading file '%s'..." % file)
  video_id = resumable_upload(insert_request)
  ## オブジェクトが残っているとプロセスがつかんでファイルを移動できなくなるらしいので、
  ## delで消す。
  del insert_request

  move_to_uploaded_folder(options, file)
  
  insert_to_playlist(youtube, video_id, options.playlistId)

## 動画ファイルをアップロード済みフォルダに移動させる
def move_to_uploaded_folder(options, file):
  path_from = file
  path_to = os.path.join(options.uploadedFolder, os.path.basename(path_from))
  shutil.copy2(path_from, path_to)
  try:
    os.remove(path_from)
  except Exception as e:
    print("Error: %s" % e)

# This method implements an exponential backoff strategy to resume a
# failed upload.
## このメソッドは失敗したアップロードを再開するための指数的バックオフ戦略を実装するものである。
def resumable_upload(insert_request):
  response = None
  error = None
  retry = 0
  video_id = None

  while response is None:
    try:
      status, response = insert_request.next_chunk()
      if response is not None:
        if 'id' in response:
          print("Video id '%s' was successfully uploaded." % response['id'])
          video_id = response['id']
        else:
          exit("The upload failed with an unexpected response: %s" % response)
    except HttpError as e:
      if e.resp.status in RETRIABLE_STATUS_CODES:
        error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status,
                                                             e.content)
      else:
        raise
    except RETRIABLE_EXCEPTIONS as e:
      error = "A retriable error occurred: %s" % e

    if error is not None:
      print(error)
      retry += 1
      if retry > MAX_RETRIES:
        exit("No longer attempting to retry.")

      max_sleep = 2 ** retry
      sleep_seconds = random.random() * max_sleep
      print("Sleeping %f seconds and then retrying..." % sleep_seconds)
      time.sleep(sleep_seconds)
    
  ## ビデオIDを返すようにコード追加。
  return video_id

## 指定した再生リストにアップロードした動画を追加する
def insert_to_playlist(youtube, video_id, playlist_id):
  response = None
  error = None

  print("Additing the video \'%s\' to playlist \'%s\'..." % (video_id, playlist_id))
  response = youtube.playlistItems().insert(
      part="snippet",
      body=dict(
        snippet=dict(
          playlistId=playlist_id,
          resourceId=dict(
            kind="youtube#video",
            videoId=video_id
          )
        )
      )
    ).execute()

  if response is not None:
    if 'id' in response:
      print("Video id '%s' was successfully added." % response['id'])
    else:
      exit("The inserting failed with an unexpected response: %s" % response)

if __name__ == '__main__':
  argparser.add_argument("--folder", required=True, help="Folder of video file to upload")
  argparser.add_argument("--title", help="Video title", default=None)
  argparser.add_argument("--description", help="Video description",
    default="Test Description")
  argparser.add_argument("--category", default="22",
    help="Numeric video category. " +
      "See https://developers.google.com/youtube/v3/docs/videoCategories/list")
  argparser.add_argument("--keywords", help="Video keywords, comma separated",
    default="")
  argparser.add_argument("--privacyStatus", choices=VALID_PRIVACY_STATUSES,
    default=VALID_PRIVACY_STATUSES[0], help="Video privacy status.")
  ## 追加先の再生リスト
  argparser.add_argument("--playlistId", required=True,
    help="Playlist ID to add this video.")
  ## アップロード後の移動先フォルダ
  argparser.add_argument("--uploadedFolder", required=True,
    help="Folder path for uploaded folder.")
  args = argparser.parse_args()

  if not os.path.exists(args.folder):
    exit("Please specify a valid folder using the --folder= parameter.")
  

  youtube = get_authenticated_service(args)
  try:
    upload_movies(youtube, args)
  except HttpError as e:
    print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))

参考サイトなど

参考にしたサイトは以下になります。
API Reference  |  YouTube Data API  |  Google Developers
pythonでYouTubeアップロード(YouTubeAPI)


また、Googleのサイトポリシーに従い、以下の事項を記載しておきます。

Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 4.0 Attribution License.

(拙訳:このページの一部は、Googleによって作成および共有され、 Creative Commons 4.0 AttributionLicenseに記載されている条件に従って使用される作成物を改変して作られています。)