0

I'm using Python to play a fullscreen video on a Raspberry Pi. This code works, but there's no way for the user watching to exit fullscreen mode.

import vlc

media_player = vlc.MediaPlayer()
media_player.toggle_fullscreen()
vid  = vlc.Media("vid.mp4")
media_player.set_media(vide)
media_player.play()

I've tried using CTRL Q, C, D, X. All the Function keys. Escape. None of them work.

I know I can use media_player.stop() in my code to stop playback - but how do I interact with the fullscreen video from a keyboard to exit fullscreen?

Terence Eden
  • 14,034
  • 3
  • 48
  • 89

1 Answers1

2

python-vlc wraps LibVLC. LibVLC is a C API for integrating VLC into other software rather than scripting a running VLC instance. You'd need to add callbacks to handle keyboard shortcuts (and the video ending), or integrate LibVLC with a windowing system like GTK, Qt etc. There are some examples of how to do that available—for Linux, have a look at the gtk3 and other examples (the pyobjc one is for macOS). LibVLC itself doesn't really do anything in terms of key handling (see this SO question).

We delve into unpleasant hackiness, but you could implement them yourself. I've seen some instances online of people using the keyboard package to do this. For instance, you could do something like this (haven't tested it as I don't have a Linux machine to hand)...

import vlc
import keyboard

media_player = vlc.MediaPlayer()
media_player.set_fullscreen(True)
keyboard.add_hotkey("Esc", lambda: media_player.set_fullscreen(False))
vid = vlc.Media("vid.mp4")
media_player.set_media(vid)
media_player.play()

Obviously, this is very far from finished and for it to be useful, you'd probably need to implement a whole bunch of other stuff including some controls like play/pause (on space), rewind/fast forward (left/right) and quit (Ctrl+Q). (I've tested the keyboard module on Mac and it doesn't work too well because of the Mac's security settings but the behaviour is platform-specific so I don't know how well it works on Linux.)

An alternative that may be simpler for the kind of use case I think this post is getting at: VLC has a REST API (and a web interface). Unlike LibVLC, which is designed for giving you the building blocks for making your own video player, the REST API lets you get data about a running instance of VLC and control it.

If you were to launch VLC from the command line (or, in Python, by using os.system) using the -I http flag to enable the REST API, the --fullscreen flag (for obvious reasons), and the path to the media, e.g. vlc -I http --fullscreen path/to/media.mp4, then if you needed to control it from the keyboard, you could (because you've just got VLC running rather than a LibVLC instance), and if you needed to control it programatically, you can then use the REST API (or a Python library that implements the functionality).

(Depending on how many steps exist between your Raspberry Pi and the big scary internet, and depending how many bad people are on your local network, you also probably want to set a password using the --http-password flag.)

Tom Morris
  • 3,979
  • 2
  • 25
  • 43