Monday 10 October 2016

Download a YouTube video with Python pytube module !!

First we need to do basic setup of python and the "pytube" module installation.

For basic python setup see the following video's:
Installation of pytube module using pip:

For installing a python module see the following video : https://www.youtube.com/watch?v=5zEQaYBukz4

pip install pytube

Note:
Seems there is an error with the new update of pytube when we try executing below command,

w = YouTube("https://www.youtube.com/watch?v=HNKQev4KLGE")

error seems some thing like this,

pytube.exceptions.RegexMatchError: regex pattern (\W['"]?t['"]?: ?'"['"]) had zero matches

so try the below command for pytube installation which will give the latest master merge of pytube,

pip install -U pytube

Now the code required to download a you tube video:

from pytube import YouTube

# not necessary, just for demo purposes.
from pprint import pprint

w = YouTube("https://www.youtube.com/watch?v=HNKQev4KLGE")

# Once set, you can see all the codec and quality options YouTube has made
# avaiilable for the particular video by printing videos.

# printing in normal method. 
w.get_videos()

# [<Video: MPEG-4 Visual (.3gp) - 144p - Simple>, <Video: MPEG-4 Visual (.3gp) - 240p - Simple>, <Video: H.264 (.mp4) - 360p - Baseline>, <Video: H.264 (.mp4) - 720p - High>, <Video: VP8 (.webm) - 360p - N/A>]

# Printing by using pprint module.
pprint(w.get_videos())

 #[<Video: MPEG-4 Visual (.3gp) - 144p - Simple>,
 # <Video: MPEG-4 Visual (.3gp) - 240p - Simple>,
 # <Video: H.264 (.mp4) - 360p - Baseline>,
 # <Video: H.264 (.mp4) - 720p - High>,
 # <Video: VP8 (.webm) - 360p - N/A>]

# The filename is automatically generated based on the video title.  You
# can override this by manually setting the filename.

# view the auto generated filename:
print(w.filename)

# Pulp Fiction - Dancing Scene [HD]

# set the filename:
w.set_filename('python installation')

# You can also filter the criteria by filetype.
pprint(w.filter('3gp'))

#[<Video: MPEG-4 Visual (.3gp) - 144p - Simple>,
# <Video: MPEG-4 Visual (.3gp) - 240p - Simple>]

# Notice that the list is ordered by lowest resolution to highest. If you
# wanted the highest resolution available for a specific file type, you
# can simply do:
print(w.filter('mp4')[-1])
# <Video: H.264 (.mp4) - 720p>

# You can also get all videos for a given resolution
pprint(w.filter(resolution='480p'))

# [<Video: H.264 (.flv) - 480p>,
#  <Video: VP8 (.webm) - 480p>]

# To select a video by a specific resolution and filetype you can use the get
# method.

video = w.get('mp4', '720p')

# NOTE: get() can only be used if and only if one object matches your criteria.
# for example:

pprint(w.videos)

#[<Video: MPEG-4 Visual (.3gp) - 144p>,
# <Video: MPEG-4 Visual (.3gp) - 240p>,
# <Video: Sorenson H.263 (.flv) - 240p>,
# <Video: H.264 (.flv) - 360p>,
# <Video: H.264 (.flv) - 480p>,
# <Video: H.264 (.mp4) - 360p>,
# <Video: H.264 (.mp4) - 720p>,
# <Video: VP8 (.webm) - 360p>,
# <Video: VP8 (.webm) - 480p>]

# Since we have two H.264 (.mp4) available to us... now if we try to call get()
# on mp4...

video = w.get('mp4')
# MultipleObjectsReturned: 2 videos met criteria.

# In this case, we'll need to specify both the codec (mp4) and resolution
# (either 360p or 720p).

# Okay, let's download it! (a destination directory is required)

# Destination for unix platform users.
video.download('/tmp/')

# Destination for windows platform users.
video.download('C:\Python27\files')

Note:

Keep seeing my blog for latest methods and downloading a playlist of you tube videos using pytube module.

2 comments:

  1. Can I add more? I usually use to download Youtube video is to use Youdowloader.net. They offer free service without registration.

    ReplyDelete
  2. Tubemate is the best application to download free videos from Youtube. Tubemate is published for free by Devian Studio. Home: https://devianstudio.net/

    ReplyDelete