pythonvideovlcpython-vlc

VLC Player window disappears as soon as playing a file with python-vlc


I am trying to play videos in VLC player via the python module "python-vlc" and the video player disappears immediately after launching.

Here is the code:

import vlc

media = vlc.MediaPlayer("video.mp4")
media.play()

When running this, the VLC player opens for a second, and displays the correct size dimensions for the video file I'm calling ("video.mp4"), but immediately disappears (i.e. closes) as soon as it launches.

Details about my setup:

There is a similar issue here, where the proposed solution is to delete some kind of vlc cache, but I'm not sure how to apply it to my context. Maybe my problem is due to something else.

Also: the video file in question is present in the project's root directory.

Any help would be greatly appreciated.


Solution

  • You need to keep your code running for the duration of the media, you are playing.
    The easiest way is to monitor the media instance.

    import vlc
    import time
    
    media = vlc.MediaPlayer("video.mp4")
    media.play()
    playing = set([1,2,3,4])
    play = True
    while play:
        time.sleep(0.5)
        state = media.get_state()
        if state in playing:
            continue
        else:
            play = False
    

    The set playing contains the 4 vlc states that should indicate that vlc is still playing the media.