Search code examples
pythonwindowstkintervlc

Can not import vlc to python program


I'm trying to reproduce a video with sound in python Tkinter through VLC, but cannot run the file since I get an error when running the line import vlc.

I have python 3 at 32 bits, VLC player at 32 bits and installed it through pip install python-VLC, which was successful and then try to run the code and get this error:

File "C:/Users/momoh/Documents/GitHub/CNDH/CNDH.py", line 5, in <module>
    import vlc
  File "C:\Users\momoh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vlc.py", line 207, in <module>
    dll, plugin_path  = find_lib()
  File "C:\Users\momoh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vlc.py", line 163, in find_lib
    dll = ctypes.CDLL(libname)
  File "C:\Users\momoh\AppData\Local\Programs\Python\Python36-32\lib\ctypes\__init__.py", line 348, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found 

This is the code that I´m tryin to run: My imports:

import sys
import tkinter as tk           
from tkinter import font  as tkfont 
from PIL import ImageTk, Image
import vlc

and the class for the frame that has the video

class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.grid_columnconfigure(0, weight=0)
        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure(2, weight=2)
        self.grid_columnconfigure(3, weight=1)
        self.grid_columnconfigure(4, weight=0)
        self.grid_rowconfigure(0, weight=0)
        self.grid_rowconfigure(1, weight=1)
        self.grid_rowconfigure(2, weight=2)
        self.grid_rowconfigure(3, weight=3)
        self.grid_rowconfigure(4, weight=2)
        self.grid_rowconfigure(5, weight=1)
        self.grid_rowconfigure(6, weight=6)

        self.configure(background="white")


        # Open the video source |temporary
        self.video_source = "assetsCNDH/prueba.mp4"

        # Canvas where to draw video output
        self.canvas = tk.Canvas(self, width= controller.winfo_screenwidth(),
                                height=controller.winfo_screenheight(), bg="black",
                                    highlightthickness=0)
        self.canvas.pack()

        # Creating VLC player
        self.instance = vlc.Instance()
        self.player = self.instance.media_player_new()

    def GetHandle(self):
        # Getting frame ID
        return self.winfo_id()

    def play(self, _source):
        # Function to start player from given source
        Media = self.instance.media_new(_source)
        Media.get_mrl()
        self.player.set_media(Media)

        # self.player.play()
        self.player.set_hwnd(self.GetHandle())
        self.player.play()

How do I get my video running?


Solution

  • I ended up using subprocess to run VLC through the CLI.

    import subprocess
    
    
    comand = 'vlc --play-and-exit --no-video-deco --no-embedded-video -f --one-instance --no-playlist-enqueue  assetsCNDH\\directorio.mp4'
    subprocess.run(comand, shell=True)