youtube-dl 如何下载需要用户认证和 2FA 的视频?

Dav*_*ish 18 video google-chrome youtube cookies youtube-dl

当我尝试下载我的私人视频时,我得到以下信息:

youtube-dl https://youtu.be/Q-xxxx-xxxx
[youtube] Q-xxxx-xxxx: Downloading webpage
[youtube] Q-xxxx-xxxx: Downloading video info webpage
WARNING: Unable to extract video title
ERROR: This video is unavailable.
Run Code Online (Sandbox Code Playgroud)

当我尝试使用用户名和密码时,它在我的 2FA 上失败。

youtube-dl --username=me@gmail.com  https://youtu.be/Q-xxxx-xxxx
Type account password and press [Return]: 
[youtube] Downloading login page
[youtube] Looking up account info
[youtube] Logging in
Type 2-step verification code and press [Return]: 
[youtube] Submitting TFA code
WARNING: Unable to submit TFA code: HTTP Error 400: Bad Request
[youtube] Q-xxxx-xxxx: Downloading webpage
[youtube] Q-xxxx-xxxx: Downloading video info webpage
WARNING: Unable to extract video title
ERROR: This video is unavailable.
Run Code Online (Sandbox Code Playgroud)

如果我尝试--cookies在 Chrome 中使用EditThisCookie 导出,那么它也会失败。我通过转到 Chrome 中的视频并将 cookie 复制到文件中来导出。

youtube-dl --cookies=cookies.txt  https://youtu.be/Q-xxxx-xxxx
Traceback (most recent call last):
  File "/usr/bin/youtube-dl", line 11, in <module>
    load_entry_point('youtube-dl==2018.6.14', 'console_scripts', 'youtube-dl')()
  File "/usr/lib/python3.6/site-packages/youtube_dl/__init__.py", line 472, in main
    _real_main(argv)
  File "/usr/lib/python3.6/site-packages/youtube_dl/__init__.py", line 439, in _real_main
    with YoutubeDL(ydl_opts) as ydl:
  File "/usr/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 414, in __init__
    self._setup_opener()
  File "/usr/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 2303, in _setup_opener
    self.cookiejar.load()
  File "/usr/lib/python3.6/http/cookiejar.py", line 1784, in load
    self._really_load(f, filename, ignore_discard, ignore_expires)
  File "/usr/lib/python3.6/http/cookiejar.py", line 2007, in _really_load
    filename)
http.cookiejar.LoadError: 'cookies.txt' does not look like a Netscape format cookies fil
Run Code Online (Sandbox Code Playgroud)

如何使用 youtube-dl 下载我的私人视频?

编辑:

我还尝试了应用程序密码,但由于某种原因,这对我来说也失败了:

youtube-dl --username=me@gmail.com --password=xxxxxxxxx youtu.be/Q-xxxxxxxx
[youtube] Downloading login page
[youtube] Looking up account info
[youtube] Logging in
WARNING: Unable to login: Invalid password
[youtube] Q-xxxxxx: Downloading webpage
[youtube] Q-xxxxxx: Downloading video info webpage
WARNING: Unable to extract video title
ERROR: This video is unavailable.
Run Code Online (Sandbox Code Playgroud)

Dav*_*ish 24

我能够使用该--cookies路线下载私人视频。我必须首先将 cookie 格式化为 Netscape 格式,这是 EditThisCookie 中的一个选项。请记住,您不仅需要https://youtube.com的 cookie,还需要https://accounts.google.com的 cookie 。您必须使用 EditThisCookie Chrome 插件手动执行此操作。

然后我需要用curl. 类似的东西:curl -b oldcookiefile.txt --cookie-jar newcookiefile.txt 'https://youtube.com'

--cookies然后可以将该标志与 youtube-dl 一起使用来下载私人视频。

我还创建了一篇博客文章,更详细地描述了我学到的东西。

  • 使用“cookies.txt”chrome 扩展,它更容易:-) https://chrome.google.com/webstore/detail/cookiestxt/njabckikapfpffapmjgojcnbfjonfjfg?hl=en (8认同)
  • @sekmo:它可能更容易,但 [浏览器扩展是一种安全和隐私风险](https://www.howtogeek.com/188346/why-browser-extensions-can-be-dangerous-and-how-to-保护你自己/)。您可以简单地从 Chrome 复制/粘贴 cookie,然后 [将它们转换为 Netscape 格式](https://github.com/dandv/convert-chrome-cookies-to-netscape-format)。 (3认同)