0

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:

  • Windows 64-bit
  • VLC Player 64-bit
  • Visual Studio w/ virtual environment
  • Python 3.9.6

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.

ScottHJ
  • 5
  • 5
  • what gui library are you using? usually you need to tell libvlc which window to draw on – mfkl Aug 31 '21 at 07:35

1 Answers1

2

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.

  •     0: 'NothingSpecial'
    
  •     1: 'Opening'
    
  •     2: 'Buffering'
    
  •     3: 'Playing'
    
  •     4: 'Paused'
    
  •     5: 'Stopped'
    
  •     6: 'Ended'
    
  •     7: 'Error'
    
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Brilliant. Thank you so much. The original code I posted was taken from a tutorial on youtube which "just worked" in the video, without the extension that you supplied. I can continue with my project now. Thanks a million. – ScottHJ Sep 01 '21 at 08:28