Python实现的视频播放器功能完整示例
本文实例讲述了Python实现的视频播放器功能。分享给大家供大家参考,具体如下:
#-*-coding:utf-8-*- #!python3 #---------------------------------------------------------------------------- #pyglet #Copyright(c)2006-2008AlexHolkner #Allrightsreserved. # #Redistributionanduseinsourceandbinaryforms,withorwithout #modification,arepermittedprovidedthatthefollowingconditions #aremet: # #*Redistributionsofsourcecodemustretaintheabovecopyright #notice,thislistofconditionsandthefollowingdisclaimer. #*Redistributionsinbinaryformmustreproducetheabovecopyright #notice,thislistofconditionsandthefollowingdisclaimerin #thedocumentationand/orothermaterialsprovidedwiththe #distribution. #*Neitherthenameofpygletnorthenamesofits #contributorsmaybeusedtoendorseorpromoteproducts #derivedfromthissoftwarewithoutspecificpriorwritten #permission. # #THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS #"ASIS"ANDANYEXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOT #LIMITEDTO,THEIMPLIEDWARRANTIESOFMERCHANTABILITYANDFITNESS #FORAPARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALLTHE #COPYRIGHTOWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT, #INCIDENTAL,SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING, #BUTNOTLIMITEDTO,PROCUREMENTOFSUBSTITUTEGOODSORSERVICES; #LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)HOWEVER #CAUSEDANDONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICT #LIABILITY,ORTORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGIN #ANYWAYOUTOFTHEUSEOFTHISSOFTWARE,EVENIFADVISEDOFTHE #POSSIBILITYOFSUCHDAMAGE. #---------------------------------------------------------------------------- '''AudioandvideoplayerwithsimpleGUIcontrols. ''' __docformat__='restructuredtext' __version__='$Id:$' importsys frompyglet.glimport* importpyglet frompyglet.windowimportkey defdraw_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() classControl(pyglet.event.EventDispatcher): x=y=0 width=height=10 def__init__(self,parent): super(Control,self).__init__() self.parent=parent defhit_test(self,x,y):#点中控件 return(self.x1: width*=video_format.sample_aspect elifvideo_format.sample_aspect<1: height/=video_format.sample_aspect returnwidth,height defset_default_video_size(self): '''Makethewindowsizejustbigenoughtoshowthecurrent videoandtheGUI.''' 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)) defon_resize(self,width,height): '''Positionandsizevideoimage.''' super(PlayerWindow,self).on_resize(width,height) self.slider.width=width-self.GUI_PADDING*2 height-=self.GUI_HEIGHT ifheight<=0: return video_width,video_height=self.get_video_size() ifvideo_width==0orvideo_height==0: return display_aspect=width/float(height) video_aspect=video_width/float(video_height) ifvideo_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 defon_mouse_press(self,x,y,button,modifiers): forcontrolinself.controls: ifcontrol.hit_test(x,y): control.on_mouse_press(x,y,button,modifiers) defon_key_press(self,symbol,modifiers): ifsymbol==key.SPACE: self.on_play_pause() elifsymbol==key.ESCAPE: self.dispatch_event('on_close') defon_close(self): self.player.pause() self.close() defon_play_pause(self): ifself.player.playing: self.player.pause() else: ifself.player.time>=self.player.source.duration:#如果放完了 self.player.seek(0) self.player.play() self.gui_update_state() defon_draw(self): self.clear() #Video ifself.player.sourceandself.player.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.player.time forcontrolinself.controls: control.draw() if__name__=='__main__': iflen(sys.argv)<2: print('Usage:media_player.py [ ...]') sys.exit(1) forfilenameinsys.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()
注:这里用到的pyglet库,可点击此处下载https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyglet
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《PythonSocket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。