oaubert / python-vlc

Python vlc bindings
GNU Lesser General Public License v2.1
391 stars 111 forks source link

vlc.Instance() returns None on MacOS Silicon #274

Closed kravlad closed 6 months ago

kravlad commented 6 months ago

I try to execute simple code:

import vlc

instance = vlc.Instance()

but instance is always None

output of python vlc.py -v:

vlc.py: 3.0.20123 (Sat Nov  4 17:10:43 2023 3.0.20)
libVLC: 3.0.16 Vetinari (0x3001000)
plugins: /Applications/VLC.app/Contents/MacOS/plugins
Python: 3.10.14 (64bit) macOS 14.4.1 arm64

what the problem could be? Thanks in advance.

mrJean1 commented 6 months ago

It looks like Python is built for Apple Si, native. Is VLC App app or is that an Intel build? Double check that both are built for the same target.

For example, Python 3.10.8 and VLC 3.0.18 both for Apple Si work fine (with some messages) using the examples tkvlc.py.

% python3.10 vlc.py -v
vlc.py: 3.0.16120 (Mon Feb 28 20:29:27 2022 3.0.16)
LibVLC version: 3.0.18 Vetinari (0x3001200)
LibVLC compiler: clang: warning: argument unused during compilation: '-mmacosx-version-min=10.7' [-Wunused-command-line-argument]
Plugin path: /Applications/VLC.app/Contents/MacOS/plugins
Python: 3.10.8 (64bit) macOS 14.4.1

