I have a Raspberry Pi 3+, python 3.7, wx, and gstreamer. I wrote the following code:
import os import wx import wx.media class TrainingUI(wx.Frame): def __init__(self, parent, title): super(TrainingUI, self).__init__(parent, title="Training") self.media_control = None self.init_ui() self.Center() def start_clicked(self, evt): print("path = " + os.path.abspath(".")) #file_to_play = 'C:\\Users\\ralph\\files\\programming\\ImpactEmbedded\\senecadevices\\easy-shift-automation\\EasyShiftController\\LSDSHN.mpg' #file_to_play = '/home/pi/SenecaDevices/easy-shift-automation/EasyShiftController/sample-mp4-file.mp4' #file_to_play = "sample-mp4-file.mp4" file_to_play = "LSDSHN.mpg" print("Path exists: ") # find out if the program thinks the file exists print(os.path.exists(file_to_play)) if not self.media_control.Load(file_to_play): print("media load failed") else: self.media_control.Play() def init_ui(self): # the main_screen_panel holds all the controls and sizers; # we arrange their placement on it with the sizers, defined later. main_screen_panel = wx.Panel(self) start_btn = wx.Button(main_screen_panel, label="Start") self.Bind(wx.EVT_BUTTON, self.start_clicked) control_box = wx.BoxSizer(wx.VERTICAL) control_box.Add(start_btn) # the prompt, error message, and the video panel video_box = wx.BoxSizer(wx.VERTICAL) self.media_control = wx.media.MediaCtrl(main_screen_panel, style=wx.SIMPLE_BORDER, size=wx.Size(250, 150), szBackend="") # , szBackend=wx.media.MEDIABACKEND_DIRECTSHOW) video_box.Add(self.media_control) big_box = wx.BoxSizer(wx.HORIZONTAL) big_box.Add(control_box) big_box.Add(video_box, border=10, flag=wx.ALL) # outermost_box has a label across the top of the main window, and a 'big box' below containing everything else outermost_box = wx.BoxSizer(wx.VERTICAL) outermost_box.Add(wx.StaticText(main_screen_panel, label="wx.media test"), flag=wx.ALIGN_CENTER_HORIZONTAL) outermost_box.Add(big_box) main_screen_panel.SetSizer(outermost_box) def main(): app = wx.App() ex = TrainingUI(None, title="traaiin") ex.Show() app.MainLoop() if __name__ == '__main__': main()
I run it as follows:
project_directory $ ls -l LSDSHN.mpg -rw-r--r-- 1 pi pi 21665792 Jul 23 11:35 LSDSHN.mpg project_directory $ python training_ui_video.py path = /home/pi/SenecaDevices/easy-shift-automation/EasyShiftController Path exists: True media load failed
When it runs, it immediately pops up a message dialog saying “Got an invalid playbin (error 2: No such file or directory)”; this appears on top of the main screen for the program. It has an OK button to close it. I don’t know where this error comes from, just loading the program doesn’t attempt to load the video file, is there something else indicated by “invalid playbin”?
On the main screen of the program, I have the button and a black border around the media control, as coded. When I click the ‘Start’ button, I get the text below the program invocation as shown above (starting with ‘path = …’)
I have also tried with an MP4 file, same result.
I found one SO post about the invalid playbin message; one answer said that wxPython must be compiled with the correct libraries. I didn’t compile it (I heard it takes 10 hours on a Pi), but installed it from a .whl file that I understood to be correct - “”; I also had to search for a gstreamer package and ended up with “apt-get install python-wxgtk-media3.0”, which eliminated a message I was getting when attempting to import wx.media.
What can I do to play video within a python program as above, either with this approach or another one. Ideally, I would have control programmatically over start, stop, pause, play different videos, etc.