0

I've been trying to find a way to control the vlc playback from within a Python script. Ideally, I'd like to use the built in vlc button controls for play, pause, stop, and, rewind. I've read some posts that implied that those controls are not available when run from a script. I then decided to use the keyboard shortcuts to control the video. My sample code is below.

def pauseVideo(event, player):
    i = 0

# Play a video.
def playVideo(path):
    global events

    try:
        #vlcCommand = "/snap/bin/vlc " + path + " --fullscreen"
        #os.system(vlcCommand)

        instance = vlc.Instance()
        mediaPlayer = instance.media_player_new()
        media = instance.media_new_location(path)
        mediaPlayer.set_media(media)

        # creating vlc media player object
        #mediaPlayer = vlc.MediaPlayer(path)
        
        # setting full screen status 
        #mediaPlayer.set_fullscreen(True)       
        
        #mediaPlayer.video_set_mouse_input(True)
        events = mediaPlayer.event_manager()

        # start playing video
        mediaPlayer.play()  

        events.event_attach(vlc.EventType.MediaPlayerPaused, pauseVideo)

        #while():
        #    continue   

    except Exception as e:
        error = "Exception from 'playVideo':" + sys.exc_info()[0] + " occurred."
        ReportError(error)

Since I could not find a way to use the standard buttons, I thought I might be able to respond to the keyboard events. I haven't been able to get it to work. When I run the code, my event is not triggered.

Can anyone point me in the right direction to handle vlc events in Python?

rrirower
  • 4,338
  • 4
  • 27
  • 45
  • Something needs to be `listening` to the keyboard within the `while` loop. That means either you code it or run with the basic `python vlc.py myvideo.mp4` which implements a built in player. Take a look at the bottom of vlc.py file. Look for `keybindings` – Rolf of Saxony Mar 27 '21 at 10:40
  • So, am I incorrect in assuming that defining an event with the "event_attach" function will create a callback that is handled when the event is triggered? – rrirower Mar 27 '21 at 13:35
  • The point is, something has to trigger the event. Only then can an `event_attach` function be expected to be activated. If the events don't occur in the first place, no amount of defining is going to help. Your code is incomplete but it looks as if it doesn't activate any events. You could test it, by popping a `pause` in the loop and just printing something when it's activated in the `pauseVideo` definition. That definition by the way, shouldn't include `player` as a parameter, just `event`. – Rolf of Saxony Mar 27 '21 at 14:52
  • I should perhaps point out that certain `events` will happen without you triggering them e.g. the media ends, starts playing, the length calculation changes etc but this question refers specifically to changes requested, which requires external input. See this answer for examples https://stackoverflow.com/questions/66615821/python-vlc-player-next-control/66644133#66644133 – Rolf of Saxony Mar 27 '21 at 15:05
  • Thanks for the quick replies. I had assumed that I could trigger a pause event by pressing the space bar while a video played. – rrirower Mar 27 '21 at 19:55
  • You can if you use `python vlc.py myvideo.mp4` but once you write your own code, not only do become master of all you survey but also the fall guy, when it comes to responsibility for all interaction. :) – Rolf of Saxony Mar 27 '21 at 20:17

0 Answers0