Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--cookies-from-browser errors when Chrome/chromium-based browser is open in Windows #7271

Open
9 of 10 tasks
yuyoiuyoiu opened this issue Jun 9, 2023 · 71 comments
Open
9 of 10 tasks
Labels
bug Bug that is not site-specific external issue Issue with an external tool

Comments

@yuyoiuyoiu
Copy link

yuyoiuyoiu commented Jun 9, 2023

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 reporting a bug unrelated to a specific site
  • I've verified that I'm running yt-dlp version 2023.03.04 (update instructions) or later (specify commit)
  • I've checked that all provided URLs are playable in a browser with the same IP and same login details
  • I've checked that all URLs and arguments with special characters are properly quoted or escaped
  • I've searched known issues and the bugtracker for similar issues including closed ones. DO NOT post duplicates
  • I've read the guidelines for opening an issue

Provide a description that is worded well enough to be understood

cannot extract cookies from chrome when the chrome browser is open
and after closing all the windows of chrome, it can run correctly
i have no idea what is going wrong

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': True to YoutubeDL params instead
  • Copy the WHOLE output (starting with [debug] Command-line config) and insert it below

Complete Verbose Output

C:\Users\Mechrevo>yt-dlp.exe -vU --newline -R 30 --proxy socks5://127.0.0.1:7890 -i -o "Z:\YouTube\%(upload_date)s %(title)s.%(ext)s" --ignore-config --hls-prefer-native --add-metadata --embed-thumbnail --sub-langs all,-live_chat,-danmaku,-jpn-JP --embed-subs --merge-output-format mp4 --live-from-start --cookies-from-browser chrome "https://www.youtube.com/watch?v=3DiKrPSmO5c"
[debug] Command-line config: ['-vU', '--newline', '-R', '30', '--proxy', 'socks5://127.0.0.1:7890', '-i', '-o', 'Z:\\YouTube\\%(upload_date)s %(title)s.%(ext)s', '--ignore-config', '--hls-prefer-native', '--add-metadata', '--embed-thumbnail', '--sub-langs', 'all,-live_chat,-danmaku,-jpn-JP', '--embed-subs', '--merge-output-format', 'mp4', '--live-from-start', '--cookies-from-browser', 'chrome', 'https://www.youtube.com/watch?v=3DiKrPSmO5c']
[debug] Encodings: locale cp936, fs utf-8, pref cp936, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version stable@2023.03.04 [392389b7d] (win_exe)
[debug] Python 3.8.10 (CPython AMD64 64bit) - Windows-10-10.0.19045-SP0 (OpenSSL 1.1.1k  25 Mar 2021)
[debug] exe versions: ffmpeg 4.4-full_build-www.gyan.dev (setts), ffprobe 4.4-full_build-www.gyan.dev
[debug] Optional libraries: Cryptodome-3.17, brotli-1.0.9, certifi-2022.12.07, mutagen-1.46.0, sqlite3-2.6.0, websockets-10.4
[Cookies] Extracting cookies from chrome
[debug] Extracting cookies from: "C:\Users\Mechrevo\AppData\Local\Google\Chrome\User Data\Default\Network\Cookies"
[debug] Found local state file at "C:\Users\Mechrevo\AppData\Local\Google\Chrome\User Data\Local State"                                           Traceback (most recent call last):
File "yt_dlp\__main__.py", line 17, in <module>
File "yt_dlp\__init__.py", line 967, in main
File "yt_dlp\__init__.py", line 927, in _real_main
File "yt_dlp\YoutubeDL.py", line 652, in __init__
File "yt_dlp\YoutubeDL.py", line 3827, in print_debug_header
File "yt_dlp\YoutubeDL.py", line 3871, in _setup_opener
File "yt_dlp\cookies.py", line 99, in load_cookies
File "yt_dlp\cookies.py", line 120, in extract_cookies_from_browser
File "yt_dlp\cookies.py", line 289, in _extract_chrome_cookies
File "yt_dlp\cookies.py", line 952, in _open_database_copy
File "shutil.py", line 418, in copy
File "shutil.py", line 264, in copyfile
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Mechrevo\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Network\\Cookies'
[25380] Failed to execute script '__main__' due to unhandled exception!
C:\Users\Mechrevo>
@yuyoiuyoiu yuyoiuyoiu added bug Bug that is not site-specific triage Untriaged issue labels Jun 9, 2023
@vkorablin

This comment was marked as duplicate.

@bashonly
Copy link
Member

bashonly commented Jun 9, 2023

(edited to be useful)

This issue affects Windows versions of Chrome and some other chromium-based browsers such as Edge.

The workaround (at least for Chrome) is to launch chrome.exe with the flag:
--disable-features=LockProfileCookieDatabase

For convenience, you can add it your desktop shortcut for Chrome. Right-click the shortcut, go into Properties, and change the executable path, e.g.:
"C:\Program Files\Chrome\chrome.exe" --disable-features=LockProfileCookieDatabase

