JuanBindez / pytubefix

Python3 library for downloading YouTube Videos.
http://pytubefix.rtfd.io/
MIT License
674 stars 95 forks source link

Error downloading SRT captions #149

Closed phasefactor closed 3 months ago

phasefactor commented 3 months ago

Describe the bug

Both Caption.download() and Caption.save_captions() are calling Caption.xml_caption_to_srt() which is throwing:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/miniconda3/lib/python3.12/site-packages/pytubefix/captions.py", line 74, in save_captions
    srt_captions = self.xml_caption_to_srt(self.xml_captions)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user/miniconda3/lib/python3.12/site-packages/pytubefix/captions.py", line 115, in xml_caption_to_srt
    caption = unescape(caption.replace("\n", " ").replace("  ", " "),)
                       ^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'

To Reproduce

from pytubefix import YouTube
yt = YouTube('https://www.youtube.com/watch?v=MFRWDuduuSw')
yt.captions['en'].save_captions('test.srt')

Additional Context

The quick fix for this was to just check the type of caption on the line before captions.py:115:

                if caption == None:
                    continue
                caption = unescape(caption.replace("\n", " ").replace("  ", " "),)

This seems to pull down the SRTs correctly, but I am sure there is a more elegant fix.