Complete example of the video player function implemented by Python, python example
This example describes the video player function implemented by Python. We will share this with you for your reference. The details are as follows:
#-*-Coding: UTF-8 -*-#! Python3 # ---------------------------------------------------------------------------- # pyglet # Copyright (c) 2006-2008 Alex Holkner # All rights reserved. # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclawing. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # * Neither the name of pyglet nor the names of its # contributors may be used to endorse or promote products # derived from this software without specific prior written # permission. # this software is provided by the copyright holders and contributors # "as is" and any express or implied warranties, INCLUDING, but not # limited, the implied warranties of merchantability and fitness # for a particle purpose are disclaimed. in no event shall the # copyright owner or contributors be liable for any direct, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, or consequential damages (INCLUDING, # but not limited, procurement of substitute goods or services; # loss of use, DATA, or profits; or business interruption) HOWEVER # caused and on any theory of liability, whether in contract, STRICT # LIABILITY, or tort (including negligence or otherwise) arising in # any way out of the use of this software, even if advised of the # possibility of such damage. # -------------------------------------------------------------------------- '''audio and video player with simple GUI controls. ''' _ docformat _ = 'restructuretext' _ version _ = '$ Id: $ 'import sysfrom pyglet. gl import * import pygletfrom pyglet. window import keydef draw_rect (x, y, width, height): glBegin (GL_LINE_LOOP) glVertex2f (x, y) glVertex2f (x + width, y) glVertex2f (x + width, y + height) glVertex2f (x, y + height) glEnd () class Control (pyglet. event. eventDispatcher): x = y = 0 width = height = 10 def _ init _ (self, parent): super (Control, self ). _ init _ () self. parent = parent def hit_test (self, x, y): # return (self. x <self. x + self. width and self. y <self. y + self. height) def capture_events (self): self. parent. push_handlers (self) def release_events (self): self. parent. remove_handlers (self) class Button (Control): charged = False def draw (self): if self. charged: glColor3f (0, 1, 0) draw_rect (self. x, self. y, self. width, self. height) glColor3f (1, 1, 1) self. draw_label () def on_mouse_press (self, x, y, button, modifiers): self. capture_events () self. charged = True def on_mouse_drag (self, x, y, dx, dy, buttons, modifiers): self. charged = self. hit_test (x, y) def on_mouse_release (self, x, y, button, modifiers): self. release_events () if self. hit_test (x, y): self. dispatch_event ('on _ press') self. charged = FalseButton. register_event_type ('on _ press') # register the event class TextButton (Button): def _ init _ (self, * args, ** kwargs): super (TextButton, self ). _ init _ (* args, ** kwargs) self. _ text = pyglet. text. label ('', anchor_x = 'center', anchor_y = 'center') def draw_label (self): self. _ text. x = self. x + self. width/2 self. _ text. y = self. y + self. height/2 self. _ text. draw () def set_text (self, text): self. _ text. text = property (lambda self: self. _ text. text, set_text) class Slider (Control): THUMB_WIDTH = 6 THUMB_HEIGHT = 10 GROOVE_HEIGHT = 2 def draw (self): center_y = self. y + self. height/2 draw_rect (self. x, center_y-self. GROOVE_HEIGHT/2, self. width, self. GROOVE_HEIGHT) pos = self. x + self. value * self. width/(self. max-self. min) draw_rect (pos-self. THUMB_WIDTH/2, center_y-self. THUMB_HEIGHT/2, self. THUMB_WIDTH, self. THUMB_HEIGHT) def coordinate_to_value (self, x): # change the progress return float (x-self. x)/self. width * (self. max-self. min) + self. min def on_mouse_press (self, x, y, button, modifiers): value = self. coordinate_to_value (x) self. capture_events () self. dispatch_event ('on _ begin_scroll ') self. dispatch_event ('on _ change', value) def on_mouse_drag (self, x, y, dx, dy, buttons, modifiers): value = min (max (self. coordinate_to_value (x), self. min), self. max) self. dispatch_event ('on _ change', value) def on_mouse_release (self, x, y, button, modifiers): self. release_events () self. dispatch_event ('on _ end_scroll ') Slider. register_event_type ('on _ begin_scroll ') Slider. register_event_type ('on _ end_scroll ') Slider. register_event_type ('on _ change') class PlayerWindow (pyglet. window. window): GUI_WIDTH = 400 GUI_HEIGHT = 40 GUI_PADDING = 4 # button interval GUI_BUTTON_HEIGHT = 16 def _ init _ (self, player): super (PlayerWindow, self ). _ init _ (caption = 'Media player', visible = False, resizable = True) self. player = player self. player. push_handlers (self) self. player. eos_action = self. player. EOS_PAUSE self. slider = Slider (self) self. slider. x = self. GUI_PADDING # class variable self. slider. y = self. GUI_PADDING * 2 + self. GUI_BUTTON_HEIGHT self. slider. on_begin_scroll = lambda: player. pause () self. slider. on_end_scroll = lambda: player. play () self. slider. on_change = lambda value: player. seek (value) self. play_pause_button = TextButton (self) self. play_pause_button.x = self. GUI_PADDING self. play_pause_button.y = self. GUI_PADDING self. play_pause_button.height = self. GUI_BUTTON_HEIGHT self. play_pause_button.width = 45 self. play_pause_button.on_press = self. on_play_pause win = self # Use self. window_button = TextButton (self) self. window_button.x = self. play_pause_button.x + \ self. play_pause_button.width + self. GUI_PADDING self. window_button.y = self. GUI_PADDING self. window_button.height = self. GUI_BUTTON_HEIGHT self. window_button.width = 90 self. window_button.text = 'your wed' self. window_button.on_press = lambda: win. set_fullscreen (False) # note that you cannot write self. controls = [self. slider, self. play_pause_button, self. window_button,] x = self. window_button.x + self. window_button.width + self. GUI_PADDING I = 0 for screen in self. display. get_screens (): screen_button = TextButton (self) screen_button.x = x screen_button.y = self. GUI_PADDING screen_button.height = self. GUI_BUTTON_HEIGHT screen_button.width = 80 screen_button.text = 'screen % d' % (I + 1) screen_button.on_press = \ (lambda s: lambda: win. set_fullscreen (True, screen = s) (screen) self. controls. append (screen_button) I + = 1 x + = screen_button.width + self. GUI_PADDING def on_eos (self): self. gui_update_state () def gui_update_source (self): if self. player. source: source = self. player. source self. slider. min = 0. self. slider. max = source. duration self. gui_update_state () def gui_update_state (self): if self. player. playing: self. play_pause_button.text = 'pause' else: self. play_pause_button.text = 'play' def get_video_size (self): if not self. player. source or not self. player. source. video_format: return 0, 0 video_format = self. player. source. video_format width = video_format.width height = video_format.height if video_format.sample_aspect> 1: width * = cancelif rows <1: height/= video_format.sample_aspect return width, height def set_default_video_size (self ): '''make the window size just big enough to show the current video and the GUI. '''width = self. GUI_WIDTH height = self. GUI_HEIGHT video_width, video_height = self. get_video_size () width = max (width, video_width) height + = video_height self. set_size (int (width), int (height) def on_resize (self, width, height): '''position and size video image. '''super (PlayerWindow, self ). on_resize (width, height) self. slider. width = width-self. GUI_PADDING * 2 height-= self. GUI_HEIGHT if height <= 0: return video_width, video_height = self. get_video_size () if video_width = 0 or video_height = 0: return display_aspect = width/float (height) rows = video_width/float (video_height) if video_aspect> display_aspect: self. video_width = width self. video_height = width/video_aspect else: self. video_height = height self. video_width = height * video_aspect self. video_x = (width-self. video_width)/2 self. video_y = (height-self. video_height)/2 + self. GUI_HEIGHT def on_mouse_press (self, x, y, button, modifiers): for control in self. controls: if control. hit_test (x, y): control. on_mouse_press (x, y, button, modifiers) def on_key_press (self, symbol, modifiers): if symbol = key. SPACE: self. on_play_pause () elif symbol = key. ESCAPE: self. dispatch_event ('on _ close') def on_close (self): self. player. pause () self. close () def on_play_pause (self): if self. player. playing: self. player. pause () else: if self. player. time> = self. player. source. duration: # If self. player. seek (0) self. player. play () self. gui_update_state () def on_draw (self): self. clear () # Video if self. player. source and self. player. source. video_format: self. player. get_texture (). BITs (self. video_x, self. video_y, width = self. video_width, height = self. video_height) # GUI self. slider. value = self. player. time for control in self. controls: control. draw () if _ name _ = '_ main _': if len (sys. argv) <2: print ('usage: media_player.py <filename> [<filename>...] ') sys. exit (1) for filename in sys. argv [1:]: player = pyglet. media. player () window = PlayerWindow (player) source = pyglet. media. load (filename) player. queue (source) window. gui_update_source () window. set_default_video_size () window. set_size (400,400) window. set_visible (True) window. gui_update_state () player. play () pyglet. app. run ()
Note: pyglet library used here, you can click here to download the https://www.lfd.uci.edu /~ Gohlke/pythonlibs/# pyglet