@bashonly bashonly removed the triage Untriaged issue label Jun 9, 2023
@bashonly bashonly changed the title [Cookies] cannot extract cookies from browser when the browser is open [Cookies] cannot extract cookies from browser when Chrome is open Jun 9, 2023
@gamer191

This comment was marked as resolved.

@gamer191

This comment was marked as resolved.

@bashonly

This comment was marked as resolved.

@gamer191

This comment was marked as resolved.

@gamer191

This comment was marked as resolved.

@lockmatrix

This comment was marked as resolved.

@bashonly bashonly added the external issue Issue with an external tool label Jun 9, 2023
@Spicadox
Copy link
Contributor

Spicadox commented Jun 9, 2023

On a side note, surely it's possible to access "locked" files in read-only mode 🤔

Also had a question regarding that. The file seems to be in use so it's being "locked" and for some reason I can't even read the file, but for some reason I could copy it? I'm on windows 11 so maybe it could be an OS thing but surely if you can copy a file, then there shouldn't be any issue with read access?

@pukkandan

This comment was marked as resolved.

@bashonly
Copy link
Member

bashonly commented Jun 9, 2023

There was also a report on discord a couple days ago about this same issue happening with Safari on macOS.

@gamer191
Copy link
Collaborator

gamer191 commented Jun 9, 2023

I found the chromium commit-https://chromium.googlesource.com/chromium/src/+/03c1544d31014c04caace4145676c30586bd9612

@vkorablin
Copy link
Contributor

vkorablin commented Jun 9, 2023

As a temporary workaround, run chrome.exe with:

--disable-features=LockProfileCookieDatabase

Expect this flag to go away after a few more releases of Chrome.

@gamer191
Copy link
Collaborator

gamer191 commented Jun 10, 2023

run chrome.exe with:

--disable-features=LockProfileCookieDatabase

