2

How do I use the --cookies option to youtube-dl to download a Youtube video? Can anyone provide a worked example of how to obtain the relevant cookies for Youtube and put it in the appropriate format for youtube-dl?

Context: I want to download a private Youtube video. For various reasons, the method of logging into my Google account is complicated and involves two-factor authentication and not supported by youtube-dl's existing command-line options. Therefore, it seems the best way is to log in manually in my browser, obtain the authentication cookies somehow, and use the --cookies option -- but I can't find a worked example of how to do this.

  • Did you try generating an app password in your google account dashboard and using that when providing your credentials to YouTube-dl? – wateroverflow9102 Jan 27 at 0:54
  • 1
    @wateroverflow9102, no! That's a great idea, thank you for the suggestion. – D.W. Jan 27 at 2:05
  • I think the cookie method would also work, but this might prove to be a more long-term solution since you wouldn’t have to repeat this process every once in a while after the session expires... – wateroverflow9102 Jan 27 at 2:23
3

Once you get the cookies, you only need to run youtube-dl --cookies /pathtocookiefile

If you use Chrome and accept third-party plugins to read your cookies.
Just try this cookie.txt,open the youtube then click cookie.txt plugin.
One more click ,you can download the cookie file for one tab or all. enter image description here

If you want do it manual in Chrome. F12 > Application > Storage > Cookies.
You need convert cookie list to Netscape format cookies file

#domain          HTTP/Secure    Expires      Name Value  
.youtube.com     TRUE/FALSE    1548523767    GPS    1
2

While this may not work for YouTube specifically, I wanted to highlight an easier approach that works well for youtube-dl to download from a site that requires login (and uses cookies to track the session).

Edit: Unfortunately YouTube is dependent on cookies set across multiple domains, so this approach probably will not work there.

youtube-dl has an option called --add-header:

--add-header FIELD:VALUE         Specify a custom HTTP header and its value, separated by a colon ':'. You can use this option
                                 multiple times

Cookies are ultimately just submitted to the server as HTTP headers, so all you have to do is set the Cookie header on the request. This method is also quite a bit easier than extracting a cookie jar from a modern browser, especially since those tend to be encrypted and people often turn to questionable 3rd party tools to extract that data.

Here is a usage example:

youtube-dl --add-header "Cookie:COOKIE_STRING_EXTRACTED_FROM_BROWSER" "https://website-that-hosts.example/the-video-you-want"

To extract the value of the cookie header:

  1. Open the website you wish to download from
  2. Log in
  3. Open your browser's network inspector
  4. Reload the page
  5. Find the very first request made to that website and click on it, then view the headers for the request
  6. Copy the entire string value of the Cookie header, highlighted in this image:

    Extracting the Cookie header from Google Chrome

  7. Paste that value into the example command above to replace COOKIE_STRING_EXTRACTED_FROM_BROWSER (inside the quotes)

This works well to download full episodes from sites that require cable provider login.

  • Brilliant! Thanks for the tip – Ode Jun 5 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .