LoginSignup
158
201

More than 3 years have passed since last update.

【Python】youtubeの動画をコピペ5行でダウンロードする方法(違法性なし)

Last updated at Posted at 2021-04-04

はじめに

今ではテレビを超えたなんて言われているyoutubeですが、ダウンロードして後でゆっくり見たいなーという時があると思います。
そんな時に簡単にダウンロードが行えるおすすめな方法をご紹介いたします。

ちなみに本記事で紹介するライブラリは違法性はないとのことですが、youtubeのダウンロードは自己責任でお願いいたします。

※当然「複製して配布」はNGです!

ライブラリ「youtube_dl」のインストール

まずはライブラリをインストールしておきます。

pip install youtube_dl

youtubeのダウンロード

拡張子「.py」のファイルに以下をコピペして実行するだけです。
5行目のhttps://www.youtube.com/watch?v=~をダウンロードしたいURLに変更してください。
※以下のコードのままだと首相官邸公式チャンネルから「安倍総理からのメッセージ ―令和2年9月16日」がダウンロード出来ます笑

from __future__ import unicode_literals
import youtube_dl
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=CFLOiR2EbKM'])

動画ファイルは.pyのコードファイルが格納されているフォルダに出力されます。

youtubeのダウンロード(コマンドライン)

コマンドラインからも実行出来ます。
下記の1行でダウンロード可能です。

youtube-dl [OPTIONS] URL [URL...]

オプション

-h, --help                           Print this help text and exit
--version                            Print program version and exit
-U, --update                         Update this program to latest version.
                                     Make sure that you have sufficient
                                     permissions (run with sudo if needed)
-i, --ignore-errors                  Continue on download errors, for
                                     example to skip unavailable videos in a
                                     playlist
--abort-on-error                     Abort downloading of further videos (in
                                     the playlist or the command line) if an
                                     error occurs
--dump-user-agent                    Display the current browser
                                     identification
--list-extractors                    List all supported extractors
--extractor-descriptions             Output descriptions of all supported
                                     extractors
--force-generic-extractor            Force extraction to use the generic
                                     extractor
--default-search PREFIX              Use this prefix for unqualified URLs.
                                     For example "gvsearch2:" downloads two
                                     videos from google videos for youtube-
                                     dl "large apple". Use the value "auto"
                                     to let youtube-dl guess ("auto_warning"
                                     to emit a warning when guessing).
                                     "error" just throws an error. The
                                     default value "fixup_error" repairs
                                     broken URLs, but emits an error if this
                                     is not possible instead of searching.
--ignore-config                      Do not read configuration files. When
                                     given in the global configuration file
                                     /etc/youtube-dl.conf: Do not read the
                                     user configuration in
                                     ~/.config/youtube-dl/config
                                     (%APPDATA%/youtube-dl/config.txt on
                                     Windows)
--config-location PATH               Location of the configuration file;
                                     either the path to the config or its
                                     containing directory.
--flat-playlist                      Do not extract the videos of a
                                     playlist, only list them.
--mark-watched                       Mark videos watched (YouTube only)
--no-mark-watched                    Do not mark videos watched (YouTube
                                     only)
--no-color                           Do not emit color codes in output

ネットワークオプション

--proxy URL                          Use the specified HTTP/HTTPS/SOCKS
                                     proxy. To enable SOCKS proxy, specify a
                                     proper scheme. For example
                                     socks5://127.0.0.1:1080/. Pass in an
                                     empty string (--proxy "") for direct
                                     connection
--socket-timeout SECONDS             Time to wait before giving up, in
                                     seconds
--source-address IP                  Client-side IP address to bind to
-4, --force-ipv4                     Make all connections via IPv4
-6, --force-ipv6                     Make all connections via IPv6

動画がダウンロード出来なくなったら

youtube側の仕様変更でダウンロードが出来なくなる可能性があります。
そんな時はyoutube_dlが更新されるのを待ちましょう。
更新されたら下記のコマンドでライブラリを最新化します。

pip install -U youtube_dl

さいごに

ライブラリ「youtube_dl」はPython初心者におすすめのライブラリです!

YoutubeはWEBスクレイピングを規約違反としているのでAPI経由で情報を取得する必要があります。
APIキー取得の記事も書きました。

今後自動動画取得ツールを作成予定です。

以上です。快適なYoutubeライフを!

参考リンク

158
201
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
158
201