How to play video with python-vlc?

5.2k Views Asked by At

I would like to play video with python-vlc module. I wrote the following code. My computer is MacOS Catalina.

#!/usr/bin/env python3
import vlc

p = vlc.MediaPlayer("mediafile.mp4")
p.play()
while True:
    pass

But the python3 interpreter threw the following errors.

[00007f89b9661950] caopengllayer vout display error: No drawable-nsobject found!
[00007f89b9661950] macosx vout display error: No drawable-nsobject nor vout_window_t found, passing over.
[00007f89b30530f0] main video output error: video output creation failed
[00007f89b9650c00] main decoder error: failed to create video output
[h264 @ 0x7f89b407c000] get_buffer() failed
[h264 @ 0x7f89b407c000] thread_get_buffer() failed
[h264 @ 0x7f89b407c000] decode_slice_header error
[h264 @ 0x7f89b407c000] no frame!

I guessed that this code didn't make a frame displaying the video. It'll be a main cause of this error, I think. However, I can not come up with a solution of this problem. Please tell me how to play video with python-vlc module!!

2

There are 2 best solutions below

1
On

I think this will work:

import vlc
 
media = vlc.MediaPlayer("1.mp4")
 
media.play()

It just takes a media filename for you. Make sure the video is in the same folder as the script.

0
On

I think the possible reason is, it requires an active hwnd where it can show the video. So you need an GUI and set hwnd to that player.

Here is my code for Tkinter Window.You can also check out different GUI based example From Github

import vlc
from tkinter import *
root=Tk()
instance=vlc.Instance()
p=instance.media_player_new()
p.set_hwnd(root.winfo_id())
p.set_media(instance.media_new(path_to_media))
p.play()
root.mainloop()

And if you r using Mac ,then as per the example you should use instaed of this line

p.set_hwnd(root.winfo_id())

try:
        libtk = 'libtk%s.dylib' % (Tk.TkVersion,)
        prefix = getattr(sys, 'base_prefix', sys.prefix)
        libtk = joined(prefix, 'lib', libtk)
        dylib = cdll.LoadLibrary(libtk)
        # getNSView = dylib.TkMacOSXDrawableView is the
        # proper function to call, but that is non-public
        # (in Tk source file macosx/TkMacOSXSubwindows.c)
        # and dylib.TkMacOSXGetRootControl happens to call
        # dylib.TkMacOSXDrawableView and return the NSView
        _GetNSView = dylib.TkMacOSXGetRootControl
        # C signature: void *_GetNSView(void *drawable) to get
        # the Cocoa/Obj-C NSWindow.contentView attribute, the
        # drawable NSView object of the (drawable) NSWindow
        _GetNSView.restype = c_void_p
        _GetNSView.argtypes = c_void_p,
        del dylib

    except (NameError, OSError):  # image or symbol not found
        def _GetNSView(unused):
            return None
        libtk = "N/A"

h = root.winfo_id()  # .winfo_visualid()?
                # XXX 1) using the videopanel.winfo_id() handle
                # causes the video to play in the entire panel on
                # macOS, covering the buttons, sliders, etc.
                # XXX 2) .winfo_id() to return NSView on macOS?
v= _GetNSView(h)
   if v:
      p.set_nsobject(v)
   else:
      p.set_xwindow(h)# plays audio, no video