.. _captions:
Subtitle/Caption Tracks
=======================
Pytube exposes the caption tracks in much the same way as querying the media
streams. Let's begin by switching to a video that contains them::
>>> yt = YouTube('http://youtube.com/watch?v=2lAe1cqCOXo')
>>> yt.captions
{'ar':
, 'zh-HK': , 'zh-TW': , 'hr': , 'cs': , 'da': , 'nl': , 'en': , 'en-GB': , 'et': , 'fil': , 'fi': , 'fr-CA': , 'fr-FR': , 'de': , 'el': , 'iw': , 'hu': , 'id': , 'it': , 'ja': , 'ko': , 'lv': , 'lt': , 'ms': , 'no': , 'pl': , 'pt-BR': , 'pt-PT': , 'ro': , 'ru': , 'sk': , 'es-419': , 'es-ES': , 'sv': , 'th': , 'tr': , 'uk': , 'ur': , 'vi': }
Now let's checkout the english captions::
>>> caption = yt.captions.get_by_language_code('en')
Great, now let's see how YouTube formats them::
>>> caption.xml_captions
'K-pop!...'
Oh, this isn't very easy to work with, let's convert them to the srt format::
>>> print(caption.generate_srt_captions())
1
00:00:10,200 --> 00:00:11,140
K-pop!
2
00:00:13,400 --> 00:00:16,200
That is so awkward to watch.
...