61
@shinkai_

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

はじめに

今ではテレビを超えたなんて言われている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

さいごに

WEBスクレイピングやyoutubeのAPIと合わせて色々なツールを作ってみようかなと思いました。
Python初心者におすすめのライブラリです!

参考リンク

61
ユーザー登録して、Qiitaをもっと便利に使ってみませんか。
  1. あなたにマッチした記事をお届けします
    ユーザーやタグをフォローすることで、あなたが興味を持つ技術分野の情報をまとめてキャッチアップできます
  2. 便利な情報をあとで効率的に読み返せます
    気に入った記事を「ストック」することで、あとからすぐに検索できます
shinkai_
SEしてます。意識高い系になりたい。
この記事は以下の記事からリンクされています
過去の0件を表示する

コメント

この記事にコメントはありません。
あなたもコメントしてみませんか :)
ユーザー登録
すでにアカウントを持っている方はログイン
記事投稿イベント開催中
新人プログラマ応援 - みんなで新人を育てよう!
~
Java開発者のためのAzure入門
~