etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

Python YouTube Downloader Script

This website contains lots of Python developer resources.There are many Python source code available that you can directly implement in your website applications. In this article, you will learn about a YouTube Python downloader script that helps you to download YouTube videos.

YouTube is the most famous video sharing software on the world. It encourages clients to upload, view, and share videos. Huge amounts of individuals chose YouTube video making as full work, as they make and upload recordings and getting income. The recordings are viewed on the web and YouTube didn't give any download choice to offline users. On the off chance that you need to get to the document of the YouTube video, the video should be downloaded. Here, we have mentioned the step by step process to create YouTube downloader script using Python.

Python provides pytube module that helps in downloading YouTube videos. So, open your terminal window and run the following command to install this module using pip tool -

pip install pytube

If the above command is not working or return error on execution, uninstall it and run the following command -

pip install pytube3
Install PyTube

Once the pytube module is installed, create a python file and import the module at the top of the script -

from pytube import YouTube

Next, for accessing the features of this module, we need to initialize the class and load the URL as an argument in the YouTube() function -

pytube.YouTube(url)




Pytube Simple Example

Here, we have taken a very simple example to download a YouTube video. The download() function accepts different parameters to set the video file location and name -

import pytube 

url = 'https://www.youtube.com/watch?v=acvdFEOy-EY'

youtube = pytube.YouTube(url)
video = youtube.streams.first()
video.download('/video', 'phpinterview')


Pytube get video information

This library provides attributes to get video information, like we can get video id, video title, and so on -

import pytube 

url = 'https://www.youtube.com/watch?v=acvdFEOy-EY'

youtube = pytube.YouTube(url)

print(youtube.title) # Title
print(youtube.video_id) # Id
print(youtube.age_restricted) # Age

The above code returns the following output -

Pytube example

Pytube Streams Format

The YouTube object opens different streams from the YouTube video URL. The given example returns all the stream information of the video -

import pytube 

url = 'https://www.youtube.com/watch?v=EY1SKJQ2rTU&t=8s'

youtube = pytube.YouTube(url)

stream = youtube.streams.all()
for i in stream:
  print(i)

It returns the following output -

<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">
<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">
<Stream: itag="137" mime_type="video/mp4" res="1080p" fps="30fps" vcodec="avc1.64001e" progressive="False" type="video">
<Stream: itag="248" mime_type="video/webm" res="1080p" fps="30fps" vcodec="vp9" progressive="False" type="video">
<Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d4016" progressive="False" type="video">
<Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9" progressive="False" type="video">
<Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d4014" progressive="False" type="video">
<Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9" progressive="False" type="video">
<Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9" progressive="False" type="video">
<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2" progressive="False" type="audio">
<Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus" progressive="False" type="audio">

The "progressive" stream contains the file having both audio and video and the "adaptive" steam contains either audio or video. The "mime_type", "res", and "fps" attributes can be used to filter the stream of the downloaded video.





Pytube Filter Streams

We can use the filter() method to filter only specific streams. It is helpful when we want to download all the different resolutions of YouTube videos-

import pytube 

url = 'https://www.youtube.com/watch?v=EY1SKJQ2rTU&t=8s'

youtube = pytube.YouTube(url)

stream = youtube.streams.filter(progressive=True, file_extension='mp4')
for i in stream:
  print(i)

It returns the following -

(env) c:\python37\Scripts\projects>youtube.py
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2" progressive="True" type="video">
<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2" progressive="True" type="video">


Pytube download high resolution video

Pytube provides get_highest_resolution() method to download high resolution video. In this given example, we have also used exception handling -

import pytube 

url = 'https://www.youtube.com/watch?v=EY1SKJQ2rTU&t=8s'

try:
  youtube = pytube.YouTube(url)
  filters = youtube.streams.filter(progressive=True, file_extension='mp4')

  # high resolution video
  filters.get_highest_resolution().download()
  print('Successfully downloaded')
except Exception as e:
  print(e)




Related Articles

Python Spell Checker Program
Python remove punctuation from string
How to convert Excel to CSV Python Pandas
How to read data from excel file using Python Pandas
How to read data from excel file in Python
Python read JSON from URL requests
Python send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml




Most Popular Development Resources
Characteristics of a Good Computer Program
-----------------
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
How to get data from XML file in PHP
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP Create Word Document from HTML
-----------------
PHP code to send email using SMTP
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
How to get current directory, filename and code line number in PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
PHP MYSQL Advanced Search Feature
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
Simple pagination in PHP with MySQL
-----------------
Fibonacci Series Program in PHP
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
How to add multiple custom markers on google map
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
Php file based authentication
-----------------
PHP Secure User Registration with Login/logout
-----------------
jQuery File upload progress bar with file size validation
-----------------
Polling system using PHP, Ajax and MySql
-----------------
Simple PHP File Cache
-----------------
SQL Injection Prevention Techniques
-----------------
Simple File Upload Script in PHP
-----------------
CSS Simple Menu Navigation Bar
-----------------
PHP User Authentication by IP Address
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
How to generate QR Code in PHP
-----------------
Calculate the distance between two locations using PHP
-----------------
Simple way to send SMTP mail using Node.js
-----------------
Detect Mobile Devices in PHP
-----------------
Set and Get Cookies in PHP
-----------------
To check whether a year is a leap year or not in php
-----------------
PHP Server Side Form Validation
-----------------
Date Timestamp Formats in PHP
-----------------
Get Visitor\'s location and TimeZone
-----------------
Simple Show Hide Menu Navigation
-----------------
Convert MySQL to JSON using PHP
-----------------
PHP Programming Error Types
-----------------
PHP Sending HTML form data to an Email
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
How to print specific part of a web page in javascript
-----------------
Google Street View API Example
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
How to add google map on your website and display address on click marker
-----------------
PHP Getting Document of Remote Address
-----------------
PHP Connection and File Handling on FTP Server
-----------------
File Upload Validation in PHP
-----------------
R Plot Types
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial




General Knowledge

listen
listen
listen
listen
listen
listen
listen
listen
listen


Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 27

    Best AI Startups In India

    Artificial Intelligence is a process of making an intelligent computer machine that does tasks intelligently...

  • Jan 23

    Most in demand programming languages for 2019

    In this article, we have mentioned the analyzed results of the most in demand programming language for 2019...

  • Jan 15

    Web Robots

    Web robots is an internet robot or simply crawlers, or spiders and do not relate this with hardware robots...

  • Jan 12

    Most in demand NoSQL databases software for 2019

    In this article, we have mentioned the analyzed result of most in demand NoSQL database softwares for 2019...

  • Jan 10

    Kotlin : Android App Development Choice

    Kotlin is a general-purpose open-source programming language. It runs on the JVM and its syntax is much like Java...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2021. All Rights Reserved.