Downloading youtube videos with python

pytube¶

pytube is a lightweight, Pythonic, dependency-free, library (and command-line utility) for downloading YouTube Videos.

Behold, a perfect balance of simplicity versus flexibility:

>>> from pytube import YouTube >>> YouTube('https://youtu.be/9bZkp7q19f0').streams.first().download() >>> yt = YouTube('http://youtube.com/watch?v=9bZkp7q19f0') >>> yt.streams . .filter(progressive=True, file_extension='mp4') . .order_by('resolution') . .desc() . .first() . .download() 

Features¶

  • Support for Both Progressive & DASH Streams
  • 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

The User Guide¶

This part of the documentation begins with some background information about the project, then focuses on step-by-step instructions for getting the most out of pytube.

  • Installation of pytube
    • Get the Source Code
    • Downloading a Video
    • DASH vs Progressive Streams
    • Filtering by streaming method
    • Filtering for audio-only streams
    • Filtering for MP4 streams
    • Creating a Playlist
    • Interacting with a playlist
    • Creating a Channel
    • Interacting with a channel

    The API Documentation¶

    If you are looking for information on a specific function, class, or method, this part of the documentation is for you.

    Indices and tables¶

    Источник

    Quickstart¶

    This guide will walk you through the basic usage of pytube.

    Let’s get started with some examples.

    Downloading a Video¶

    Downloading a video from YouTube with pytube is incredibly easy.

    Begin by importing the YouTube class:

    >>> from pytube import YouTube 

    Now, let’s try to download a video. For this example, let’s take something like the YouTube Rewind video for 2019:

    >>> yt = YouTube('http://youtube.com/watch?v=2lAe1cqCOXo') 

    Now, we have a YouTube object called yt .

    The pytube API makes all information intuitive to access. For example, this is how you would get the video’s title:

    >>> yt.title YouTube Rewind 2019: For the Record | #YouTubeRewind 

    And this would be how you would get the thumbnail url:

    >>> yt.thumbnail_url 'https://i.ytimg.com/vi/2lAe1cqCOXo/maxresdefault.jpg' 

    Neat, right? For advanced use cases, you can provide some additional arguments when you create a YouTube object:

    >>> yt = YouTube( 'http://youtube.com/watch?v=2lAe1cqCOXo', on_progress_callback=progress_func, on_complete_callback=complete_func, proxies=my_proxies, use_oauth=False, allow_oauth_cache=True ) 

    When instantiating a YouTube object, these named arguments can be passed in to improve functionality.

    The on_progress_callback function will run whenever a chunk is downloaded from a video, and is called with three arguments: the stream, the data chunk, and the bytes remaining in the video. This could be used, for example, to display a progress bar.

    The on_complete_callback function will run after a video has been fully downloaded, and is called with two arguments: the stream and the file path. This could be used, for example, to perform post-download processing on a video like trimming the length of it.

    The use_oauth and allow_oauth_cache flags allow you to authorize pytube to interact with YouTube using your account, and can be used to bypass age restrictions or access private videos and playlists. If allow_oauth_cache is set to True, you should only be prompted to do so once, after which point pytube will cache the tokens it needs to act on your behalf. Otherwise, you will be prompted again for each action that requires you to be authenticated.

    Once you have a YouTube object set up, you’re ready to start looking at different media streams for the video, which is discussed in the next section.

    © Copyright Revision a32fff39 .

    Источник

    How to Build a Python Program to Download YouTube Videos

    David Fagbuyiro

    David Fagbuyiro

    How to Build a Python Program to Download YouTube Videos

    YouTube is a well-known internet video streaming service. There are millions of videos in categories such as education, entertainment, and travel.

    You can quickly watch videos with a few mouse clicks, but downloading videos is difficult. But in a recent upgrade, YouTube now allows you to save videos in its download folder for offline viewing. Still, you are unable to save them locally.

    In this tutorial, you will learn how to use Python code to download YouTube videos. As you may know, one of Python’s great strengths is its huge number of modules and libraries. We will write the Python script using the popular pytube package.

    Prerequisites

    Below are the basic requirements to proceed with this tutorial:

    • Understanding of the Python Programming language
    • You must have Python 3+ installed on your computer
    • You must have installed the Python library Pytube
    • You should have a Python code editor such as Pycharm, Vscode, and so on.

    Pytube Overview and Installation

    Pytube is a small, dependency-free Python module for accessing videos from the internet.

    The native library is not pytube – you must first install it to be able to use it. When you have pip, installation is simple.

    To install Pytube using pip, you will need to open your command prompt CLI as an administrator and enter the following command:

    The pytube library improves video downloads. Build the YouTube module’s object by supplying the URL as a parameter. Then, obtain the video’s proper extension and resolution. You can change the name of the file at your leisure – otherwise, the original name will be retained.

    Now let’s get to the main aspect of writing and implementing the code to download our favorite videos from YouTube.

    from pytube import YouTube def Download(link): youtubeObject = YouTube(link) youtubeObject = youtubeObject.streams.get_highest_resolution() try: youtubeObject.download() except: print("An error has occurred") print("Download is completed successfully") link = input("Enter the YouTube video URL: ") Download(link) 

    You use the from pytube import YouTube function to import the Python Pytube library before continuing with the other aspects. Then you define the function download link.

    The youtubeObject = youtubeObject.streams.get_highest_resolution() command will automatically download the highest resolution available.
    Then I implemented the Try and Except to return an error message if the download fails – else it will print out that the download is completed successfully.

    The Link function will ask for the preferred YouTube video link to download, then immediately after you hit the enter button, the video downloading will commence.

    The Output:

    s_2A6E5F7B9EF3D136021C2A8815B8956B830A35B9A863E60136A6FD8F4C45E374_1666119447422_pytube

    The video I downloaded was successful. You can see the video in the same Python folder where the file you are working on is located. But if you wish, you can then move the video to your preferred storage location. In my case the video name is «Ronaldo celebrates with Antony.mp4.»

    However, it would be preferable if you had a reliable internet connection.

    This library also has numerous sophisticated features, but we have covered all of the major ones. You can learn more about the pytube library by visiting its official well-written documentation.

    Conclusion

    We have successfully built a YouTube video downloader script of our own in Python. This helps you avoid the stress of looking for an external website or application to get your preferred video to your local storage.

    It also saves you from having to expose your data on a third-party website or phishing link – all in the name of getting a video from YouTube to your local storage.

    Hopefully after going through this article, you will understand the process required to download videos from YouTube without the need to download an external application or visit any website you don’t trust.

    Источник

    Читайте также:  Css за пределы div
Оцените статью