How to download private videos from YouTube with youtube-dl

Posted on June 22, 2018

youtube-dl is a wonderful tool for downloading videos so they can be watched offline. Most of the time I only want to store a publicly accessible video. Sometimes I want to access a video that is only accessible when I am logged into my YouTube account. To do this was more challenging than I thought it would be, partly because youtube-dl is working through some issues. The specific issues I ran into dealt with two factor authentication and reading cookie formats.

Summary

Here is what eventually worked for me.

  1. Install EditThisCookie plugin for Chrome (Chromium in my case)
  2. Set the cookie export format to Netscape HTTP Cookie File
  3. Log into YouTube
  4. Export the cookies into a file for the domains https://youtube.com, https://accounts.google.com and https://www.google.com
  5. Make sure the cookies are properly formatted with the curl tool
  6. Download your video using the --cookies flag

Why cookies?

I had to use cookies because I couldn’t get the --username flag to work due to an issue with 2FA in youtube-dl. If 2FA authentication is not turned on then maybe --username will work. Because of this I needed to export my cookies with EditThisCookie plugin. I couldn’t find a plugin for my version of Firefox (version 60.0.2).

EditThisCookie

The first issue I had with EditThisCookie is it exported in JSON format, but youtube-dl only takes Netscape format. In EditThisCookie the format can be changed to Netscape HTTP Cookie File.

YouTube cookies

In order to get the cookies you need to first log into YouTube. You could then inspect the page and see all the cookies loaded. If you really wanted to you could then manually copy over all of the cookies manually into a cookies file. I don’t know how to do this so that the format is right, that is why I am using EditThisCookie and curl. However, you will notice that YouTube uses cookies from different domains which are needed for youtube-dl to work. Unfortunatly, EditThisCookie will not download all the cookies from several domains for a web page. You will need to manually export the cookies into a file for each of the domains https://youtube.com, https://acounts.google.com and https://www.google.com. You should finally get a file that looks something like this:

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by EditThisCookie
.youtube.com	FALSE	/	FALSE	1592746624.181169	APISID	xxxxxxxxx
.youtube.com	FALSE	/	FALSE	1529675666.4735	GPS	1
.youtube.com	FALSE	/	FALSE	1592746624.181027	HSID	xxxxx
.youtube.com	FALSE	/	FALSE	1592746624.956381	LOGIN_INFO	xxxxx
.youtube.com	FALSE	/	FALSE	1550712605.073926	PREF	xxxxx
.youtube.com	FALSE	/	TRUE	1592746624.181263	SAPISID	xxxxx
.youtube.com	FALSE	/	FALSE	1592746624.180932	SID	xxxxx
.youtube.com	FALSE	/	TRUE	1592746624.181105	SSID	xxxxxx
.youtube.com	FALSE	/	FALSE	1531743283.040211	VISITOR_INFO1_LIVE xxxxx
.youtube.com	FALSE	/	FALSE	0	YSC	xxxxx

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by EditThisCookie
.google.com	FALSE	/	FALSE	1592746624.009486	APISID	xxxxx
.google.com	FALSE	/	FALSE	1592746624.009335	HSID	xxxxx
.google.com	FALSE	/	FALSE	1545488676.021505	NID	xxxxxx
.google.com	FALSE	/	TRUE	1592746624.009559	SAPISID	xxxxx
.google.com	FALSE	/	FALSE	1592746624.009103	SID	xxxxxx
.google.com	FALSE	/	FALSE	1537454702.826293	SIDCC	xxxxxx
.google.com	FALSE	/	TRUE	1592746624.009422	SSID	xxxxxx
accounts.google.com	TRUE	/	TRUE	1592746624.009627	xxxxx
accounts.google.com	TRUE	/	TRUE	1592746624.106059	GAPS	xxxxx
accounts.google.com	TRUE	/	TRUE	1592746624.009204	LSID	xxxxx
accounts.google.com	TRUE	/	TRUE	1801458753.849165	SMSV	xxxxxx

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by EditThisCookie
.google.com	FALSE	/	FALSE	1592746624.009486	APISID	xxxxx
.google.com	FALSE	/	FALSE	1592746624.009335	HSID	xxxxx
.google.com	FALSE	/	FALSE	1545488676.021505	NID	xxxxx
.google.com	FALSE	/	TRUE	1592746624.009559	SAPISID	xxxxx
.google.com	FALSE	/	FALSE	1592746624.009103	SID	xxxxx
.google.com	FALSE	/	FALSE	1537454702.826293	SIDCC	xxxxxx
.google.com	FALSE	/	TRUE	1592746624.009422	SSID	xxxxxx

There is still more work to be done on the cookies file in order for it to work with youtube-dl. Because youtube-dl cannot parse the format exported from EditThisCookie, the cookie file needs to be run through curl like this:

curl -b cookiefile.txt --cookie-jar newcookiefile.txt '/https://youtube.com'

That will create a new newcookiefile.txt which can be used with youtube-dl.

Finally

youtube-dl can now be used to download the private videos by using the --cookies flag like so:

youtube-dl --cookies=newcookiefile.txt https://youtu.be/Q-xxxx-xxxx

Yay! 🍪

Modified: June 22, 2018 - 10:58:47 PM