58

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.

3
  • 1
    Did you try generating an app password in your google account dashboard and using that when providing your credentials to YouTube-dl?
    – user220129
    Commented Jan 27, 2019 at 0:54
  • a fresh version of youtube-dl (from their just reinstated GitHub repo) works for me with Google's 2FA (put the app-specific password you can generate on the command line, as -2 XXXXXXXXX) Commented Nov 30, 2020 at 12:39
  • This question does not seem to be related to macOS in any way. Commented May 1, 2023 at 8:19

6 Answers 6

31

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 a Get cookies.txt chrome extension, open the youtube then export the cookies using the plugin.

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
3
29

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.

3
  • Works well on Ubuntu and with the fork yt-dlp
    – brasofilo
    Commented Jun 18, 2022 at 0:23
  • Works not for me, as I still get ERROR: Join this channel to get access to members-only content like this video, and other exclusive perks.
    – alrts
    Commented Jan 12, 2023 at 11:04
  • It works for me like this: youtube-dl --add-header Cookie:"COOKIE_STRING"
    – Garric
    Commented Mar 10, 2023 at 20:21
14

This approach doesn't require installing any browser extension, which could pose a security risk, as @bgentry also pointed out.

It requires a terminal window, Node.js, and this open-source script I published.

  1. Download the script (or clone the repo if you prefer)

  2. In an editor, open a new blank file

  3. In Chrome/Chromium, launch Developer Tools (F12)

  4. Navigate to the site you need cookies from, e.g. YouTube, and log in.

  5. Go to Application -> Storage -> Cookies

  6. For each URL under Cookies (e.g. https://www.youtube.com), copy the table of cookies into the clipboard, then paste it at the end of the file you've opened in step 2. Chrome cookies NOTE: Ensure long cookies are intact, copy/paste directly to repair any that were cut off if needed. [cutoff will include … symbol, can search for it with ctrl+f]

  7. Save the file with a name like file-with-cookies-copy-pasted-from-Chrome.txt

  8. Run the script:

     node convert-cookies.js file-with-cookies-copy-pasted-from-Chrome.txt > netscape-cookies.txt
    

Now, netscape-cookies.txt will contain cookies ready to be used by any application that reads cookies in Netscape format (e.g. yotube-dl or curl).

3
  • Note: the input txt file must be encoded in UTF-8, weird results if Unicode
    – hymced
    Commented May 1, 2020 at 9:50
  • @dan-dascalescu When the "Copy" item does not appear in the right-clik menu, just press the "shift" key BEFORE right-click to open the context menu.
    – SebMa
    Commented Mar 16, 2021 at 13:38
  • 2
    @dan-dascalescu One can also use this command to convert the cookies to the Nescape jar cookies format : curl -o /dev/null -s --cookie file-with-cookies-copy-pasted-from-Chrome.txt --cookie-jar netscape-cookies.txt https://www.youtube.com
    – SebMa
    Commented Mar 16, 2021 at 13:39
14

This answer doesn't require an extension and is mostly automatic.

  1. Press Ctrl+Shift+I to open Web Developer Tools
  2. Select the Console tab at the top of the Web Developer Tools window
  3. Copy this code into the console input and press Enter
(function() {
  let S = '# Netscape HTTP Cookie File\n';
  for (raw_cookie of document.cookie.split(';')) {
    let cookie = raw_cookie.trim();
    let separator = cookie.indexOf('=');
    let name = cookie.substring(0, separator);
    let value = cookie.substring(separator + 1);

    let domain = window.location.hostname;
    // hopefully this will convert domains like `www.test.com` and `test.com` into `.test.com`
    domain = domain.replace('www.', '.');
    if (domain[0] !== '.') {
        domain = '.' + domain;
    }

    // netscape cookie file format:
    // # domain  HTTP PATH SECURE timestamp name  value
    // .test.com TRUE /    FALSE  123456789 token 1234abcdef
    S += `${domain}\tTRUE\t/\tTRUE\t0\t${name}\t${value}\n`
  }

  console.log(S)
})();
  1. Right-click on output and select Copy Message
  2. Create cookies.txt and paste output there
  3. I also had to delete the last line which said something like this: debugger eval code:21:11

I've tested this method with Firefox on twitch.tv and successfully used the resulting file with youtube-dl 2021.06.06

PS: Don't forget to remove HttpOnly attributes from the cookies before running the function (in Chrome: DevTools>Application>Cookies>{URL}, uncheck the checkboxes in HttpOnly column). Otherwise, HttpOnly cookies won't show up in the output of the function and this may lead to authorization errors.

2
  • While this is cool, for some reason yt-dlp didn't work with cookies, but it did work on crunchyrolll when I used the -u username and password option. Commented Feb 11, 2023 at 7:00
  • Thank you, this worked perfectly with Safari.
    – Martijn
    Commented Oct 18, 2023 at 6:46
7

While this is not a direct answer to the question, it's a much better solution: you can use a fork of youtube-dl called yt-dlp that can directly load cookies from browser databases using --cookies-from-browser firefox or --cookies-from-browser chrome so no need to export at all!

4

Since all of the Chrome extensions for cookies are closed-source…

  1. F12 → Application → Storage → Cookies
  2. Select everything manually and copy to some text file
  3. $ cat /tmp/some-text-file.txt | awk 'BEGIN{print "# Netscape HTTP Cookie File"}{sub(/^[^.]/,".&",$3);print $3"\tTRUE\t/\tTRUE\t0\t"$1"\t"$2}' > /tmp/cookies.txt

You must log in to answer this question.

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