2

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.

5
  • add while True: pass after of player.play() Commented Feb 25, 2019 at 9:05
  • I am sorry but I do not understand... Can you please elaborate a bit? I am really really new to python... Commented Feb 25, 2019 at 9:06
  • 1
    What part of my comment is not clear ?, just add while True: pass at the end of your code Commented Feb 25, 2019 at 9:08
  • Ok sorry, I just don't understand why, but I will try it! Thank you Commented Feb 25, 2019 at 9:09
  • 1
    great stuff! it works! thanks, I'll investigate the rest in the documentation Commented Feb 25, 2019 at 10:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.