Chapter 20: YouTube Media/Audio Download using Python

This tutorial will help you download youtube video or audio with python using pafy library. Pafy library is used to retrieve YouTube content and metadata.

Features of Pafy

(i) Retrieve metadata such as viewcount, duration, rating, author, thumbnail, keywords.
(ii) Download video or audio at requested resolution / bitrate / format / filesize
(iii) Command line tool (ytdl) for downloading directly from the command line
(iv) Download video only (no audio) in m4v or webm format
(v) Download audio only (no video) in ogg or m4a format
(vi) Works with Python 2.6+ and 3.3+
(vii) Optionally depends on youtube-dl (recommended; more stable)

Installation

virtualenv venv

pip install pafy

Library Imported

import pafy

Example1:

Retrieve metadata such as viewcount, duration, rating, author, description.

import pafy  

# url of video 
url = "<https://www.youtube.com/watch?v=Ns4LCeeOFS4&t=320s>"  

# instant created 
video = pafy.new(url)  

# print title 
print(video.title)  

# print rating 
print(video.rating)

# print viewcount 
print(video.viewcount)  

# print author & video length 
print(video.author, video.length) 

# print duration, likes, dislikes & description 
print(video.duration, video.likes, video.dislikes, video.description)

Output:

Dynamic Programming \| Set 3 (Longest Increasing Subsequence)

4.30275249481

57739

Example2:

Download best resolution video regardless of extension

import pafy
url = "<https://www.youtube.com/watch?v=eACohWVwTOc>" 
video = pafy.new(url)  
streams = video.streams for i in streams:     
print(i) 

# get best resolution regardless of format 
best = video.getbest()  
print(best.resolution, best.extension)  

# Download the vi 
best.download()

Output:

normal:3gp\@176x144

normal:3gp\@320x180

normal:webm\@640x360

normal:mp4\@640x360

normal:mp4\@1280x720

1280x720 mp4

25, 707, 969 Bytes [100.00%] received. Rate: [ 506 KB/s]. ETA: [0 secs]

Download video of a particular format specified (let say .3gp)

import pafy  
url = "<https://www.youtube.com/watch?v=eACohWVwTOc>" 
video = pafy.new(url)  
streams = video.streams for i in streams:     
print(i) 

# get best resolution of a specific format 
# set format out of(mp4, webm, flv or 3gp) 
best = video.getbest(preftype ="3gp")  
best.download()

Output:

normal:3gp\@176x144

normal:3gp\@320x180

normal:webm\@640x360

normal:mp4\@640x360

normal:mp4\@1280x720

6, 049, 643 Bytes [100.00%] received. Rate: [ 241 KB/s]. ETA: [0 secs]

Example4:

Download a specific format audio.

import pafy  
url = "<https://www.youtube.com/watch?v> =eACohWVwTOc" 
video = pafy.new(url)  
audiostreams = video.audiostreams 
for i in audiostreams:     
print(a.bitrate, i.extension, i.get_filesize())  
audiostreams[3].download()

Output:

160k webm 1365668

160k webm 1811815

160k m4a 3470205

160k webm 3301003

160k webm 3588746

Example5:

Download the bestaudio

import pafy  
url = "<https://www.youtube.com/watch?v=eACohWVwTOc>" 
video = pafy.new(url)  
bestaudio = video.getbestaudio() 
bestaudio.download()

References:

https://pypi.python.org/pypi/pafy

MD5 hash in Python

Cryptographic hashes are used in day-day life like in digital signatures, message authentication codes, manipulation detection, fingerprints, checksums (message integrity check), hash tables, password storage and much more. They are also used in sending messages over network for security or storing messages in databases.

There are many hash functions defined in the “hashlib” library in python.
This article deals with explanation and working of MD5 hash.

MD5 Hash

This hash function accepts sequence of bytes and returns 128 bit hash value, usually used to check data integrity but has security issues.

Functions associated :

  • encode() : Converts the string into bytes to be acceptable by hash function.
  • digest() : Returns the encoded data in byte format.
  • hexdigest() : Returns the encoded data in hexadecimal format.

The below code demonstrates the working of MD5 hash accepting bytes and output as bytes.

# Python 3 code to demonstrate the 
# working of MD5 (byte - byte)  
import hashlib  

# encoding GeeksforGeeks using md5 hash 
# function 
result = hashlib.md5(b'GeeksforGeeks')  

# printing the equivalent byte value. 
print("The byte equivalent of hash is : ", end ="") 
print(result.digest())

Output:

The byte equivalent of hash is : b'\\xf1\\xe0ix\~\\xcetS\\x1d\\x11%Y\\x94\\\\hq'

Explanation : The above code takes byte and can be accepted by the hash function. The md5 hash function encodes it and then using digest(), byte equivalent encoded string is printed.

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?