JuanBindez/pytubefix

caption.download throw errors

Closed this issue ยท 4 comments

Hi, I've installed pytubefix version 5.1.2. Here's the test code:

from pytubefix import YouTube
from pytubefix import Channel

url = 'https://www.youtube.com/watch?v=JRAUvWLULEk' # caption failed

def test_YouTube():
    yt = YouTube(url, on_progress_callback=show_download_progress
                    , use_oauth=True, allow_oauth_cache=True)
    video_stream = yt.streams.get_highest_resolution()

    output_path = r"./"
    # video_title_file = "test.mp4"
    # video_stream.download(output_path=output_path, filename=video_title_file, max_retries=5)

    for caption in yt.captions:
        caption_code = caption.code
        if 'en' in caption_code:
            print(f':{caption} {caption_code}...')
            caption.download(title=yt.title,output_path=output_path)
            print(f'{caption_code} downloaded successfully.')

if __name__ == '__main__': 
    test_YouTube()

Below is the error message I get when running the code:

PS D:\code\python\project\some-reverse-works\examples\pytube> python .\test_pytubefix.py
:<Caption lang="English (auto-generated)" code="a.en"> a.en...
Traceback (most recent call last):
  File "D:\code\python\project\some-reverse-works\examples\pytube\test_pytubefix.py", line 62, in <module>
    test_YouTube()
  File "D:\code\python\project\some-reverse-works\examples\pytube\test_pytubefix.py", line 19, in test_YouTube
    caption.download(title=yt.title,output_path=output_path)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\pytubefix\captions.py", line 179, in download
    file_handle.write(self.generate_srt_captions())
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\pytubefix\captions.py", line 61, in generate_srt_captions
    return self.xml_caption_to_srt(self.xml_captions)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\pytubefix\captions.py", line 101, in xml_caption_to_srt
    for child in list(root.iter("body"))[0]:
IndexError: list index out of range

Originally posted by @jinhang87 in #69 (comment)

try pytubefix==5.2rc1

Seriously, you're the best!

Thanks to @JuanBindez for the quick update, but after updating to 5.2rc1, I still can't get the a.en caption for the videos. I've tried several videos, even changed a few IPs, but it doesn't work.

hello, try using this pytubefix==5.6rc2, where @felipeucelli made PR #85 that fixes this problem, and see if it is normalized and tell us if it is resolved.