Few days ago, I needed to download a complete video from YouTube including it’s subtitles.

There are many tools and browser plug-ins allowing you to download the video itself, just take a look at UserScripts. But I didn’t find any tool, that still works, which allows me to download the closed captions for a video.

I’ve decided to take a look at it myself. I’ve found the following video with closed-captions: http://youtu.be/zJOS0sV2a24 and started sniffing around.

Using chrome’s Development Tool, I’ve monitored the traffic to YouTube servers and found out the following requests:

As you can see, there are two requests for a file called “timedtext” that caught my eyes immediately. The first request was a listing request, “Waiter, Gimme’ a menu of all the captions please”. YouTube’s player passes a lot of data in each of those request, But most of it is not needed. I’ve managed to reduce the request to the following fields:

http://www.youtube.com/api/timedtext?v=VIDEO-ID&expire=1&type=list

Where instead of VIDEO-ID you should write the video ID from the URL path. For example, in Randall Munroe’s lecture, the request will be:

http://www.youtube.com/api/timedtext?v=zJOS0sV2a24&expire=1&type=list

When I’ve opened the link in my browser I got the following XML in response:

The first and the last lines, don’t contain any interesting information. The second line, does. The second line tells us there are english closed caption for this video.

So, How do we get the captions?
That’s when the second request takes place. In the second request we actually ordered the specific captions of our choice, “Waiter, I would like the english captions please”.
Again, I’ve reduced the fields to the following request:

http://www.youtube.com/api/timedtext?v=VIDEO-ID&lang=LANGUAGE-CODE

Where the VIDEO-ID is the video identifier again, and the LANGUAGE-CODE is the lang_code attribute’s value from the list XML we requested earlier. For example, for our video and for english captions (The only one this video got) the request will be:

http://www.youtube.com/api/timedtext?v=zJOS0sV2a24&lang=en

And that’s it. This address contains the entire english captions of our video in a XML format.