yt-dlp: Passing Youtube cookies doesn't seem to work; cookies are being invalidated quickly
DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE
- I understand that I will be blocked if I intentionally remove or skip any mandatory* field
Checklist
- I’m asking a question and not reporting a bug or requesting a feature
- I’ve looked through the README
- I’ve verified that I’m running yt-dlp version 2023.09.24 (update instructions) or later (specify commit)
- I’ve searched known issues and the bugtracker for similar questions including closed ones. DO NOT post duplicates
- I’ve read the guidelines for opening an issue
Please make sure the question is worded well enough to be understood
I have been used to youtube coookies lasting a couple of weeks. But since a few weeks I have to update the cookies for yt-dlp daily. Does anyone know what is happening here? My guess is that youtube change its policies. If that is the case, is there a way to automate the process? My current “workflow”:
- Opening youtube.com in firefox,
- log in to my channel
- export cookies (all domains) using “Export cookies”
- Use the cookies file with yt-dlp
Provide verbose output that clearly demonstrates the problem
- Run your yt-dlp command with -vU flag added (
yt-dlp -vU <your command line>) - If using API, add
'verbose': TruetoYoutubeDLparams instead - Copy the WHOLE output (starting with
[debug] Command-line config) and insert it below
Complete Verbose Output
[debug] Command-line config: ['-vU']
[debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version stable@2023.09.24 [088add956] (pip)
[debug] Python 3.11.5 (CPython armv7l 32bit) - Linux-5.10.63-v7+-armv7l-with-glibc2.28 (OpenSSL 1.1.1n 15 Mar 2022, glibc 2.28)
[debug] exe versions: ffmpeg 4.1.11-0, ffprobe 4.1.11-0
[debug] Optional libraries: Cryptodome-3.19.0, brotli-1.1.0, certifi-2023.07.22, mutagen-1.47.0, sqlite3-3.27.2, websockets-11.0.3
[debug] Proxy map: {}
[debug] Loaded 1886 extractors
[debug] Fetching release info: https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest
Available version: stable@2023.09.24, Current version: stable@2023.09.24
yt-dlp is up to date (stable@2023.09.24)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 31 (11 by maintainers)
TL;DR of current known workarounds:
https://accounts.youtube.com/RotateCookiesPageandhttps://accounts.youtube.com/RotateCookiesin your browser you are using cookies from (not recommended)--cookies-from-browserif your action is quickA simple workaround would be to generate the cookies file in an incognito window, where the cookies are never used again in browser and therefore will never be rotated.
A few weeks ago, I saw this workaround somewhere (without knowing why) and tried it out. And my cookies file is still valid until now.
Since logging in with OAuth2 is gone, and cookies extracted from an incognito session still expire after a certain period of time, would implementing Cookie rotation now be the reliable way to prolong the login state?
Yes! Time to grab this link above and integrate to official builds right now. Seems Google started deprecate third party cookies, first party cookies deprecates soon in short to middle. They were annouced and deploying new mechanism on Chromium based browser. YES! TIME TO DEPLOY OAUTH LOGIN SUPPORT RIGHT NOW.
From a quick look: the browser appears to be calling
https://accounts.youtube.com/RotateCookiesPage?origin=https://www.youtube.com&yt_pid=1. This runs another javascript rube goldberg machine to transform a value akin to nsig. It then callshttps://accounts.youtube.com/RotateCookieswith that transformed value which refreshes the cookies.I’m guessing this is required or else the cookies are invalidated by YouTube if they are not refreshed.
We could attempt to implement this, but, I don’t think its really feasible or worthwhile to maintain.
Probably better off spending that time implementing oauth support for YouTube.
Blocking these 2 URLs (using an ad-blocker) can be a workaround for this, in addition to generate the cookies from a private window/a browser you never use.
I also tried adding a plug-in to firefox to automatically refresh the page, but it also didn’t work.
If this is a new mechanism added by YouTube, then maybe a function needs to be added, For example, re-obtain cookies from the browser when downloading each playlist and video.
I first tried to export cookies and use --cookies “%~dp0\cookies.txt” to specify cookies, but after a few minutes, it became invalid. When I downloaded member-only videos, it prompted me to need a higher level. permission.
Later, I used the --cookies-from-browser firefox parameter and logged in to YouTube in the browser. Everything worked fine at first, but after a few minutes to dozens of minutes, the same cookie failure occurred.
I’d guess that this cookie rotation is actually intended to protect their users from cookie stealing
I found a very strange bug, I have two accounts. Both have exported the cookie from private mode as instructed above. But one of them is very durable, I’ve been using it for almost two months, and did a lot of downloads without invalidating. However, the other account only downloaded a few videos, and it was invalid within a few hours. I want to know if it’s because the durable cookie was exported from Chrome, and I also usually log in to this YouTube account on Chrome, whereas the other not durable one is because, although I also exported the cookie from Chrome, I usually use the Opera browser for that YouTube account.
I’d imagine this is common practice for many/most users of yt-dlp, and as such we should keep this issue open until there is some kind of solution applied to yt-dlp
I’ve uploaded a cookie regenerator on my gist. It can be easily ported to youtube just by changing the urls, I guess.
https://gist.github.com/szv99/f78c032736443fab51075bc45f9faf09
It seems that youtube is rapidly invalidating cookies for IP addresses that have been flagged (e.g. data center IPs) or blocked (you would be getting 403 error responses from videoplayback URL requests without cookies).
Using
--cookies-from-browserwould be the simplest way around this. You may need to refresh the cookies in browser; so if you need full automation you could have an external script launch firefox pointing to youtube dot com before executing yt-dlp