0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

youtube に API 経由で動画をアップロードする

Last updated at Posted at 2023-03-20

環境

  • Ubuntu 20.04 LTS
  • Python 3.8

手順

  • Youtube API を有効化する
  • Python スクリプトを実行する

Youtube API を有効化する

下記動画を参考に、 API をセットアップし、認証情報をダウンロードする

How to Upload Videos with the YouTube API (using Python) - YouTube

Python スクリプトを実行する

単体のファイルをアップロードする場合は、以下のような形で。

import os.path

from simple_youtube_api.Channel import Channel
from simple_youtube_api.LocalVideo import LocalVideo

# loggin into the channel
channel = Channel()
channel.login("client_secret.json", "credentials.storage")

# setting up the video that is going to be uploaded
file_path = "/home/taro/.Videos/video.mp4"
video = LocalVideo(file_path=file_path)

# setting snippet
video.set_title(os.path.basename(file_path))

# setting status
video.set_privacy_status("private")
video.set_public_stats_viewable(True)

# uploading video and printing the results
video = channel.upload_video(video)
print(video.id)
print(video)

# liking video

ディレクトリ以下のファイルをすべてアップロードする場合は、以下のような形で。

import os.path

from simple_youtube_api.Channel import Channel
from simple_youtube_api.LocalVideo import LocalVideo


def upload_local_file(file_path):
    video = LocalVideo(file_path=file_path)
    # setting snippet
    video.set_title(os.path.basename(file_path))
    # setting status
    video.set_privacy_status("private")
    video.set_public_stats_viewable(True)
    # uploading video and printing the results
    video = channel.upload_video(video)
    print(video.id)
    print(video)
    # liking video


# loggin into the channel
channel = Channel()
channel.login("client_secret.json", "credentials.storage")

# setting up the video that is going to be uploaded
dir_path = "/home/taro/.Videos/PS4/SHARE/Video Clips/Bloodborne®"

for file_path in os.scandir(dir_path):
    upload_local_file(file_path)

OAuth 認証が行われるため、アクセス許可のリクエストを承認する。

OAuth 認証を行うと「credentials.storage」にキャッシュファイルが作成され、以降はその情報が利用されるようになる。

なお、環境は「testing」のまま利用している。用途において必要十分であるため。

結果

image.png

Youtube Studio で、動画がアップロードされていることを確認できる。

経緯

Ubuntu 端末を初期化しようと思い、端末に保管していた PS4 のビデオクリップを Youtube に移動しようとした。

管理画面から一度にアップロードできる動画数は 15 であり、一括でアップロードするには API を利用する必要があったため、 API で動画をアップロードできる方法を探した。

余談

よくわからなかったのだが、 Bulk Upload する方法は API 以外にももう一つあるらしい。

追記

複数ファイルのアップロードを行っていると、 Quota 制限に引っかかった。

YouTube Data APIとクォータ(割り当て、quotas)について(2022年2月) | masaki-blog

特にアップロードは Quota を大きく消費するようで、 Quota の上限を引き上げる申請を行う必要がある。

結局、本来の目的である動画ファイルのバックアップには Cloud Strage を利用し、この方法は利用しないことにした。

Ref

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?