Python-implemented video player features

Source: Internet
Author: User
Tags modifiers
This article mainly introduced the Python realization video player function, combined with the complete instance form analysis Python based on the Pyglet library realizes the video playback function The related operation skill, needs the friend to refer to the next

The example in this article describes the video player functionality that Python implements. Share to everyone for your reference, as follows:

#-*-Coding:utf-8-*-#! python3#----------------------------------------------------------------------------# pyglet# Copyright (c) 2006-2008 Alex holkner# All rights reserved.# # redistribution and use of source and binary forms, with or without# Modifi  cation, is permitted provided that the following conditions # is met:## * redistributions of source code must retain the Above copyright# notice, this list of conditions and the following disclaimer.# * redistributions in binary form must re produce 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 Used to endorse or promote products# derived from this software without specific prior written# permission.## this SOFT WARE is provided by the COPYRIGHT holders and contributors# ' as is ' and any EXPRESS OR implied warranties, including, but not# LIMITED To, the implied warranties of merchantability and fitness# for A particular PURPOSE is disclaimed. In NO EVENT shall the# COPYRIGHT OWNER OR CONTRIBUTORS is liable for any DIRECT, indirect,# incidental, special, exemplary , or consequential damages (including,# but not LIMITED to, procurement of substitute GOODS OR services;# LOSS of Use, DAT A, 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-out of the-of-the-software, even IF advised of the# possibility of SUCH damage.#---------------- ------------------------------------------------------------"Audio and video player with a simple GUI controls." __docformat__ = ' restructuredtext ' __version__ = ' $Id: $ ' import sysfrom pyglet.gl import *import pygletfrom Pyglet.window I Mport Keydef draw_rect (x, y, Width, height): Glbegin (Gl_line_loop) glvertex2f (x, y) glvertex2f (x + width, y) glVertex2 Fx + width, y + height) glvertex2f (x, y + height) glend () class Control (Pyglet.event.EventDispatcher): x = y = 0 width = Height = ten def __init__ (self, parent): Super (Control, self). __init__ () self.parent = parent Def hit_test (self, X , y): #点中控件 return (self.x < x < self.x + Self.width and Self.y < y < Self.y + self.height) def cap Ture_events (self): Self.parent.push_handlers (self) def release_events (self): Self.parent.remove_handlers (self) clas S 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.char GED = self.hit_test (x, y) def on_mouse_release (self, x, Y, button, modifiers): Self.release_events () if Self.hit_te St (x, y): Self.dispaTch_event (' on_press ') self.charged = Falsebutton.register_event_type (' on_press ') #注册事件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 = Text Text = Property (lambda Self:self._text.text, Set_text) class Slider (Control): Thumb_width = 6 Thumb_height = Groove_height = 2 D EF 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): #改变进度 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 = Gui_height = + gui_padding = 4# button Interval gui_button_height = 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.pl Ay () 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 = self.play_pause_button.on_press = Self.on_play_pause win = SE lf# own Magical Self.window_button = Textbutton (self) self.window_button.x = self.play_pause_button.x + \ Sel F.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 = Self.window_button.Text = ' windowed ' self.window_button.on_press = Lambda:win.set_fullscreen (False) #注意不能写self self.controls = [s Elf.slider, Self.play_pause_button, Self.window_button,] x = self.window_button.x + SELF.WINDOW_BUTTON.W Idth + self. Gui_padding i = 0 for screens 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 = Screen_button.text = ' screen%d '% (i + 1) screen_button.on_pres      s = \ (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:sou      Rce = 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.t ext = ' Play ' def get_video_size (self): If isn't 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 *= video_format.sample_aspect elif video_format.sample_aspect < 1: Height/= video_format.sample_aspect return width, height def set_default_video_size (self): "Make the Window Si    Ze 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_heig     HT 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 O r Video_height = = 0:return Display_aspect = width/float (height) video_aspect = video_width/float (Video_hei    ght) 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.vide O_width)/2 self.video_y = (height-self.video_height)/2 + self. Gui_height def on_mouse_press (self, x, Y, button, modifiers): With 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 = = k ey. 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.P Ause () else:if self.player.time >= self.player.source.duration: #如果放完了 self.player.seek (0) Self.pla Yer.play () self.gui_update_state () def on_draw (self): Self.clear () # Video if Self.player.source and Self.pla                      Yer.source.video_format:self.player.get_texture (). Blit (self.video_x, Self.video_y, Width=self.video_width, Height=self.video_height) # GUI Self.slider.value = Self.playe R.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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.