|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +u'''Bare Bones VLC Media Player Demo with Playlist. |
| 5 | +
|
| 6 | +1 - Originally the Demo_Media_Player_VLC_Based.py duplicated from |
| 7 | + <https://GitHub.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms> |
| 8 | + and modified to work and showing videos on recent macOS versions. |
| 9 | +
|
| 10 | +2 - This script uses PySimpleGUI under its LGPL3+ stipulations. |
| 11 | +
|
| 12 | +3 - You will need to install the Python bindings for VLC, for example |
| 13 | + using pip: python3 -m pip install python-vlc |
| 14 | +
|
| 15 | +4 - You need the VLC player itself from <https://www.VideoLan.org>. |
| 16 | +
|
| 17 | +5 - On macOS, you also need to get tkvlc.py from this location |
| 18 | + <https://GitHub.com/oaubert/python-vlc/tree/master/examples> |
| 19 | + to get video and audio. |
| 20 | +
|
| 21 | +6 - On macOS, the video plays full-frame, overwriting the buttons. |
| 22 | +
|
| 23 | +7 - Original <https://GitHub.com/israel-dryer/Media-Player> by Israel |
| 24 | + Dryer, modified to be a PySimpleGUI Demo Program and a python-vlc |
| 25 | + example for you to customize. Uses the VLC player to playback |
| 26 | + local media files (and YouTube streams). |
| 27 | +''' |
| 28 | +import sys |
| 29 | +if sys.version_info[0] < 3: # Python 3.4+ only |
| 30 | + sys.exit('%s requires Python 3.4 or later' % (sys.argv[0],)) |
| 31 | + # import Tkinter as tk |
| 32 | +import PySimpleGUI as sg |
| 33 | +import vlc |
| 34 | + |
| 35 | +__all__ = ('libtk',) |
| 36 | +__version__ = '22.11.07' # mrJean1 at Gmail |
| 37 | + |
| 38 | +_Load_ = 'Load' |
| 39 | +_Next_ = 'Next' |
| 40 | +_Path_ = 'Media URL or local path:' |
| 41 | +_Pause_ = 'Pause' |
| 42 | +_Play_ = 'Play' |
| 43 | +_Prev_ = 'Previous' |
| 44 | +_Stop_ = 'Stop' |
| 45 | + |
| 46 | +# GUI definition & setup |
| 47 | +sg.theme('DarkBlue') |
| 48 | + |
| 49 | +def Bn(name): # a PySimpleGUI "User Defined Element" (see docs) |
| 50 | + return sg.Button(name, size=(8, 1), pad=(1, 1)) |
| 51 | + |
| 52 | +layout = [[sg.Input(default_text=_Path_, size=(40, 1), key='-VIDEO_PATH-'), sg.Button(_Load_)], |
| 53 | + [sg.Frame('', [], size=(300, 170), key='-VID_OUT-')], # was [sg.Image('', ...)], |
| 54 | + [Bn(_Prev_), Bn(_Play_), Bn(_Next_), Bn(_Pause_), Bn(_Stop_)], |
| 55 | + [sg.Text('Load media to start', key='-MESSAGE_AREA-')]] |
| 56 | + |
| 57 | +window = sg.Window('PySimpleGUI VLC Player', layout, element_justification='center', finalize=True, resizable=True) |
| 58 | + |
| 59 | +window['-VID_OUT-'].expand(True, True) # type: sg.Element |
| 60 | + |
| 61 | +# Media Player Setup |
| 62 | +inst = vlc.Instance() |
| 63 | +list_player = inst.media_list_player_new() |
| 64 | +media_list = inst.media_list_new([]) |
| 65 | +list_player.set_media_list(media_list) |
| 66 | +player = list_player.get_media_player() |
| 67 | +# tell VLC where to render the video(s) |
| 68 | +tk_id = window['-VID_OUT-'].Widget.winfo_id() |
| 69 | +libtk = '' |
| 70 | +if sg.running_linux(): |
| 71 | + player.set_xwindow(tk_id) |
| 72 | +elif sg.running_windows(): |
| 73 | + player.set_hwnd(tk_id) |
| 74 | +elif sg.running_mac(): |
| 75 | + try: |
| 76 | + from tkvlc import _GetNSView, libtk |
| 77 | + ns = _GetNSView(tk_id) |
| 78 | + except ImportError: |
| 79 | + ns = None |
| 80 | + libtk = 'none, install tkvlc.py from <https://GitHub.com/oaubert/python-vlc> examples' |
| 81 | + if ns: # drawable NSview |
| 82 | + player.set_nsobject(ns) |
| 83 | + else: # no video, only audio |
| 84 | + player.set_xwindow(tk_id) |
| 85 | +else: # running trinket, etc. |
| 86 | + player.set_hwnd(tk_id) # TBD |
| 87 | + |
| 88 | +if __name__ == '__main__': # MCCABE 20 |
| 89 | + |
| 90 | + if len(sys.argv) > 1: |
| 91 | + if sys.argv[1].lower() in ('-v', '--version'): |
| 92 | + # show all versions, this vlc.py, libvlc, etc. (sample output on macOS): |
| 93 | + # ... |
| 94 | + # % python3 ./psgvlc.py -v |
| 95 | + # psgvlc.py: 22.11.06 |
| 96 | + # tkinter: 8.6 |
| 97 | + # libTk: /Library/Frameworks/Python.framework/Versions/3.11/lib/libtk8.6.dylib |
| 98 | + # vlc.py: 3.0.12119 (Mon May 31 18:25:17 2021 3.0.12) |
| 99 | + # libVLC: 3.0.16 Vetinari (0x3001000) |
| 100 | + # plugins: /Applications/VLC.app/Contents/MacOS/plugins |
| 101 | + # Python: 3.11.0 (64bit) macOS 13.0 arm64 |
| 102 | + for t in ((sys.argv[0], __version__), (sg.tk.__name__, sg.tk.TkVersion), ('libTk', libtk)): |
| 103 | + print('{}: {}'.format(*t)) |
| 104 | + try: |
| 105 | + vlc.print_version() |
| 106 | + vlc.print_python() |
| 107 | + except AttributeError: |
| 108 | + pass |
| 109 | + sys.exit(0) |
| 110 | + |
| 111 | + if sys.argv[1]: |
| 112 | + media_list.add_media(sys.argv[1]) |
| 113 | + list_player.set_media_list(media_list) |
| 114 | + |
| 115 | + # The Event Loop |
| 116 | + while True: |
| 117 | + # run with a timeout so that current location can be updated |
| 118 | + event, values = window.read(timeout=1000) |
| 119 | + |
| 120 | + if event == sg.WIN_CLOSED: |
| 121 | + break |
| 122 | + |
| 123 | + if event == _Pause_: |
| 124 | + list_player.pause() |
| 125 | + elif event == _Stop_: |
| 126 | + list_player.stop() |
| 127 | + elif event == _Next_: |
| 128 | + list_player.next() |
| 129 | + list_player.play() |
| 130 | + elif event == _Prev_: |
| 131 | + list_player.previous() # first call causes current video to start over |
| 132 | + list_player.previous() # second call moves back 1 video from current |
| 133 | + list_player.play() |
| 134 | + elif event == _Play_: |
| 135 | + list_player.play() |
| 136 | + elif event == _Load_: |
| 137 | + path = values['-VIDEO_PATH-'] |
| 138 | + if path and _Path_ not in path: |
| 139 | + media_list.add_media(path) |
| 140 | + list_player.set_media_list(media_list) |
| 141 | + window['-VIDEO_PATH-'].update(_Path_) # only add a legit submit |
| 142 | + |
| 143 | + # update elapsed time if a video loaded and playing |
| 144 | + if player.is_playing(): |
| 145 | + text = '{:02d}:{:02d}'.format(*divmod(player.get_time() // 1000, 60)) + ' / ' + \ |
| 146 | + '{:02d}:{:02d}'.format(*divmod(player.get_length() // 1000, 60)) |
| 147 | + if sg.running_mac(): |
| 148 | + print('{}: {}'.format(sys.argv[0], text)) |
| 149 | + |
| 150 | + elif not media_list.count(): |
| 151 | + text = 'Load media to start' |
| 152 | + else: |
| 153 | + text = 'Ready to play media' |
| 154 | + window['-MESSAGE_AREA-'].update(text) |
| 155 | + |
| 156 | + window.close() |
0 commit comments