2

I am trying to make an audio-only player using python for a small project. The script I am using is as follows:

#!/usr/bin/env python                                                                                                                          
import re                                                                                                                                      
import sys                                                                                                                                     
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()                                                                                                                                                                                                                                
while True: pass

Now, this script works great on my work machine running manjaro and the following python version:

Python 3.7.2 (default, Jan 10 2019, 23:51:51)

The machine I plan to run this script is a raspberry pi zero W running raspbian stretch and I set it to run this python version:

Python 3.5.3 (default, Sep 27 2018, 17:25:39) 

When I run this script on the raspberry pi I get nothing and when I stop it I get the following messages:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/youtube_dl/extractor/__init__.py", line 4, in <module>
    from .lazy_extractors import *
ImportError: No module named 'youtube_dl.extractor.lazy_extractors'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "box.py", line 4, in <module>
    import pafy
  File "/usr/local/lib/python3.5/dist-packages/pafy/__init__.py", line 7, in <module>
    from .pafy import new
  File "/usr/local/lib/python3.5/dist-packages/pafy/pafy.py", line 48, in <module>
    import youtube_dl
  File "/usr/local/lib/python3.5/dist-packages/youtube_dl/__init__.py", line 43, in <module>
    from .extractor import gen_extractors, list_extractors
  File "/usr/local/lib/python3.5/dist-packages/youtube_dl/extractor/__init__.py", line 9, in <module>
    from .extractors import *
  File "/usr/local/lib/python3.5/dist-packages/youtube_dl/extractor/extractors.py", line 732, in <module>
    from .newgrounds import (
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 954, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 896, in _find_spec
  File "<frozen importlib._bootstrap_external>", line 1147, in find_spec
  File "<frozen importlib._bootstrap_external>", line 1121, in _get_spec
  File "<frozen importlib._bootstrap_external>", line 1229, in find_spec
  File "<frozen importlib._bootstrap_external>", line 82, in _path_stat
KeyboardInterrupt

Running the commands one by one, I think I found the problem with the vlc module. When the script reaches the following command:

player=vlc.MediaPlayer(playurl)

I get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'vlc' has no attribute 'MediaPlayer'

For reference I used pip3 to install python-vlc, pafy and youtube_dl modules.

This is my first ever experience with Python. I got this far by reading from several posts on here and other sites. This completely confuses me and I have no idea what to do to make it work.

It is entirely possible that there is a problem with the python installation on raspbian (I am using a completely fresh install, only last night I reinstalled it again!). The only thing I added to the fresh raspbian install was to update the system, install git and a few other programs.

Can someone please help me out?

skaul05
  • 2,154
  • 3
  • 16
  • 26
lucian
  • 350
  • 4
  • 18

2 Answers2

2

The problem seems to be related to two versions of vlc package (32 bit vs 64bit). There are two ways to install it as well: python-vlc vs vlc. Please check which system version you have and install the correct package version for it. You may still experience the same problem I had where I was missing some DLLs.

I hope the following links will help:

Python vlc install problems

Error when importingPython-vlc

Import Vlc module in python

  1. pip install vlc does not have the MediaPlayer class
  2. pip install python-vlc has the MediaPlayer class but gives a DLL lib error
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
naivepredictor
  • 898
  • 4
  • 14
  • I did install it yes and I hope it did it right: with ``$ sudo pip3 install python-vlc Requirement already satisfied: python-vlc in /usr/local/lib/python3.5/dist-packages``. I ran your code and got the same error message. – lucian Feb 27 '19 at 09:49
  • I am experiencing same problem regardless the fact vlc is installed in my Python environment I can not import vlc at all. – naivepredictor Feb 27 '19 at 09:55
  • what is really weird is that on my work computer I had no problem! could it be the python version? I am running manjaro here and it is bleeding edge as they say. ``Python 3.7.2 (default, Jan 10 2019, 23:51:51) [GCC 8.2.1 20181127] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import vlc >>> player = vlc.MediaPlayer() >>> ' – lucian Feb 27 '19 at 10:06
  • ok proggress! from the first link you sent, I noticed the last comment which said that you also need a functioning vlc installation on the system for the module to work. I installed it and now I don't get that error when I try it with python 2.7. It only appears for python3... – lucian Feb 27 '19 at 10:28
0

For simplicity, place the vlc.py program in the same directory as your program.
Then this is the simplest form of getting vlc to play something

url = "file:///home/rolf/GWPE.mp4"

import vlc

playing = set([1,2,3,4])
instance=vlc.Instance()
player=instance.media_player_new()
player.set_mrl(url)
player.play()
while True:
    state = player.get_state()
    if state not in playing:
        break

Not sure what the pafy part is about but the above will play a local file and I suspect whatever pafy passes to it.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • from what I understand ``pafy`` is used to extract the audio link from a youtube link which you can then pass to ``vlc`` and play it as audio from the python script... I will try as you suggest. Thank you! – lucian Feb 27 '19 at 10:30