Skip to content
Related Articles

Related Articles

Pafy – Getting Media Type of Stream
  • Last Updated : 20 Jul, 2020
GeeksforGeeks - Summer Carnival Banner

In this article we will see how we can get media type of the given youtube video stream in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. Stream is basically available resolution of video is available on youtube. Media type of stream can be normal, audio or video here normal means a stream containing both video and audio.

We can get the pafy object with the help of new method and with the help of allstreams attribute we can get the all the streams available for the video, below is the command to get the pafy object for given video

video = pafy.new(url)
streams = video.allstreams

The video url should exist on youtube as it get the information of those videos which are present on the youtube. YouTube is an American online video-sharing platform.

In order to do this we use mediatype attribute with the pafy stream object of video

Syntax : stream.mediatype



Argument : It takes no argument

Return : It returns string

Below is the implementation




# importing pafy
import pafy 
    
# url of video 
url = "https://www.youtube.com / watch?v = vG2PNdI8axo"
    
# getting video
video = pafy.new(url) 
  
# getting all the available streams
streams = video.allstreams
  
# selecting one stream
stream = streams[1]
  
# getting media tyope of stream
value = stream.mediatype
  
# printing the value
print("Media Type : " + str(value))

Output :

Media Type : audio

Another example




# importing pafy
import pafy 
    
# url of video 
url = "https://www.youtube.com / watch?v = i6rhnSoK_gc"
    
# getting video
video = pafy.new(url) 
  
# getting all the available streams
streams = video.allstreams
  
# selecting one stream
stream = streams[4]
  
# getting media tyope of stream
value = stream.mediatype
  
# printing the value
print("Media Type : " + str(value))

Output :

Media Type : video

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :