Playing Youtube Video using Python
Last Updated :
29 Aug, 2020
In this article, we will see how we can play youtube video in python. In order to play youtube videos in python we need pafy and vlc module.
Pafy is a Python library to download YouTube content and retrieve metadata. Below is the command to install pafy
pip install pafy
VLC : is a python library to use the functionality of the vlc media player. In order to use the vlc module in python the user system should have a compatible version of VLC player as well. Below is the command to install vlc module
pip install python-vlc
Steps for implementation :
1. Import the pafy and vlc module
2. Create a variable having URL of the video
3. Create a pafy object using the link
4. Get the best quality stream of the given youtube link
5. Create a vlc MediaPlayer object by passing the best Stream
6. Play the video
Below is the implementation
import vlc
import pafy
video = pafy.new(url)
best = video.getbest()
media = vlc.MediaPlayer(best.url)
media.play()
|
Output :
Another example
import vlc
import pafy
video = pafy.new(url)
best = video.streams[ 0 ]
media = vlc.MediaPlayer(best.url)
media.play()
|
Output :
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...