Chúng tôi đang cập nhật Data API để phù hợp với cách YouTube tính số lượt xem video ngắn.
Tìm hiểu thêm
Triển khai: Phụ đề
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Các ví dụ sau đây cho biết cách sử dụng API Dữ liệu YouTube (phiên bản 3) để thực hiện các chức năng liên quan đến phụ đề.
Truy xuất danh sách bản phụ đề cho một video
Tạo bản phụ đề
Bạn không thể kiểm thử truy vấn này bằng Trình khám phá API vì Trình khám phá API không hỗ trợ tính năng tải tệp lên (đây là yêu cầu đối với phương thức này).
Bạn có thể sử dụng phương thức captions.insert
của API để tải một bản phụ đề mới lên cho một video. Khi tải một bản nhạc lên, bạn phải chỉ định giá trị cho các thuộc tính tài nguyên caption
sau:
Bạn có thể sử dụng các tuỳ chọn bổ sung sau đây khi tải một bản phụ đề lên:
-
Nếu bạn đặt tham số sync
thành true
, YouTube sẽ bỏ qua mọi mã thời gian có trong tệp phụ đề đã tải lên và tạo mã thời gian mới cho phụ đề.
Bạn nên đặt tham số sync
thành true
nếu bạn đang tải bản chép lời không có mã thời gian lên hoặc nếu bạn nghi ngờ mã thời gian trong tệp của mình không chính xác và muốn YouTube cố gắng khắc phục.
-
Nếu bạn đặt thuộc tính snippet.isDraft
của tài nguyên caption
thành true
, thì bản nhạc sẽ không xuất hiện công khai.
Xem tài liệu của phương thức captions.insert
để biết mã mẫu.
Tải bản phụ đề xuống
Bạn không thể kiểm thử truy vấn này bằng Trình khám phá API vì Trình khám phá API không hỗ trợ tính năng tải tệp xuống.
Để tải một bản nhạc phụ đề cụ thể xuống, hãy gọi phương thức captions.download
. Đặt giá trị tham số id
thành mã của bản phụ đề trên YouTube. Mã này giúp xác định duy nhất bản phụ đề mà bạn đang tải xuống. Yêu cầu của bạn phải được uỷ quyền bằng OAuth 2.0.
API phiên bản 3 hỗ trợ các tuỳ chọn sau:
-
Để chỉ định rằng một bản nhạc phụ đề sẽ được trả về theo một định dạng cụ thể, hãy đặt giá trị tham số tfmt
để xác định định dạng mong muốn. Phần khai báo thông số liệt kê các giá trị được hỗ trợ.
-
Để truy xuất bản dịch của một bản phụ đề, hãy đặt giá trị thông số tlang
thành mã ngôn ngữ gồm hai chữ cái theo ISO 639-1 để xác định ngôn ngữ phụ đề mong muốn. Bản dịch được tạo bằng công nghệ dịch máy, chẳng hạn như Google Dịch.
Xem tài liệu của phương thức captions.download
để biết mã mẫu.
Cập nhật bản phụ đề
API này hỗ trợ việc thay đổi trạng thái bản nháp của một bản phụ đề, tải một bản phụ đề mới lên cho một tệp hoặc cả hai.
Bạn có thể sử dụng Trình khám phá API để kiểm thử khả năng thay đổi trạng thái nháp của một bản nhạc. Tuy nhiên, bạn không thể sử dụng Trình khám phá API để cập nhật bản lồng tiếng thực tế vì công cụ này không hỗ trợ tính năng tải tệp lên.
Yêu cầu bên dưới truy xuất danh sách phụ đề của một video trên kênh Google Stories trên YouTube:
Yêu cầu này sẽ cập nhật trạng thái bản nháp của một bản nhạc phụ đề thành true
, nghĩa là bản nhạc đó sẽ không xuất hiện công khai. Để hoàn tất yêu cầu trong Trình khám phá API, bạn cần đặt giá trị của thuộc tính id
để xác định một kênh phụ đề.
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.captions.update?
part=snippet
Xem tài liệu của phương thức captions.update
để biết mã mẫu.
Xoá bản phụ đề
Ví dụ này cho thấy cách xoá một bản lồng tiếng. Ví dụ này có các bước sau:
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2024-11-23 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2024-11-23 UTC."],[[["\u003cp\u003eThis document provides instructions on how to use the YouTube Data API (v3) to manage video captions.\u003c/p\u003e\n"],["\u003cp\u003eYou can retrieve a list of caption tracks for a specific video using the \u003ccode\u003ecaptions.list\u003c/code\u003e method, requiring OAuth 2.0 authorization.\u003c/p\u003e\n"],["\u003cp\u003eUploading a new caption track to a video is achieved using the \u003ccode\u003ecaptions.insert\u003c/code\u003e method, requiring the \u003ccode\u003esnippet.videoId\u003c/code\u003e, \u003ccode\u003esnippet.language\u003c/code\u003e, and \u003ccode\u003esnippet.name\u003c/code\u003e properties to be set.\u003c/p\u003e\n"],["\u003cp\u003eDownloading a specific caption track is possible via the \u003ccode\u003ecaptions.download\u003c/code\u003e method, including options to specify the desired format and retrieve a translated version.\u003c/p\u003e\n"],["\u003cp\u003eThe API allows for updating a caption track's draft status and even deleting it completely using the \u003ccode\u003ecaptions.update\u003c/code\u003e and \u003ccode\u003ecaptions.delete\u003c/code\u003e methods respectively.\u003c/p\u003e\n"]]],["The YouTube Data API (v3) facilitates caption management. Key actions include: retrieving caption track lists for a video via `captions.list`; creating new tracks with `captions.insert`, specifying video ID, language, and name; downloading tracks with `captions.download`, allowing format and language selection; updating track status or file with `captions.update`; and deleting tracks using `captions.delete` after retrieving the track ID. Uploading caption files cannot be tested in the APIs Explorer.\n"],null,["# Implementation: Captions\n\nThe following examples show how to use the YouTube Data API (v3) to perform functions related to captions.\n\nRetrieve a list of caption tracks for a video\n---------------------------------------------\n\nTo retrieve a list of caption tracks that are available for a specific video, call the [captions.list](/youtube/v3/docs/captions/list) method. Set the [videoId](/youtube/v3/docs/captions/list#videoId) parameter value to the YouTube video ID that uniquely identifies the video for which you are retrieving captions. Your request must be authorized using OAuth 2.0.\n\nThe request below retrieves a list of captions for a video on the Google Stories YouTube channel: \n\n```\nhttps://developers.google.com/apis-explorer/#p/youtube/v3/youtube.captions.list?\npart=snippet\n&videoId=PRU2ShMzQRg\n```\n\nSee the [captions.list](/youtube/v3/docs/captions/list#usage) method's documentation for code samples.\n\nCreate a caption track\n----------------------\n\nThis query cannot be tested using the APIs Explorer because the APIs Explorer does not support the ability to upload files, which is a requirement for this method.\n\nYou can use the API's [captions.insert](/youtube/v3/docs/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:\n\n\u003cbr /\u003e\n\n- [snippet.videoId](/youtube/v3/docs/captions#snippet.videoId) - This identifies the video to which the caption track is being added.\n- [snippet.language](/youtube/v3/docs/captions#snippet.language) - The language of the caption track.\n- [snippet.name](/youtube/v3/docs/captions#snippet.name) - The name of the caption track.\n\n\u003cbr /\u003e\n\nThe following additional options are available when uploading a caption track:\n\n- If you set the [sync](/youtube/v3/docs/captions/insert#sync) parameter to `true`, YouTube will disregard any time codes that are in the uploaded caption file and generate new time codes for the captions.\n\n You should set the [sync](/youtube/v3/docs/captions/insert#sync) parameter to `true` if you are uploading a transcript, which has no time codes, or if you suspect the time codes in your file are incorrect and want YouTube to try to fix them.\n- If you set the `caption` resource's [snippet.isDraft](/youtube/v3/docs/captions#snippet.isDraft) property to `true`, the track will not be publicly visible.\n\nSee the [captions.insert](/youtube/v3/docs/captions/insert#examples) method's documentation for code samples.\n\nDownload a caption track\n------------------------\n\nThis query cannot be tested using the APIs Explorer because the APIs Explorer does not support the ability to download files.\n\nTo download a specific caption track, call the [captions.download](/youtube/v3/docs/captions/download) method. Set the [id](/youtube/v3/docs/captions/download#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.\n\nThe v3 API supports the following options:\n\n- To specify that a caption track should be returned in a specific format, set the [tfmt](/youtube/v3/docs/captions/download#tfmt) parameter value to identify the desired format. The parameter definition lists supported values.\n\n- To retrieve a translation of a caption track, set the [tlang](/youtube/v3/docs/captions/download#tlang) parameter value to an [ISO 639-1 two-letter language code](http://www.loc.gov/standards/iso639-2/php/code_list.php) that identifies the desired caption language. The translation is generated by using machine translation, such as Google Translate.\n\nSee the [captions.download](/youtube/v3/docs/captions/download#examples) method's documentation for code samples.\n\nUpdate a caption track\n----------------------\n\nThe API supports the ability to change a caption track's [draft status](/youtube/v3/docs/captions#snippet.isDraft), upload a new caption track for a file, or both.\n\nYou can use the APIs Explorer to test the ability to change a track's draft status. However, you cannot use the APIs Explorer to update the actual caption track because tool does not support the ability to upload files.\n\nThe request below retrieves a list of captions for a video on the Google Stories YouTube channel:\n\nThe request updates the draft status of a caption track to `true`, which means the track will not 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. \n\n```\nhttps://developers.google.com/apis-explorer/#p/youtube/v3/youtube.captions.update?\npart=snippet\n```\n\nSee the [captions.update](/youtube/v3/docs/captions/update#usage) method's documentation for code samples.\n\nDelete a caption track\n----------------------\n\nThis example shows how to delete a caption track. The example has the following steps:\n\n- **Step 1: Retrieve the caption track ID**\n\n Follow the steps above to [retrieve a list of caption tracks](#captions-list) for a video. The list could be used to display a list of caption tracks, using each track's ID as a key.\n- **Step 2: Delete the track**\n\n Call the [captions.delete](/youtube/v3/docs/captions/delete) method to delete a specific video. In the request, the `id` parameter 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 the `id` parameter value. \n\n ```\n https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.captions.delete?\n id=CAPTION_TRACK_ID\n ```"]]