[% python3.10 tkvlc.py 
2024-05-12 10:14:36.820 Python[39798:3441818] WARNING: Secure coding is not enabled for restorable state! Enable secure coding by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState: and returning YES.
[0000000121f08fc0] videotoolbox decoder: Using Video Toolbox to decode 'h264'
[0000000121f08fc0] videotoolbox decoder: vt cvpx chroma: 420v
kravlad commented 6 months ago

It looks like Python is built for Apple Si, native. Is VLC App app or is that an Intel build? Double check that both are built for the same target.

For example, Python 3.10.8 and VLC 3.0.18 both for Apple Si work fine (with some messages) using the examples tkvlc.py.

% python3.10 vlc.py -v
vlc.py: 3.0.16120 (Mon Feb 28 20:29:27 2022 3.0.16)
LibVLC version: 3.0.18 Vetinari (0x3001200)
LibVLC compiler: clang: warning: argument unused during compilation: '-mmacosx-version-min=10.7' [-Wunused-command-line-argument]
Plugin path: /Applications/VLC.app/Contents/MacOS/plugins
Python: 3.10.8 (64bit) macOS 14.4.1

[% python3.10 tkvlc.py 
2024-05-12 10:14:36.820 Python[39798:3441818] WARNING: Secure coding is not enabled for restorable state! Enable secure coding by implementing NSApplicationDelegate.applicationSupportsSecureRestorableState: and returning YES.
[0000000121f08fc0] videotoolbox decoder: Using Video Toolbox to decode 'h264'
[0000000121f08fc0] videotoolbox decoder: vt cvpx chroma: 420v

I have installed VLC Version 3.0.16 Vetinari (Apple Silicon) that's downloaded from official website.

mrJean1 commented 6 months ago

What does the command line show now ...

python3.10  vlc.py  -v

The instance should not be None.

% python3.10
Python 3.10.8 ... on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import vlc
>>> i = vlc.Instance()
>>> i
<vlc.Instance object at 0x101170250>
>>> 

Also, get the tkvlc.py example and try ...

python3.10  tkvlc.py  -v

Example ...

% python3.10 tkvlc.py -v
2024-05-12 14:42:13.337 Python[65746:3646528] WARNING: ... returning YES.
tkvlc.py: 22.12.28
tkinter: 8.6
libTk: /Library/Frameworks/Python.framework/Versions/3.10/lib/libtk8.6.dylib
is_Tk: aqua
vlc.py: 3.0.16120 (Mon Feb 28 20:29:27 2022 3.0.16)
LibVLC version: 3.0.18 Vetinari (0x3001200)
LibVLC compiler: clang: warning: ... [-Wunused-command-line-argument]
Plugin path: /Applications/VLC.app/Contents/MacOS/plugins
Python: 3.10.8 (64bit) macOS 14.4.1

Lastly, play a video ...

python3.10  tkvlc.py  <video_file_name>

Keep in mind, running ...

python3.10  vlc.py  <video_file_name>

will not play the video on macOS, only the sound. For more details, see the examples.

kravlad commented 6 months ago

What does the command line show now ...

python3.10  vlc.py  -v

The instance should not be None.

% python3.10
Python 3.10.8 ... on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import vlc
>>> i = vlc.Instance()
>>> i
<vlc.Instance object at 0x101170250>
>>> 

Also, get the tkvlc.py example and try ...

python3.10  tkvlc.py  -v

Example ...

% python3.10 tkvlc.py -v
2024-05-12 14:42:13.337 Python[65746:3646528] WARNING: ... returning YES.
tkvlc.py: 22.12.28
tkinter: 8.6
libTk: /Library/Frameworks/Python.framework/Versions/3.10/lib/libtk8.6.dylib
is_Tk: aqua
vlc.py: 3.0.16120 (Mon Feb 28 20:29:27 2022 3.0.16)
LibVLC version: 3.0.18 Vetinari (0x3001200)
LibVLC compiler: clang: warning: ... [-Wunused-command-line-argument]
Plugin path: /Applications/VLC.app/Contents/MacOS/plugins
Python: 3.10.8 (64bit) macOS 14.4.1

Lastly, play a video ...

python3.10  tkvlc.py  <video_file_name>

Keep in mind, running ...

python3.10  vlc.py  <video_file_name>

will not play the video on macOS, only the sound. For more details, see the examples.

python3.10 vlc.py -v shows exactly the same I texted in my first comment:

vlc.py: 3.0.20123 (Sat Nov  4 17:10:43 2023 3.0.20)
libVLC: 3.0.16 Vetinari (0x3001000)
plugins: /Applications/VLC.app/Contents/MacOS/plugins
Python: 3.10.14 (64bit) macOS 14.4.1 arm64

python3.10 tkvlc.py -v shows:

tkvlc.py: 22.12.28
tkinter: 8.6
libTk: /opt/homebrew/lib/libtk8.6.dylib
is_Tk: aqua, isAquaTk, isCocoaTk
vlc.py: 3.0.20123 (Sat Nov  4 17:10:43 2023 3.0.20)
libVLC: 3.0.16 Vetinari (0x3001000)
plugins: /Applications/VLC.app/Contents/MacOS/plugins
Python: 3.10.14 (64bit) macOS 14.4.1 arm64

and vlc.Instance() still returns None:

Python 3.10.14 (main, Mar 19 2024, 21:46:16) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import vlc
>>> i = vlc.Instance()
>>> print(i)
None
>>> 

As expected, python3.10 tkvlc.py rises an error:

Traceback (most recent call last):
  File "/python-vlc/examples/tkvlc.py", line 1307, in <module>
    player = Player(root, video=_video, debug=_debug)
  File "/python-vlc/examples/tkvlc.py", line 601, in __init__
    self.player = self.Instance.media_player_new()
AttributeError: 'NoneType' object has no attribute 'media_player_new'
mrJean1 commented 6 months ago

There may be more confusion. Since the tklib is the Homebrew one, the Python build should be Homebrew, but may not be. Type …

% python3.10
>>> import sys
>>> print(sys.executable)
kravlad commented 6 months ago

There may be more confusion. Since the tklib is the Homebrew one, the Python build should be Homebrew, but may not be. Type …

% python3.10
>>> import sys
>>> print(sys.executable)

It doesn't look that tklib can be a reason of this problem, because I create an instance of vlc.Instance separately of any frameworks.

Anyway, output of sys.executable:

>>> import sys
>>> print(sys.executable)
/opt/homebrew/opt/python@3.10/bin/python3.10
mrJean1 commented 6 months ago

Well, tklib may not be the issue, but Homebrew on Apple Si may. Both tkvlc.py and vlc.py haven been tested with Homebrew builds of Python and Tk/Tcl on Intel macOS using VLC for Intel. Especially example tkvlc.py was updated recently to handle the Homebrew specifics, see the tkvlc.py source.

Don't know about Homebrew builds for Apple Si. It could be a ctypes issue or something else, unrelated to Python, VLC and vlc.py (and Tk/Tcl and tkvlc.py). Standard Python works just fine with vlc.py and examples.

kravlad commented 6 months ago

I did where python3.10 and found out that there's three python 3.10 on my machine:

/opt/homebrew/bin/python3.10
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10
/usr/local/bin/python3.10

I tried to execute vlc.py by each one but no one interpreter works with vlc.Instance.

It's hard to believe that there's nobody who has the same problem, definitely I do something wrong :(

mrJean1 commented 6 months ago

FWIW, I just installed Python 3.12 from Homebrew, imported vlc.py and the vlc.Instance is fine, at least not None.

% /opt/local/bin/python3 
Python 3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import vlc
>>> i = vlc.Instance()
>>> i
<vlc.Instance object at 0x102d4ca70>

PS) The are quite a number SyntaxWarning: invalid escape sequence ... in vlc.py, but those have no effect.

kravlad commented 6 months ago

FWIW, I just installed Python 3.12 from Homebrew, imported vlc.py and the vlc.Instance is fine, at least not None.

% /opt/local/bin/python3 
Python 3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import vlc
>>> i = vlc.Instance()
>>> i
<vlc.Instance object at 0x102d4ca70>

PS) The are quite a number SyntaxWarning: invalid escape sequence ... in vlc.py, but those have no effect.

This is under intel chip or apple?

mrJean1 commented 6 months ago

macOS Sonoma 14.4.1 Apple M1 macBook Air

kravlad commented 6 months ago

macOS Sonoma 14.4.1 Apple M1 macBook Air

My mac is the same. If I understood correctly you built your own VLC from source, right?

mrJean1 commented 6 months ago

Also, tkvlc.py does not work since python 3.12.3 in Homebrew doesn't have the tkinter module even though tcl-tk 8.6.14 is installed in Homebrew. Correction: tkvlc.py works fine after brew install python-tk.

Finally, example cocoavlc.py does play a video but that is all. Other windows, dialogs, etc do not work on 'Homebrew'. Perhaps there is a ctypes issue, cocoavlc.py depends on PyCocoa which depends heavily on ctypes to get to a handful of macOS libraries.

With any standard Python build for Apple Si, cocoavlc.py works fine with the latest PyCocoa and VLC 3.0.18 or earlier.

mrJean1 commented 6 months ago

No. All VLC and Python are pre-built releases from their respective web-sites.

kravlad commented 6 months ago

No. All VLC and Python are pre-built releases from their respective web-sites.

Then it's really weird. I use the same Python and VLC builds on the same machine, but the behavior is different.

mrJean1 commented 6 months ago

VLC is the VLC app and Python is a std build? Not Homebrew?

In any case, get Python 3.11 or 3.12 from Python.org. Those install next to 3.10 and will not override the latter. Then, check the vlc.Instance, it shouldn’t be None.

kravlad commented 6 months ago

VLC is the VLC app and Python is a std build? Not Homebrew?

Yes, VLC for MacOs from here and Python from here

kravlad commented 6 months ago

VLC is the VLC app and Python is a std build? Not Homebrew?

In any case, get Python 3.11 or 3.12 from Python.org. Those install next to 3.10 and will not override the latter. Then, check the vlc.Instance, it shouldn’t be None.

I installed Python 3.11 from python.org and it didn't help.

mrJean1 commented 6 months ago

What is the output of …

python3.11  vlc.py  -v

python3.11  tkvlc.py  -v
kravlad commented 6 months ago

What is the output of …

python3.11  vlc.py  -v

python3.11  tkvlc.py  -v
% /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 vlc.py -v

vlc.py: 3.0.20123 (Sat Nov  4 17:10:43 2023 3.0.20)
libVLC: 3.0.16 Vetinari (0x3001000)
plugins: /Applications/VLC.app/Contents/MacOS/plugins
Python: 3.11.9 (64bit) macOS 14.4.1 arm64
% /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11 tkvlc.py -v

tkvlc.py: 22.12.28
tkinter: 8.6
libTk: /Library/Frameworks/Python.framework/Versions/3.11/lib/libtk8.6.dylib
is_Tk: aqua, isAquaTk, isCocoaTk
vlc.py: 3.0.20123 (Sat Nov  4 17:10:43 2023 3.0.20)
libVLC: 3.0.16 Vetinari (0x3001000)
plugins: /Applications/VLC.app/Contents/MacOS/plugins
Python: 3.11.9 (64bit) macOS 14.4.1 arm64
mrJean1 commented 6 months ago

This looks normal, correct. It is not possible to diagnose the issue further … remotely, sorry.

kravlad commented 6 months ago

This looks normal, correct. It is not possible to diagnose the issue further … remotely, sorry.

Yeah, I see. Thanks a lot for helping me!

mrJean1 commented 6 months ago

None for vlc.Instance is the direct result from LibVLC function libvlc_new and not a Python or python_vlc issue.

Google for "libvlc_new returns null" and see whether any of those issue apply to your particular case.

Also, check the forum at VideoLAN.org, one entry there mentions that libvlc_new may return NULL if a plugin can't be found. The plugins folder exists but may not be populated properly?

For example, there may be plugins in that folder /Applications/VLC.app/Contents/MacOS/plugin from an older version of VLC or for the Intel processor. Delete everything in the folder, see if that helps. Then, reinstall the M version of VLC.

kravlad commented 6 months ago

libvlc_new returns null

Oh, man, it turned out to be so easy. Just setting the environment variable:

VLC_PLUGIN_PATH=/Applications/VLC.app/Contents/MacOS/plugins

solved my problem. It's literally the first link in Google results.

Thank you so much again!

mrJean1 commented 6 months ago

That’s strange because … that is the exact same plugins paths vlc.py found and showed when run with the -v command line option.

kravlad commented 6 months ago

vlc.py doesn't look at environment variables, the path is hardcoded:

elif sys.platform.startswith('darwin'):
    # FIXME: should find a means to configure path
    d = '/Applications/VLC.app/Contents/MacOS/'
    c = d + 'lib/libvlccore.dylib'
    p = d + 'lib/libvlc.dylib'
    if os.path.exists(p) and os.path.exists(c):
        # pre-load libvlccore VLC 2.2.8+
        ctypes.CDLL(c)
        dll = ctypes.CDLL(p)
        for p in ('modules', 'plugins'):
            p = d + p
            if os.path.isdir(p):
                plugin_path = p
                break
mrJean1 commented 6 months ago

True. And it is the same path (before /plugins).

Something else must be wrong or off, because earlier, vlc.py found the same /Applications/VLC.app/Contents/MacOS/plugins folder on its own, without setting env var VLC_PLUGIN_PATH.

Maybe, file /Applications/VLC.app/Contents/MacOS/lib/libvlccore.dylib does not exist on your machine (but file /Applications/VLC.app/Contents/MacOS/lib/libvlc.dylib does).

Btw, which flavor of vlc.py is this, generated/3.0/vlc.py or generated/2.2/vlc.py?