2

I'm trying to make a simple script that will play a video (.mp4) in python. I don't want to play a you tube video or anything online, just a video on my computer.

Here's my code:

import vlc
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new('test.mp4')
Media.get_mrl()
player.set_media(Media)
player.play()

I keep getting this error when I run it (talking about line 3):

AttributeError: 'NoneType' object has no attribute 'media_player_new'

I'm using python 3.5.4 in the IDLE on macOS Sierra.

2
  • 1
    I could imagine that there is a conflict with how you name your objects. As a rule of thumb, never use upper-case letters for objects or variables (only for classes). In line 2 change Instance to vlc_instance or similar and rename in the other lines as well. Commented Sep 21, 2017 at 14:42
  • that didn't seem to work Commented Sep 21, 2017 at 15:38

5 Answers 5

1

I solved the problem by comment out find_lib() in vlc.py.

p = os.getcwd()
os.chdir(os.path.join(p, 'sdk'))
dll = ctypes.CDLL("libvlc.dll")
plugin_path = os.path.join(os.getcwd(), r'sdk\plugins')
Sign up to request clarification or add additional context in comments.

Comments

0

You are not holding the instance open

import vlc
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new('2005.mp3')
Media.get_mrl()
player.set_media(Media)
player.play()
while player.get_state() !=6:
    continue

Should work or
try this:

import vlc
import time
import sys

def progressbar(progress,guage_length=50):
    div = 100 / float(guage_length)
    prog = int(progress / div) # ensure progress fits in guage
    text = "\rPlaying: {0}{1}{2} [{3}%]".format(">"*prog,"|","-"*(guage_length - prog),format(progress,'.2f'))
    sys.stdout.write(text)
    sys.stdout.flush()

instance = vlc.Instance()
player = instance.media_player_new()
player.set_mrl("V2.mp4")
player.play()
playing = set([1,2,3,4])
play=True
guage_length=30
while play == True:
    time.sleep(0.5)
    play_state = player.get_state()
    if play_state in playing:
        length = player.get_length()
        ptime = player.get_time()
        progress = ptime/float(length)*100
        progressbar(progress,guage_length)
        continue
    else:
        progressbar(100,guage_length)
        play = False
print ("\nPlay terminated")

Note that # State 0: None,1 Opening,2 Buffering,3 Playing,4 Paused,5 Stopped,6 Ended,7 Error

Caveat: tested on Linux only

Comments

0

You could simplify your code using simply

import vlc
p = vlc.MediaPlayer('2005.mp3')
p.play()

but this would not solve your problem. Apparently, the vlc instance is not correctly created. This can be caused by a variety of issues. Use

i = vlc.Instance('--verbose 3')

to see possible error messages.

Comments

0

I got this error message on a Raspberry Pi Zero with Raspbian Buster Lite. I searched a long time for the answer but suddenly I noticed the remark on the pypi project page of python-vlc:

Note that it relies on an already present install of VLC.

So installing vlc with sudo apt install vlc solved the problem!

Comments

0

The fix of AttributeError: 'NoneType' object has no attribute 'media_player_new' for onefile for windows is --add-data "./plugins/;./plugins/" make sure that plugins is in the same path as main.py

Make sure to copy plugins folder from the VLC binary path before launching the command also don't forget to add DLLs files too with :

--add-data "libvlc.dll;." --add-data "libvlccore.dll;." --add-data "axvlc.dll;." --add-data "npvlc.dll;."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.