前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 媒体文件播放 脚本

python 媒体文件播放 脚本

作者头像
用户5760343
发布2022-05-13 11:05:25
5390
发布2022-05-13 11:05:25
举报
文章被收录于专栏:sktj

!/usr/local/bin/python

""" ################################################################################## Try to play an arbitrary media file. Allows for specific players instead of always using general web browser scheme. May not work on your system as is; audio files use filters and command lines on Unix, and filename associations on Windows via the start command (i.e., whatever you have on your machine to run .au files--an audio player, or perhaps a web browser). Configure and extend as needed. playknownfile assumes you know what sort of media you wish to open, and playfile tries to determine media type automatically using Python mimetypes module; both try to launch a web browser with Python webbrowser module as a last resort when mimetype or platform unknown. ################################################################################## """

import os, sys, mimetypes, webbrowser

helpmsg = """ Sorry: can't find a media player for '%s' on your system! Add an entry for your system to the media player dictionary for this type of file in playfile.py, or play the file manually. """

def trace(args): print(args) # with spaces between

##################################################################################

player techniques: generic and otherwise: extend me

##################################################################################

class MediaTool: def init(self, runtext=''): self.runtext = runtext def run(self, mediafile, **options): # most ignore options fullpath = os.path.abspath(mediafile) # cwd may be anything self.open(fullpath, **options)

class Filter(MediaTool): def open(self, mediafile, **ignored): media = open(mediafile, 'rb') player = os.popen(self.runtext, 'w') # spawn shell tool player.write(media.read()) # send to its stdin

class Cmdline(MediaTool): def open(self, mediafile, **ignored): cmdline = self.runtext % mediafile # run any cmd line os.system(cmdline) # use %s for filename

class Winstart(MediaTool): # use Windows registry def open(self, mediafile, wait=False, **other): # or os.system('start file') if not wait: # allow wait for curr media os.startfile(mediafile) else: os.system('start /WAIT ' + mediafile)

class Webbrowser(MediaTool): # file:// requires abs path def open(self, mediafile, **options): webbrowser.open_new('file://%s' % mediafile, **options)

##################################################################################

media- and platform-specific policies: change me, or pass one in

##################################################################################

map platform to player: change me!

audiotools = { 'sunos5': Filter('/usr/bin/audioplay'), # os.popen().write() 'linux2': Cmdline('cat %s > /dev/audio'), # on zaurus, at least 'sunos4': Filter('/usr/demo/SOUND/play'), # yes, this is that old! 'win32': Winstart() # startfile or system

'win32': Cmdline('start %s')

代码语言:javascript
复制
}

videotools = { 'linux2': Cmdline('tkcVideo_c700 %s'), # zaurus pda 'win32': Winstart(), # avoid DOS pop up }

imagetools = { 'linux2': Cmdline('zimager %s'), # zaurus pda 'win32': Winstart(), }

texttools = { 'linux2': Cmdline('vi %s'), # zaurus pda 'win32': Cmdline('notepad %s') # or try PyEdit? }

apptools = { 'win32': Winstart() # doc, xls, etc: use at your own risk! }

map mimetype of filenames to player tables

mimetable = {'audio': audiotools, 'video': videotools, 'image': imagetools, 'text': texttools, # not html text: browser 'application': apptools}

##################################################################################

top-level interfaces

##################################################################################

def trywebbrowser(filename, helpmsg=helpmsg, **options): """ try to open a file in a web browser last resort if unknown mimetype or platform, and for text/html """ trace('trying browser', filename) try: player = Webbrowser() # open in local browser player.run(filename, **options) except: print(helpmsg % filename) # else nothing worked

def playknownfile(filename, playertable={}, **options): """ play media file of known type: uses platform-specific player objects, or spawns a web browser if nothing for this platform; accepts a media-specific player table """ if sys.platform in playertable: playertable[sys.platform].run(filename, **options) # specific tool else: trywebbrowser(filename, **options) # general scheme

def playfile(filename, mimetable=mimetable, **options): """ play media file of any type: uses mimetypes to guess media type and map to platform-specific player tables; spawn web browser if text/html, media type unknown, or has no table """ contenttype, encoding = mimetypes.guess_type(filename) # check name if contenttype == None or encoding is not None: # can't guess contenttype = '?/?' # poss .txt.gz maintype, subtype = contenttype.split('/', 1) # 'image/jpeg' if maintype == 'text' and subtype == 'html': trywebbrowser(filename, **options) # special case elif maintype in mimetable: playknownfile(filename, mimetable[maintype], **options) # try table else: trywebbrowser(filename, **options) # other types

###############################################################################

self-test code

###############################################################################

if name == 'main': # media type known playknownfile('sousa.au', audiotools, wait=True) playknownfile('ora-pp3e.gif', imagetools, wait=True) playknownfile('ora-lp4e.jpg', imagetools)

代码语言:javascript
复制
# media type guessed
input('Stop players and press Enter')
playfile('ora-lp4e.jpg')                     # image/jpeg
playfile('ora-pp3e.gif')                     # image/gif
playfile('priorcalendar.html')               # text/html
playfile('lp4e-preface-preview.html')        # text/html
playfile('lp-code-readme.txt')               # text/plain
playfile('spam.doc')                         # app
playfile('spreadsheet.xls')                  # app
playfile('sousa.au', wait=True)              # audio/basic
input('Done')                                # stay open if clicked
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • !/usr/local/bin/python
  • player techniques: generic and otherwise: extend me
  • media- and platform-specific policies: change me, or pass one in
  • map platform to player: change me!
  • 'win32': Cmdline('start %s')
  • map mimetype of filenames to player tables
  • top-level interfaces
  • self-test code
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档