43

I have started using v3 of the YouTube apis on an android device, using the java client library. Some videos that I am interested in have transcripts that I can access on the web interface (like educational videos). Is there a way to access the transcripts, if present, using the v3 apis?

Thanks

1
  • Are you talking about a server api or a client api?
    – Ken Sharp
    Commented Feb 22, 2016 at 13:46

4 Answers 4

45

I had the same problem with this... and spent like a week looking for a solution until I hit another StackOverflow question.

Just do a GET request on: http://video.google.com/timedtext?lang={LANG}&v={VIDEOID} You don't need any api/oauth/etc. to access this.

11
  • 3
    Worked fine for me. Excelent job! Commented May 31, 2014 at 4:41
  • 15
    this seems to work when the captions were entered manually, as opposed to be created automagically by youtube.
    – popstack
    Commented May 27, 2015 at 14:04
  • 2
    +1 for the information but it is not working for me too, I have uploaded video and generated captions automatically http://video.google.com/timedtext?lang=en&v=h2pWeot3MJY&track=asr but not getting caption from the video and it does not seems possible.
    – manish1706
    Commented Jan 2, 2018 at 8:46
  • 18
    None of the solutions I could find allowed me to retrieve automatically generated subtitles. Therefore I came up with a bit more complex solution. Code can be found on my GitHub if anyone is still interested: github.com/jdepoix/youtube-transcript-api
    – jdepoix
    Commented Apr 20, 2018 at 14:02
  • 4
    The post you linked to is no longer available
    – katiedev
    Commented Feb 2, 2023 at 5:54
17

With API v3 you can first grab the available transcripts with the snippet:

https://www.googleapis.com/youtube/v3/captions?videoId=U1e2VNtEqm4&part=snippet&key=(my_api_key):

{
 "kind": "youtube#captionListResponse",
 "etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/aGHflncRxq1Uz6m1akhrOLUWUqU\"",
 "items": [
  {
   "kind": "youtube#caption",
   "etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/IC7rNKkn3SQNdovFwR6fEabUYnY\"",
   "id": "TqXDnlamg84o4bX0q2oaHz4nfWZdyiZMOrcuWsSLyPc=",
   "snippet": {
    "videoId": "U1e2VNtEqm4",
    "lastUpdated": "2016-01-25T21:50:27.142Z",
    "trackKind": "standard",
    "language": "en-GB",
    "name": "",
    "audioTrackType": "unknown",
    "isCC": false,
    "isLarge": false,
    "isEasyReader": false,
    "isDraft": false,
    "isAutoSynced": false,
    "status": "serving"
   }
  },
  {
   "kind": "youtube#caption",
   "etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/5UP1qPkmq6mzTUaEVnFC8WqjFgU\"",
   "id": "TqXDnlamg84o4bX0q2oaHw_Y53ilUWv6vMFbk0RL3XY=",
   "snippet": {
    "videoId": "U1e2VNtEqm4",
    "lastUpdated": "2016-01-25T21:55:07.481Z",
    "trackKind": "standard",
    "language": "en-US",
    "name": "",
    "audioTrackType": "unknown",
    "isCC": false,
    "isLarge": false,
    "isEasyReader": false,
    "isDraft": false,
    "isAutoSynced": false,
    "status": "serving"
   }
  }
 ]
}

And then pick the transcript you want:

https://www.googleapis.com/youtube/v3/captions/id?id=TqXDnlamg84o4bX0q2oaHz4nfWZdyiZMOrcuWsSLyPc=

or

https://www.googleapis.com/youtube/v3/captions/TqXDnlamg84o4bX0q2oaHz4nfWZdyiZMOrcuWsSLyPc=

at which point you need provide an authorization key. Apparently a simple key isn't enough. Possibly because:

Quota impact: A call to this method has a quota cost of approximately 200 units.

Note the slight difference in the URLs (/caption/ versus /caption?).

All the lovely documentation is here: https://developers.google.com/youtube/v3/docs/captions

2
  • 3
    can't pick the transcript using id, showing error 404 "The caption track could not be found. Check the value of the requests <code>id</code> parameter to ensure that it is correct." Commented Oct 20, 2016 at 4:15
  • 3
    pick the transcript you want - this part don't work with an API Key, gives error HTTP 401, Login Required.: "API keys are not supported by this API. Expected OAuth2 access token or other authentication credentials that assert a principal. See cloud.google.com/docs/authentication"
    – user555121
    Commented Nov 17, 2021 at 10:24
10

I may be wrong, but I don't think there is yet a documented way to get the caption track via v3 of the API. If you're authenticating with oAuth2, however, your authentication will also be good for v2 of the API, so you could do a quick call to this feed:

http://gdata.youtube.com/feeds/api/videos/[VIDEOID]/captiondata/[CAPTION TRACKID]

to get the data you want. To retrieve a list of possible caption track IDs with v2 of the API, you access this feed:

https://gdata.youtube.com/feeds/api/videos/[VIDEOID]/captions

That feed request also accepts some optional parameters, including language, max-results, etc. For more details, along with a sample that shows the returned format of the caption track list, see the documentation at https://developers.google.com/youtube/2.0/developers_guide_protocol_captions#Retrieve_Caption_Set

5
  • 1
    Thanks for your reply. How do I obtain the [caption trackid]? I can't see that in the v3 apis either ....
    – Ali Naddaf
    Commented Dec 28, 2012 at 15:05
  • This API will be dropped. How can I replace with v3?
    – Victor
    Commented Apr 10, 2015 at 19:22
  • 3
    Captions were added to V3 last week; here is the documentation. developers.google.com/youtube/v3/docs/captions
    – jlmcdonald
    Commented Apr 10, 2015 at 22:50
  • 2
    https://gdata.youtube.com API has been moved to https://www.googleapis.com/youtube/v3/videos/ so pls use this on as of 2018 January
    – manish1706
    Commented Jan 3, 2018 at 5:28
  • 3
    Can this method retrieve transcripts of videos not owned by myself?
    – William
    Commented Dec 17, 2020 at 15:44
6

Heres some code I wrote which grabs all the caption tracks from any youtube video without having to use the API. Just plug the video URL in the $video_url variable.

// get video id from url
$video_url = 'https://www.youtube.com/watch?v=kYX87kkyubk';
preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $video_url, $matches);

// get video info from id
$video_id = $matches[0];
$video_info = file_get_contents('http://www.youtube.com/get_video_info?&video_id='.$video_id);
parse_str($video_info, $video_info_array);

if (isset($video_info_array['caption_tracks'])) {
    $tracks = explode(',', $video_info_array['caption_tracks']);

    // print info for each track (including url to track content)
    foreach ($tracks as $track) {
        parse_str($track, $output);
        print_r($output);
    }
}
2
  • 1
    I have create a code based on your logic of caption trackas, if you want we can add more support together , I have added translation details for example. github.com/jamesjara/php-transcript-youtube-api-and-xml-parser/…
    – jamesjara
    Commented Jan 4, 2017 at 8:51
  • Seems to not work on automatically transcripted videos... :( ! So probably the way is using the Youtube API 3 with key
    – gtamborero
    Commented Jan 31, 2021 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.