Upon encountering a 402 error, youtube-dl should:
It would be great if someone could provide a pcap dump that contains
Hi phihag, here is the dump - http://www.grab.lv/_external/capture-libcapng
So basically you'd have to forcibly save cookie jar and save passed cookies (use_hitbox, goojf, I'm not sure about VISITOR_INFO1_LIVE) before and after authentication (only captcha filling should be done in a real browser), parse returned html page (goes via 302 redirect) and get iframe location. This URL should be passed to console, so user can click or copy-paste in browser. User fills the captcha in his browser, copies returned hash and pastes in youtube-dl. And again, same as step one - save cookies and afterwards - retry downloading.
Maybe you should take a look at Mechanize - http://wwwsearch.sourceforge.net/mechanize/ - unfortunately I'm not familiar with Python, but WWW::Mechanize works really well for Perl.
The most important thing IMO is to give a different exit code if this 402 error is returned. I'm pretty sure this is what's requested in closed issue 144.
For the benefit of Google, this is presently the best (??) way to catch 402 errors:
error=$(youtube-dl -q "$url" 2>&1)
if [ "$?" -eq 0 ] ; then
echo "video downloaded ok!"
case "$error" in
(*404*) echo "Error - video deleted" ;;
(*402*) echo "Error - retrying too fast" ;;
Obviously, you can put some other stuff - to handle what to actually do in the case of a successful download or error - next to the appropriate echo statement. This will probably involve sleeping before retrying the download.
For the record, it doesn't require a fast box connected 24/7 to produce this error - I can reproduce using youtube-dl --write-info-json --skip-download -q "$url" (i.e. only getting the metadata about the videos) with a slow box on a home connection and only a hundred or two videos. This does appear to vary - some days this week YouTube appears to have let me get away with more than on others.
youtube-dl --write-info-json --skip-download -q "$url"
Hey i'm actually working on a project mainly depending on youtube-dl and I'm sure I'll encounter this error, does someone found a way to bypass or solve this problem ?
I got an idea but i'm not sure if it's possible:
1) send shell cmd by php an launch youtube-dl
2) if error 402 send captcha image and input to php
3) write captcha in the website and send it to linux
4)continue to DL
I don't really think this is the place to expand much on my comments above.
Whilst I hoped to help people who are encountering this problem, this is a bug report for youtube-dl, not a homework assistance forum.
Once the user fills in a captcha, the IP address is once again allowed to download.
However, filling in a captcha sets a cookie in the browser - this session is given freer access to YouTube than other sessions from the same IP.
Even with another session allowed access, it is easy for youtube-dl to once again exceed permitted downloads (and once again hit the 402 error).
youtube-dl does not have any way to tell you what captcha it wants you to solve, thus also no way for you to return the result of a captcha.
If you can have your PHP program flush the cookies from a browser and open a new browser on the same machine, then that will work.
The easiest way I have found to solve this is to build a dynamic delay - when you get a 402 error, just wait for some time before trying again. When you get multiple successful downloads, decrease the delay. This means the downloads are slower, but no human interaction is required. If you fill in a captcha, then you'll need to fill in another one within a couple of downloads; if you just wait some time (or take it easy for some time) then you can eventually resume again at a reasonable speed.
I am not going to tell you any more of what I know of the parameters of YouTube's rate limiting. As soon as they become publicly disseminated, YouTube will tighten their restrictions further.
I repeat: THIS IS A BUG REPORT, NOT A HELP FORUM.
If you need further assistance, post a question in an appropriate forum and message or email me, so that I can answer it there in public.
I was going to suggest the dynamic delay, but then noticed Strolls got it down quite nicely. Think it'll be a simple solution to try and test; at least taking the human interaction out of the equation - say when leaving the computer unattended for some time...
Would it be possible to implement something like this: http://ptigas.com/blog/2011/02/18/simple-captcha-solver-in-python/
Or this: http://www.wausita.com/captcha/
They are both written in python and may be able to be included easily?
What about adding OAuth 2 support so we can authorize our apps using this library against YouTube? This would solve the issue as far as I can tell.
@jasonrwalters in what would consist your solution? Can you explain please?
Actually, are we able to place our developer API key in our calls? If that's not a feature it should be. But if the captcha occurs because you're improperly flagged then you need to contact Google, and just write something to handle the event yourself.
See Google docs:
I had CAPTCHA issues frequently when re-grabbing entire channels. I implemented some hacks to (1) avoid hitting YouTube for videos I've already downloaded, and (2) wait 1 sec between HTTP requests: https://github.com/ludios/youtube-dl/commits/prime - and I haven't had problems since.
In the docs there's an option for that now: