Open In App

How to download public YouTube captions in XML using Pytube in Python?

Prerequisite: Pytube

Pytube is a dependency-free lightweight Python library for downloading YouTube videos. There are various APIs to fetch metadata from YouTube. In this article, we are going to see how to download public YouTube captions in XML using Python.



Before starting we need to install this module:

pip install pytube

Approach:



Below is the implementation:




from pytube import YouTube
 
src = YouTube(link)
 
# prints all available captions in various languages.
print('Captions Available: ', src.captions)
print()
 
# Getting only English captions by specifying 'en' as parameter
en_caption = src.captions.get_by_language_code('en')
print(en_caption.xml_captions)
 
# Instead of Captions in XML format we are converting it to string format.
en_caption_convert_to_srt = (en_caption.generate_srt_captions())
print(en_caption_convert_to_srt)

Output:

XML Captions 

Article Tags :