LoginSignup
47
77

More than 3 years have passed since last update.

pytube3を使って、Youtubeの動画をダウンロードする方法

Last updated at Posted at 2020-07-07

環境

OS: mac os x 10.15.5
python: 3.8.0
pythonバージョン管理: pyenv

pytube3とは

pytubeはyoutubeの動画をダウンロードするpythonライブラリです。
googleさんはyoutube Data APIを公開しておりますので、ビジネスなどでyoutubeの動画のダウンロード機能を使う場合は、pytubeは使わないことをおすすめします。

pytubeはあくまで、私的で非営利な活動で、過度なリクエストを送らない程度にお使いください。

pytube3をインストール

python3 をお使いの場合は、pytube3をインストールしてください。

pip install pytube3

python2 をお使いの場合は、pytubeをインストールしてください。


pip install pytube

動画リンクを挿入したcsvファイルを用意

youtube_links.csv
https://www.youtube.com/watch?v=-VoogELsBms
https://www.youtube.com/watch?v=cN1qnAx8tqg

インストールを行うソースコード

main.py
import csv
from pytube import YouTube


def download(url, index):
    yt = YouTube(url)
    print(str(index) + "番目の動画をダウンロードします。")
    yt.streams.filter(progressive=True, file_extension='mp4').order_by(
        'resolution').desc().first().download('./news', str(index))


with open('youtube_links.csv') as f:
    reader = csv.reader(f)
    for index, row in enumerate(reader):
        url = row[0]
        print("start download")
        download(url, index)
        print("completed download")

実行

$ python main.py

githubにソースコードを載せておきます。
https://github.com/SeiyaTakahashi/pytube3-project

47
77
1

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
47
77