JuanBindez / pytubefix

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

Issue with Downloading YouTube Videos that Require Login #143

Closed tzzht closed 1 month ago

tzzht commented 1 month ago

Hi JuanBindez,

I really appreciate your work. But I've been using your YouTube video downloader tool from this repository and have encountered a problem. It seems that when trying to download certain videos, the process fails because the video requires user login.

Here's a brief rundown of what I've experienced:

  1. Video URL: https://www.youtube.com/watch?v=d0o89z134CQ
  2. Error Message: see bottom.
  3. Steps to Reproduce:
    yt = YouTube(url)
    # print the statistics
    print(f"Video Title: {yt.title}")

I was wondering if there's a way to handle videos that are behind a login, such as providing a method to log in with my account or bypassing this requirement.

Would it be possible for you to provide a solution or guidance on how to resolve this? It would be greatly appreciated.

Thank you for your time and for maintaining this useful tool. Looking forward to your response.

Best regards

Error Message:

Traceback (most recent call last):
  File "C:\Users\tzzht\miniconda3\envs\subator\lib\site-packages\pytubefix\__main__.py", line 612, in title
    self._title = self.vid_info['videoDetails']['title']
KeyError: 'videoDetails'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Documents\Code\Subator\src\main.py", line 33, in <module>
    video_path, audio_path, resouces_dir = downloader.downloader(url, save_dir)
  File "D:\Documents\Code\Subator\src\downloader.py", line 52, in downloader
    yt = get_video_info(url)
  File "D:\Documents\Code\Subator\src\downloader.py", line 11, in get_video_info
    print(f"Video Title: {yt.title}")
  File "C:\Users\tzzht\miniconda3\envs\subator\lib\site-packages\pytubefix\__main__.py", line 616, in title
    self.check_availability()
  File "C:\Users\tzzht\miniconda3\envs\subator\lib\site-packages\pytubefix\__main__.py", line 310, in check_availability
    raise exceptions.LoginRequired(video_id=self.video_id)
pytubefix.exceptions.LoginRequired: d0o89z134CQ requires login to view
kalyan-sama commented 1 month ago

@tzzht, have you got any solution for this?

tzzht commented 1 month ago

@tzzht, have you got any solution for this?

Yeah, based on the updated document of pytubefix, you can use use_oauth=True, allow_oauth_cache=True

from pytubefix import YouTube
from pytubefix.cli import on_progress

url = "url"

yt = YouTube(url, use_oauth=True, allow_oauth_cache=True, on_progress_callback = on_progress)

ys = yt.streams.get_audio_only()

ys.download(mp3=True) # you will only get the request to authenticate once you download

to add authentication