Working With Captions and CaptionArray¶
The Caption class represents a single caption with its metadata, such as language, and format. The CaptionArray class provides a collection of Caption objects, offering methods to manage and interact with multiple captions easily.
With these tools, you can:
-
Extract captions in various languages and formats (e.g., SRT, TXT).
-
Download and save captions for offline use.
These features make it seamless to access and utilize YouTube captions efficiently.
Tip
You can also fetch the captions from the video object.
Fetching a Specific Caption¶
The CaptionArray object has methods that makes it easy to fetch a caption of a specfic language or langcode1
Difference between normal and translated captions
There are two types of captions Normal and Translated. Which can be determined using the translated
attribit from the Caption object.
a normal caption is on written by the user or is auto generated by youtube while a translated caption is one translated by AI.
>>> print(captions.subtitles)
({'name': 'English (auto-generated)', 'code': 'a.en'},
{'name': 'English (United States)', 'code': 'en-US'},
{'name': 'Portuguese', 'code': 'pt'},
{'name': 'Spanish', 'code': 'es'})
>>> print(captions.translations)
({'name': 'Abkhaz', 'code': 'ab'},
{'name': 'Afar', 'code': 'aa'},
{'name': 'Afrikaans', 'code': 'af'},
{'name': 'Akan', 'code': 'ak'},
{'name': 'Albanian', 'code': 'sq'},
{'name': 'Amharic', 'code': 'am'},
{'name': 'Arabic', 'code': 'ar'},
...
{'name': 'Zulu', 'code': 'zu'})
Fetching Caption by Language Name¶
To get a caption by langname
we use the get_captions_by_name
method.
Note
The get_captions_by_name
method only returns normal Captions. Read more from the api refrence of youtube_dl_scraper.core.caption_array.CaptionArray.
Example
You can also fetch a translated caption by langname
using the get_translated_captions_by_name()
method.
Note
The get_translated_captions_by_name()
method only return translated Captions. Read more from the api refrence of youtube_dl_scraper.core.caption_array.CaptionArray.
Example
Fetching Caption by Language Code¶
To get a caption by langname
, we use the get_captions_by_lang_code
method.
Example
You can also fetch a translated caption by langname
using the get_translated_captions_by_lang_code()
method.
Example
Dowloading Captions¶
Now, that you've gotten the specfic Caption you want, now you can read, save it in different formats.
The Caption object has diffrent methods and attributes to allow this.
Example
1
00:00:00,060 --> 00:00:04,219
Hace seis años, Tesla bajó una sorpresa de la parte trasera de un camión.
2
00:00:04,260 --> 00:00:13,380
Se trataba del Roadster 2.0, un auto superdeportivo eléctrico de dos puertas con especificaciones increíbles y el récord de aceleración mundial.
3
00:00:13,420 --> 00:00:17,590
Se suponía que ese sería el auto que acabaría con todos los otros autos a combustible.
...
137
00:12:19,240 --> 00:12:19,775
Adiós.
/storage/emulated/0/Youtube-dl-scraper/Youtube-dl-scraper/downloads/Driving The New Fastest Car Ever Made!.txt
/storage/emulated/0/Youtube-dl-scraper/Youtube-dl-scraper/downloads/Driving The New Fastest Car Ever Made!.srt
-
Language codes are standardized short identifiers, like en or fr, used to represent languages and their regional or script variants for localization and data interchange. ↩