Download Youtube Video Using pytube

YouTube is the most popular video-sharing platform in the world and as a hacker you may encounter a situation where you want to script something to download videos. For this I recommend pytube to you.
pytube is a very serious, lightweight, dependency-free Python library (and command-line utility) for downloading YouTube Videos. It has no third party dependencies and aims to be highly reliable.
pytube also makes pipelining easy, allowing you to specify callback functions for different download events, such as on progress or on complete. Finally pytube also includes a command-line utility, allowing you to quickly download videos right from terminal

Features:

  • Support for both progressive & DASH streams
  • Support for downloading complete playlist
  • Easily register on download progress & on download complete callbacks
  • Command-line interfaced included
  • Caption track support
  • Outputs caption tracks to .srt format (SubRip Subtitle)
  • Ability to capture thumbnail URL
  • Extensively documented source code
  • No third-party dependencies

Installation:

pip install pytube

Using pytube in a python script:

To download a video using the library in a script, you’ll need to first import the YouTube class from the library, and pass it an argument of the video url. From there, you can access the streams and download them.
from pytube import YouTube
YouTube(‘https://www.youtube.com/watch?v=FbnvalILrL4’).streams.first().download()
yt = YouTube(‘https://www.youtube.com/watch?v=FbnvalILrL4’)
yt.title
yt.streams.filter(progressive=True, file_extension=‘mp4’).
order_by(‘resolution’).desc().first().download()

One thought on “Download Youtube Video Using pytube

Leave a Reply

Your email address will not be published. Required fields are marked *