📜  python pytube - Python (1)

📅  最后修改于: 2023-12-03 15:18:59.408000             🧑  作者: Mango

Python Pytube

Pytube is a lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos. It was created and is maintained by Nick Ficano.

Installation

Pytube can be installed with pip:

pip install pytube
Usage

To download a YouTube video with Pytube, simply instantiate the YouTube class with the video URL and call the streams attribute. From there, you can choose the stream you want to download and call the download() method.

from pytube import YouTube

# Instantiate YouTube class with video URL
yt = YouTube('https://www.youtube.com/watch?v=dQw4w9WgXcQ')

# Print all available video streams
print(yt.streams.all())

# Choose the stream you want to download
stream = yt.streams.get_by_itag(22)

# Download the stream
stream.download()
Features

Pytube offers the following features:

  • Download YouTube videos
  • Retrieve metadata for YouTube videos
  • Retrieve captions for YouTube videos
  • Batch operations (i.e., downloading multiple videos in parallel)
Command-Line Utility

Pytube also comes with a command-line utility that can be used for downloading YouTube videos. To download a video simply run:

python -m pytube [url]

For example:

python -m pytube https://www.youtube.com/watch?v=dQw4w9WgXcQ
Limitations

Pytube does not currently support:

  • Live video streams
  • Dash streams
  • Age-restricted videos (without logging in)
Conclusion

Pytube is a lightweight and easy-to-use Python library for downloading YouTube videos. Its simplicity makes it a great choice for beginners, while its versatility and batch operations make it a solid choice for more experienced programmers.