The following examples show how to use the YouTube Data API (v3) to perform functions related to captions.
Retrieve a list of caption tracks for a video
To retrieve a list of caption tracks that are available for a specific video,
call the captions.list
method. Set the
videoId
parameter value to the YouTube video ID that uniquely identifies the video for
which you are retrieving captions. Requests for private or unlisted videos, or
to view draft or non-servable tracks, must be authorized using OAuth 2.0 by a
user with access to the video's captions.
The following request retrieves a list of captions for a video on the Google Stories YouTube channel:
https://developers.google.analiseja.site/apis-explorer/#p/youtube/v3/youtube.captions.list? part=snippet &videoId=PRU2ShMzQRg
See Use cases and code snippets for code samples.
Create a caption track
You can use the API's
captions.insert method to
upload a new caption track for a video. When uploading a track, you must specify
a value for the following caption resource properties:
snippet.videoId- This identifies the video to which the caption track is being added.snippet.language- The language of the caption track.snippet.name- The name of the caption track.
Keep the following constraints and options in mind when uploading a caption track:
- Uploaded caption files must include timing information; the
syncparameter is deprecated and no longer supported. -
If you set the
captionresource'ssnippet.isDraftproperty totrue, the track is not publicly visible.
See Use cases and code snippets for code samples.
Download a caption track
To download a specific caption track, call the
captions.download method.
Set the
id
parameter value to the YouTube caption track ID that uniquely identifies the
caption track that you are downloading. Your request must be authorized using
OAuth 2.0 by a user with permission to edit the video's captions.
The v3 API supports the following options:
- To specify that a caption track should be returned in a specific format, set
the
tfmtparameter value to identify the selected format. The parameter definition lists supported values. - To retrieve a translation of a caption track, set the
tlangparameter value to a supported BCP-47 language tag that identifies the selected caption language. The translation is generated by machine translation, such as Google Translate.
See Use cases and code snippets for code samples.
Update a caption track
The API supports the ability to change a caption track's draft status, upload a new caption track for a file, or both.
You can use the APIs Explorer to test the ability to change a track's draft status. However, you can't use the APIs Explorer to update the actual caption track because the tool doesn't support uploading files.
The following request updates the draft status of a caption track to true,
which means the track won't be publicly visible. To complete the request in the
APIs Explorer, you need to set the id property's value to identify a caption
track.
https://developers.google.analiseja.site/apis-explorer/#p/youtube/v3/youtube.captions.update? part=snippet
See Use cases and code snippets for code samples.
Delete a caption track
This example shows how to delete a caption track. The example has the following steps:
-
Step 1: Retrieve the caption track ID
Follow the earlier steps to retrieve a list of caption tracks for a video. The list could be used to display a list of caption tracks, using each track's ID as a key.
-
Step 2: Delete the track
Call the
captions.deletemethod to delete a specific video. In the request, theidparameter specifies the caption track ID of the track that you are deleting. The request must be authorized using OAuth 2.0. If you are testing this query in the APIs Explorer, you will need to substitute a valid caption track ID for theidparameter value.https://developers.google.analiseja.site/apis-explorer/#p/youtube/v3/youtube.captions.delete? id=CAPTION_TRACK_ID