I am trying to write a small python program to play audio only from youtube links. The idea is that I want to make a small player for my son that doesn't need a tv, just some speakers. I can create playlists for him and then load them up in this player and there will be music but no image. But since I am really new to python I am having some trouble:
After some research I settled on vlc and pafy.
First I tried with vlc only, but the python-vlc
module seems to play the video too without other arguments like the --no-video
command line argument that can be passed to vlc to only listen to the audio stream.
Then I read about pafy
that can extract the audio url and can be passed to vlc. I have tried it using this code but I get no audio. The code runs for a few seconds then it stops.
import pafy
import vlc
#
#
url = "https://www.youtube.com/watch?v=G0OqIkgZqlA"
video = pafy.new(url)
best = video.getbestaudio()
playurl = best.url
player = vlc.MediaPlayer(playurl)
player.play()
What am I doing wrong? Also can this be achieved with python2.7 only? I mean no python 3 stuff.