この記事を書いた動機
特に、2015年以降の買収済みのyoutube(というかgoogle)はどうも営利企業としての側面をあらわにしているのか、広告ブロックされる理由を考えて対応するより先に、広告ブロックを悪者にして追い出したり、利用者に対して一方的な方針転換やサービスの実装をしているようである。
その波が、動画をダウンロードするこのコマンドにも来ているようで、なんか年齢制限でダウンロードできないというあまりに不便な挙動をしていることに気づいた。何とかしようと考えるわけだが、なかなか面倒だったし、思い出せる自信もないので、結構いい加減だけど、記録を残すことにした。
環境について
私は普段、windows を使っておらず、linux環境しか使ってないです。なのでこの記事は、windows ユーザにとってはずれているところがあったりと、微妙なところがあるかもしれません。
制限を突破する方法
- yt-dlpコマンドのプラグインを使う。
- yt-dlpコマンドの以下のオプションを使う。
※ これは自分のGoogleアカウントでyoutubeにログインすると、そのログイン状態を保持するための情報がcookieに入っていることを利用していると思われる。そのログイン情報が悪用される可能性があるかは未知数。。。なので、なるべくサブのアカウントなどの利用を推奨する。yt-dlp [urlとか] --cookies [クッキーのテキストファイルのパス]
今とにかく動いた方法
なんか最初にやったことから書いたらグダグダになったので、いったんとにかく動作確認ができた方法から記録してみる。
-
youtubeからcookieを抽出
youtubeにログインし、cookieを抽出可能な拡張機能をインストールして、抽出する。私は、Get cookies.txt LOCALLYという拡張機能を利用した。どうも、yt-dlpは、Netscapeとかいう古のブラウザのcookie形式でしか受け付けないらしく、javascriptでdocument.cookie
とかやって得られる情報そのままではだめらしいので、楽な方に流れたという感じ。 -
yt-dlpでcookieのテキストファイルを指定し、実際に動画をダウンロード
yt-dlp [urlとか] --cookies [クッキーのテキストファイルのパス]
とにかく、これで一時的に動かすことができたが、セッションが有効期限切れになったりすると、たぶんまた作業をやり直す必要があり面倒である。
cookieのテキストファイルって何?
Netscape形式の場合、以下に示すようなものであるよう。
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file! Do not edit.
.youtube.com TRUE / TRUE 1741146319 LOGIN_INFO 何かのデータ
.youtube.com TRUE / FALSE 1763243188 SID 何かのデータ
.youtube.com TRUE / TRUE 1763243188 __Secure-1PSID 何かのデータ
.youtube.com TRUE / TRUE 1763243188 __Secure-3PSID 何かのデータ
.youtube.com TRUE / FALSE 1763243188 HSID 何かのデータ
.youtube.com TRUE / TRUE 1763243188 SSID 何かのデータ
.youtube.com TRUE / FALSE 1763243188 APISID 何かのデータ
.youtube.com TRUE / TRUE 1763243188 SAPISID 何かのデータ
.youtube.com TRUE / TRUE 1763243188 __Secure-1PAPISID 何かのデータ
.youtube.com TRUE / TRUE 1763243188 __Secure-3PAPISID 何かのデータ
.youtube.com TRUE / TRUE 1764701056 PREF 何かのデータ
.youtube.com TRUE / TRUE 1761676825 __Secure-1PSIDTS 何かのデータ
.youtube.com TRUE / TRUE 1761676825 __Secure-3PSIDTS 何かのデータ
.youtube.com TRUE / FALSE 1761677056 SIDCC 何かのデータ
.youtube.com TRUE / TRUE 1761677056 __Secure-1PSIDCC 何かのデータ
.youtube.com TRUE / TRUE 1761677056 __Secure-3PSIDCC 何かのデータ
.youtube.com TRUE / TRUE 1745681252 VISITOR_PRIVACY_METADATA 何かのデータ
.youtube.com TRUE / TRUE 1745681252 VISITOR_INFO1_LIVE 何かのデータ
.youtube.com TRUE / TRUE 0 YSC 何かのデータ
以降の内容
これ以降の内容は、単に記録として適当にグダグダ書いてあるので、詳しいことを知りたい人以外には、あまり有用ではない可能性があるということだけはここに書いておく。
最初にやったこと
最初は、自分のアカウントを使ってどうこうしないといけないということ自体が、そもそも嫌だったので、とりあえずプラグインに頼ってみようと考えた。
しかし、今のところプラグイン自体は動きはするものの、インストールしずらいわ、目的の機能は動かないわで、なんかうまくいかなかった。私はgithubにある、pukkandan/yt-dlp-YTAgeGateBypass というやつを試していた。
具体的には、まずどうやってプラグインをインストールすればいいのかというところでいったんつまずいた。でも、これに関しては以下のように、yt-dlpの方に説明があったので、ややこしかったが何とかなった。以下の引用には私が注目したところだけで、すべては載せていないので、必要に応じて各自直接サイトに出向いて確認してほしいと思う。
引用した部分へのURL
- https://github.com/yt-dlp/yt-dlp?tab=readme-ov-file#plugins
- https://github.com/yt-dlp/yt-dlp?tab=readme-ov-file#installing-plugins
PLUGINS
…
yt_dlp_plugins/ extractor/ myplugin.py postprocessor/ myplugin.py
yt-dlp looks for these yt_dlp_plugins namespace folders in > many locations (see below) and loads in plugins from all of > them. Set the environment variable YTDLP_NO_PLUGINS to > something nonempty to disable loading plugins entirely.
…Installing Plugins
Plugins can be installed using various methods and > locations.
Configuration directories: Plugin packages (containing a > yt_dlp_plugins namespace folder) can be dropped into the > following standard configuration locations:
- User Plugins
... ~/yt-dlp-plugins/<package name>/yt_dlp_plugins/ ...
上記に引用した情報を参考にして、以下のようにyt-dlpのプラグインをインストールした。
具体的な方法
- プラグインをインストールするフォルダを用意し、そこへ移動する。
mkdir -p ~/yt-dlp-plugins/
cd ~/yt-dlp-plugins/
- プラグインのリポジトリをクローンする。
git clone https://github.com/pukkandan/yt-dlp-YTAgeGateBypass
- プラグインの説明にある"–extractor-args youtube:no-video-proxy"を引数として、yt-dlpに渡し、動作確認する。
yt-dlp [urlとか] --extractor-args youtube:no-video-proxy
ちなみに、最終的にこの方法でプラグインが実際に動いていることは確認できたが、どうもプラグインが必要とするサーバがここ最近一週間くらい動いていない状態になってる。プラグインやyoutubeの動向に雲行きがかかっている模様で、本来の機能である年齢制限回避は動かなかった。githubのissueに、同じことについて書いてある。
個人的には、issueにある以下のコメントが気になった。Google の Don’t be evil の方針崩れ、金や自分たちのエゴのためなら、Be evil みたいな雰囲気を感じられることについて、共通する点が含まれていて、普通に共感できるなと思った。
Vangelis66 commented last week The upstream service (age-verified YT account proxy) this AGB plugin uses under the hood has been DOWN for some weeks now; it’s uncertain when/if it’ll be back on and at what state; latest words from the maintainers:
zerodytrash/Simple-YouTube-Age-Restriction-Bypass#262 (comment)
(With most-evil Google 😡 breaking the known A-G-B hacks recently, why does this “coincidence” wake up the conspiracy-theorist inside me? 😉 )
補足情報
Simple-YouTube-Age-Restriction-Bypassというプラグインもあるらしいが、とにかく動いたし、疲れたのでまだ試していない。
pukkandan/yt-dlp-YTAgeGateBypass の方にもっと簡単そうなpipを使ったインストール方法があるじゃんと思うかもしれない。しかし、私の環境では、Anacondaとかいろいろ入っててやりずらかったので、そちらの方法は最初は使おうとしていたのだけど、動いているのかわからず諦めた。
思うのだけど、yt-dlpコマンドのプラグイン導入はなんか本当に動いているのか、間接的にしか確認できなくて辛い。プラグイン自体が読み込まれ動いているのかという確認は、以下のようにpythonがモジュール等に対して作るキャッシュファイル(__pycache__)がyt-dlpコマンド実行時に作られるかどうかで判定した。
userName on ~/yt-dlp-plugins/yt-dlp-YTAgeGateBypass/yt_dlp_plugins/extractor master
# ls
drwxr-xr-x⠀userName⠀4096⠀Oct 29 03:31:04⠀⠀__pycache__/
-rw-r--r--⠀userName⠀3482⠀Oct 29 03:23:07⠀⠀yt_agb.py
次にやったこと
次にやったことは、ブラウザのcookie情報を素直に利用して、年齢制限付きの動画をダウンロードしようと試みた。まずは、yt-dlpの以下のオプションを利用して、やってみた。ちなみに、すでにchrome側ではyoutubeにログインした状態かつ、一つのプロファイルしかない状態である。
# yt-dlp [urlとか] --cookies-from-browser BROWSER[+KEYRING][:PROFILE][::CONTAINER]
yt-dlp [urlとか] --cookies-from-browser chrome
# 以下は実行結果
userName on ~/work/temp
# yt-dlp https://www.youtube.com/watch?v=[videoID] --cookies-from-browser chrome
Extracting cookies from chrome
[Cookies] Loading cookie 0/ 65WARNING: cannot decrypt v11 cookies: no key found
Extracted 0 cookies from chrome (65 could not be decrypted)
[youtube+AGB] Extracting URL: https://www.youtube.com/watch?v=[videoID]
[youtube+AGB] [videoID]: Downloading webpage
[youtube+AGB] [videoID]: Downloading ios player API JSON
[youtube+AGB] [videoID]: Downloading tv embedded player API JSON
[youtube+AGB] [videoID]: Downloading web embedded client config
[youtube+AGB] [videoID]: Downloading player 576f07ca
[youtube+AGB] [videoID]: Downloading web embedded player API JSON
[youtube+AGB] [videoID]: Downloading Zerody API JSON
WARNING: [youtube+AGB] Unable to download JSON metadata: HTTP Error 502: Bad Gateway
ERROR: [youtube+AGB] [videoID]: Sign in to confirm your age. This video may be inappropriate for some users.
# なんかcookie読み込んでるぽいけど、復号に失敗してるっぽく、なんでダメなのかなぁ。。。探せば何とかなるかもだけど、もう疲れたや
こんな感じでうまくいかず、さらにめんどくさく感じて、最初のクッキーのテキストファイルを直接渡す方法を試すに至り、とにかく年齢制限付きの動画をダウンロードできるようになった。
正直なことを言えば、アカウントなしで何とかなる方法を模索することには興味がある。
追記あり
一部投げやりな部分について書き足した。
参考にしたサイト
- Plugins · yt-dlp/yt-dlp Wiki · GitHub
https://github.com/yt-dlp/yt-dlp/wiki/Plugins (2024年10月29日) - GitHub - yt-dlp/yt-dlp: A feature-rich command-line audio/video downloader
https://github.com/yt-dlp/yt-dlp?tab=readme-ov-file#plugins (2024年10月29日) - GitHub - yt-dlp/yt-dlp: A feature-rich command-line audio/video downloader
https://github.com/yt-dlp/yt-dlp?tab=readme-ov-file#installing-plugins (2024年10月29日) - GitHub - pukkandan/yt-dlp-YTAgeGateBypass: A yt-dlp extractor plugin to bypass YouTube age-gate
https://github.com/pukkandan/yt-dlp-YTAgeGateBypass (2024年10月29日) - Sign in to confirm your age. This video may be inappropriate for some users. · Issue #4 · pukkandan/yt-dlp-YTAgeGateBypass · GitHub
https://github.com/pukkandan/yt-dlp-YTAgeGateBypass/issues/4 (2024年10月29日) - GitHub - zerodytrash/Simple-YouTube-Age-Restriction-Bypass: A simple browser extension to bypass YouTube’s age verification, disable content warnings and watch age restricted videos without having to sign in!
https://github.com/zerodytrash/Simple-YouTube-Age-Restriction-Bypass (2024年10月29日) - Get cookies.txt LOCALLY - Chrome Web Store
https://chromewebstore.google.com/detail/get-cookiestxt-locally/cclelndahbckbenkjhflpdbgdldlbecc?hl=en (2024年10月29日)