You can also modify the start menu shortcut (on my computer it's at C:\ProgramData\Microsoft\Windows\Start Menu\Programs).

If anyone's familiar with the location of windows start menu and taskbar shortcuts, and with the command prompt, it may be useful to create a batch file (or even better, a one-liner) to automatically do that

@BreadFish64
Copy link

Using the win32 copy instead of shutil copy doesn't work either. Might be possible to work around it with shadow copies.

@csm10495
Copy link

shadow copies work, though require admin. ... is it ok to prompt the user for admin to copy the cookies file somewhere else then use it after?

@pukkandan
Copy link
Member

No. User will need to start yt-dlp as admin in in that case. We can issue an appropriate error message, but do not elevate interactively

@csm10495
Copy link

csm10495 commented Jun 11, 2023

Opened a PR to allow this to work at least when the user is admin via a shadow copy. (#7290)

@mbway
Copy link
Contributor

mbway commented Jun 12, 2023

I don't have a solution but I have done some research that may be useful.

I tried moving the Cookies database and creating a symlink and hardlink to it for chrome to use but LockFileEx follows the links. It's possible that if the database is placed on a filesystem that doesn't support locking (like an old version of samba) then the locking won't take effect but that isn't a very practical solution.

the shadow copy approach seems like a good way around the limitation on the yt-dlp side. I think admin will be required for any solution which makes many options available but most would be too invasive.
One idea I had would be to create a separate tool that a user could use to start chrome and inject a DLL that makes LockFileEx do nothing.

The good news is that I don't think Linux will be affected because mandatory locking with fcntl is opt-in and disabled by default source (see Mandatory locking section). WSL should also be unaffected because LockFileEx hopefully can't be supported in WSL

@atefalvi

This comment was marked as resolved.

@ForsakenRei

This comment was marked as resolved.

@d2kx

This comment was marked as resolved.

@csm10495
Copy link

csm10495 commented Nov 3, 2023

So if you read into it more, they actually disabled the Bing Wallpaper app's ability to check cookies this way as the fix. He explains it in full here: https://textslashplain.com/2022/05/26/losing-your-cookies/#:~:text=Culprit%3A%20SQLLite%20DB%20Load%20failure .. so in other words there still is no non-admin workaround.

.. honestly if there was one based off what they did it would likely point to an OS security issue. The Chrome-like process locks the file for exclusive use. If you could obtain it while it had an exclusive lock, it would violate the expected OS behavior here.

I still think shadowcopy is the best way around with a prompt to run as admin to use this feature on Windows.

@csm10495
Copy link

csm10495 commented Nov 3, 2023

And I guess I spoke too soon-ish. This project: https://github.com/thewh1teagle/rookie came up with a way to do it without admin. It cheats a bit, it actually kills the process in chrome that holds the lock to the file.. thus releasing the lock... but hey chrome, just restarts it when needed and it works out fine.

I've replicated it in a pure-python gist here: https://gist.github.com/csm10495/e89e660ffee0030e8ef410b793ad6a7e.. using that logic cookies can be fetched even while Chrome is running, without admin.

@evrial

This comment was marked as resolved.

@J-Broadway

This comment was marked as resolved.

@Aelius51
Copy link

bashonly's solution (adding the --disable-features=LockProfileCookieDatabase flag) used to work for me, but lately it has failed to. I'm right back at the "Errno 13" Permission denied" error that started this issue report.

I am using Vivaldi, if that makes a difference. But again, bashonly's solution worked until just recently.

@Gadgetblues

This comment was marked as duplicate.

@gamer191

This comment was marked as resolved.

@csm10495

This comment was marked as resolved.

@Gadgetblues

This comment was marked as resolved.

@csm10495
Copy link

That's kind of offensive tbh. I was just providing a possible solution. This is open source code, people make recommendations, code changes, etc. it's part of the culture.

I'm not sure why my suggestion would not instill confidence. I'm offended by that remark. It makes me think I shouldn't share things that I think can help steer us towards a solution. I had been very excited to share that because it gives the possibility of fixing the issue for folks longer term.

Honestly it's not a suggestion for someone who doesn't know python. It's a suggestion to other people at least somewhat familiar with parts of the yt-dlp source code.

I'm not sure why you then say it shouldn't need to exist. None of us influenced the chromium behavior change here. We are where we are: trying to provide solutions.

Anyways, you don't need --cookies-from-browser. You can download the cookies.txt file then just pass those cookies via --cookies. Consider something like https://github.com/kairi003/Get-cookies.txt-LOCALLY to get that file.

@evrial
Copy link

evrial commented Nov 23, 2023

That's a really handy extension, even better I will use my local cookies on my network raspberry yt-dlp server, thanks.

@YuenSzeHong

This comment was marked as off-topic.

@evrial

This comment was marked as off-topic.

@seproDev
Copy link
Collaborator

seproDev commented Jan 4, 2024

I adapted the work of @csm10495 in to a yt-dlp plugin: https://github.com/seproDev/yt-dlp-ChromeCookieUnlock

Seems to be working well for me. Use at your own risk.

@sdudley7
Copy link

sdudley7 commented Jan 30, 2024

For some reason, not sure if related but the --disable-features=LockProfileCookieDatabase seemed to slow down and cause my chrome to hog more memory. Might be a no brainer, but worked better than the Chrome solution for me is to just get them from Firefox instead.

--cookies-from-browser firefox

@SZRabinowitz
Copy link

@csm10495 I really appreciate all your help here and cookie3 repo with all the methods of getting cookies from chrome. I settled with rookiepy in a script and it worked great. Weird that it gaves the cookies as a list of dicts, which is incompatible with requests library. I simply converted it with:
{cookie['name']: cookie['value'] for cookie in rookiepy.chrome(['domain.com'])}

Thanks again for all your help!

@pukkandan
Copy link
Member

@csm10495 Would you be willing to Unlicense your script so we can merge it?

@csm10495
Copy link

@csm10495 Would you be willing to Unlicense your script so we can merge it?

I'm a big fan of yt-dlp, but sorry, I believe in attribution for my external open source code. The unlicence isn't really something I'm currently interested in. Thanks for considering it.

@ofek
Copy link

ofek commented Mar 6, 2024

In case it helps someone unfamiliar with plugins as I was, you can simply clone into the configuration directory like so (Windows example):

git clone https://github.com/seproDev/yt-dlp-ChromeCookieUnlock C:\Users\ofek\AppData\Roaming\yt-dlp\plugins\cookie_unlock

Now I'm just trying to find a way to get rid of the Attempting to unlock cookies message so I can pipe the output of the following command directly to a JSON field extractor:

yt-dlp -j --cookies-from-browser chrome ...

@bashonly
Copy link
Member

bashonly commented Mar 6, 2024

@ofek seproDev/yt-dlp-ChromeCookieUnlock#1

git fetch origin pull/1/head:fix/stderr
git checkout fix/stderr

then after PR is merged you can:

git checkout main
git pull

@user1121114685
Copy link

user1121114685 commented Mar 18, 2024

re: https://github.com/seproDev/yt-dlp-ChromeCookieUnlock/

.
├── yt-dlp.exe
└── yt-dlp-plugins
    └── cookie_unlock
        └── yt_dlp_plugins
            └── postprocessor
                └── chrome_cookie_unlock.py

Here is an example to help novice users quickly use the plug-in to solve this problem. You don't have to use anything from this example, you just need to store the corresponding files in the corresponding directory.
这是一个例子,方便新手用户快速使用插件解决这个问题。你不必使用这个例子上的任何东西,你只需要按照响应的目录存放对应文件即可。

@tweety3d
Copy link

tweety3d commented Apr 5, 2024

I was shown following error:
ERROR: Could not copy Chrome cookie database. See https://github.com/yt-dlp/yt-dlp/issues/7271 for more info
And I was able to resolve it by simply closing Chrome (because it locks the cookies file).

pagend0s added a commit to pagend0s/pobierak4windows that referenced this issue Apr 21, 2024
…ormation display

added opening an instance with unblocked cookies for edge and chrome:
--disable-features=LockProfileCookieDatabase
yt-dlp/yt-dlp#7271
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug that is not site-specific external issue Issue with an external tool
Projects
None yet
Development

Successfully merging a pull request may close this issue.