#! /usr/bin/env python

#

# mutagen aims to be an all purpose media tagging library

# Copyright (C) 2005  Michael Urman

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of version 2 of the GNU General Public License as

# published by the Free Software Foundation.

#

# $Id: __init__.py 4348 2008-12-02 02:41:15Z piman $

#

"""Mutagen aims to be an all purpose tagging library.

import mutagen.[format]

metadata = mutagen.[format].Open(filename)

metadata acts like a dictionary of tags in the file. Tags are generally a

list of string-like values, but may have additional methods available

depending on tag or format. They may also be entirely different objects

for certain keys, again depending on format.

"""

version = (1, 20)

version_string = ".".join(map(str, version))

import warnings

import mutagen._util

class Metadata(object):

"""An abstract dict-like object.

Metadata is the base class for many of the tag objects in Mutagen.

"""

def __init__(self, *args, **kwargs):

if args or kwargs:

self.load(*args, **kwargs)

def load(self, *args, **kwargs):

raise NotImplementedError

def save(self, filename=None):

raise NotImplementedError

def delete(self, filename=None):

raise NotImplementedError

class FileType(mutagen._util.DictMixin):

"""An abstract object wrapping tags and audio stream information.

Attributes:

info -- stream information (length, bitrate, sample rate)

tags -- metadata tags, if any

Each file format has different potential tags and stream

information.

FileTypes implement an interface very similar to Metadata; the

dict interface, save, load, and delete calls on a FileType call

the appropriate methods on its tag data.

"""

info = None

tags = None

filename = None

_mimes = ["application/octet-stream"]

def __init__(self, filename=None, *args, **kwargs):

if filename is None:

warnings.warn("FileType constructor requires a filename",

DeprecationWarning)

else:

self.load(filename, *args, **kwargs)

def load(self, filename, *args, **kwargs):

raise NotImplementedError

def __getitem__(self, key):

"""Look up a metadata tag key.

If the file has no tags at all, a KeyError is raised.

"""

if self.tags is None: raise KeyError, key

else: return self.tags[key]

def __setitem__(self, key, value):

"""Set a metadata tag.

If the file has no tags, an appropriate format is added (but

not written until save is called).

"""

if self.tags is None:

self.add_tags()

self.tags[key] = value

def __delitem__(self, key):

"""Delete a metadata tag key.

If the file has no tags at all, a KeyError is raised.

"""

if self.tags is None: raise KeyError, key

else: del(self.tags[key])

def keys(self):

"""Return a list of keys in the metadata tag.

If the file has no tags at all, an empty list is returned.

"""

if self.tags is None: return []

else: return self.tags.keys()

def delete(self, filename=None):

"""Remove tags from a file."""

if self.tags is not None:

if filename is None:

filename = self.filename

else:

warnings.warn(

"delete(filename=...) is deprecated, reload the file",

DeprecationWarning)

return self.tags.delete(filename)

def save(self, filename=None, **kwargs):

"""Save metadata tags."""

if filename is None:

filename = self.filename

else:

warnings.warn(

"save(filename=...) is deprecated, reload the file",

DeprecationWarning)

if self.tags is not None:

return self.tags.save(filename, **kwargs)

else: raise ValueError("no tags in file")

def pprint(self):

"""Print stream information and comment key=value pairs."""

stream = "%s (%s)" % (self.info.pprint(), self.mime[0])

try: tags = self.tags.pprint()

except AttributeError:

return stream

else: return stream + ((tags and "\n" + tags) or "")

def add_tags(self):

raise NotImplementedError

def __get_mime(self):

mimes = []

for Kind in type(self).__mro__:

for mime in getattr(Kind, '_mimes', []):

if mime not in mimes:

mimes.append(mime)

return mimes

mime = property(__get_mime)

def File(filename, options=None, easy=False):

"""Guess the type of the file and try to open it.

The file type is decided by several things, such as the first 128

bytes (which usually contains a file type identifier), the

filename extension, and the presence of existing tags.

If no appropriate type could be found, None is returned.

"""

if options is None:

from mutagen.asf import ASF

from mutagen.apev2 import APEv2File

from mutagen.flac import FLAC

if easy:

from mutagen.easyid3 import EasyID3FileType as ID3FileType

else:

from mutagen.id3 import ID3FileType

if easy:

from mutagen.mp3 import EasyMP3 as MP3

else:

from mutagen.mp3 import MP3

from mutagen.oggflac import OggFLAC

from mutagen.oggspeex import OggSpeex

from mutagen.oggtheora import OggTheora

from mutagen.oggvorbis import OggVorbis

if easy:

from mutagen.trueaudio import EasyTrueAudio as TrueAudio

else:

from mutagen.trueaudio import TrueAudio

from mutagen.wavpack import WavPack

if easy:

from mutagen.easymp4 import EasyMP4 as MP4

else:

from mutagen.mp4 import MP4

from mutagen.musepack import Musepack

from mutagen.monkeysaudio import MonkeysAudio

from mutagen.optimfrog import OptimFROG

options = [MP3, TrueAudio, OggTheora, OggSpeex, OggVorbis, OggFLAC,

FLAC, APEv2File, MP4, ID3FileType, WavPack, Musepack,

MonkeysAudio, OptimFROG, ASF]

if not options:

return None

fileobj = file(filename, "rb")

try:

header = fileobj.read(128)

# Sort by name after score. Otherwise import order affects

# Kind sort order, which affects treatment of things with

# equals scores.

results = [(Kind.score(filename, fileobj, header), Kind.__name__)

for Kind in options]

finally:

fileobj.close()

results = zip(results, options)

results.sort()

(score, name), Kind = results[-1]

if score > 0: return Kind(filename)

else: return None

平时的笔记02:处理mp3的更多相关文章

  1. 平时的笔记02:处理fnmatch模块

    # Copyright 2006 Joe Wreschnig## This program is free software; you can redistribute it and/or modif ...

  2. 软件测试之loadrunner学习笔记-02集合点

    loadrunner学习笔记-02集合点 集合点函数可以帮助我们生成有效可控的并发操作.虽然在Controller中多用户负载的Vuser是一起开始运行脚本的,但是由于计算机的串行处理机制,脚本的运行 ...

  3. 《30天自制操作系统》笔记(02)——导入C语言

    <30天自制操作系统>笔记(02)——导入C语言 进度回顾 在上一篇,记录了计算机开机时加载IPL程序(initial program loader,一个nas汇编程序)的情况,包括IPL ...

  4. 《The Linux Command Line》 读书笔记02 关于命令的命令

    <The Linux Command Line> 读书笔记02 关于命令的命令 命令的四种类型 type type—Indicate how a command name is inter ...

  5. 强化学习读书笔记 - 02 - 多臂老O虎O机问题

    # 强化学习读书笔记 - 02 - 多臂老O虎O机问题 学习笔记: [Reinforcement Learning: An Introduction, Richard S. Sutton and An ...

  6. JS自学笔记02

    JS自学笔记02 1.复习 js是一门解释性语言,遇到一行代码就执行一行代码 2.查阅mdn web文档 3.提示用户输入并接收,相比之下,alert只有提示的作用: prompt(字符串) 接收: ...

  7. 机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)

    机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN) 关键字:邻近算法(kNN: k Nearest Neighbors).python.源 ...

  8. CS229 笔记02

    CS229 笔记02 公式推导 $ {\text {For simplicity, Let }} A, B, C \in {\Bbb {R}}^{n \times n}. $ ​ $ {\bf {\t ...

  9. OpenCV 学习笔记 02 使用opencv处理图像

    1 不同色彩空间的转换 opencv 中有数百种关于不同色彩空间的转换方法,但常用的有三种色彩空间:灰度.BRG.HSV(Hue-Saturation-Value) 灰度 - 灰度色彩空间是通过去除彩 ...

随机推荐

  1. HR系统+人脸识别

    近期一直在写一套HR系统,这套HR系统和人脸识别相结合.全然杜绝取代刷卡的情况产生.系统的灵活性比較强,开发简洁高速. 例如以下是一些功能上的截图 考勤模块仅仅是列举当中一个请假申请的功能做为展示   ...

  2. JMeter简单性能测试(适合初学者)

    利用JMeter进行Web测试     JMeter介绍    脚本录制    运行JMeter进行测试    JMeter主要组件介绍    参数化设置    动态数据关联    使用命令行运行JM ...

  3. Understanding Abstractions of Secure Channels 的研读

  4. Unity 对象池的使用

    在游戏开发过程中,我们经常会遇到游戏发布后,测试时玩着玩着明显的感觉到有卡顿现象.出现这种现象的有两个原因:一是游戏优化的不够好或者游戏逻辑本身设计的就有问题,二是手机硬件不行.好吧,对于作为程序员的 ...

  5. windows消息常量值

    WM_NULL = 0WM_CREATE = 1应用程序创建一个窗口WM_DESTROY = 2一个窗口被销毁WM_MOVE = 3移动一个窗口WM_SIZE = 5改变一个窗口的大小WM_ACTIV ...

  6. BZOJ 1001 狼抓兔子 (网络流最小割/平面图的对偶图的最短路)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 算法讨论: 1.可以用最大流做,最大流等于最小割. 2.可以把这个图转化其对偶图,然 ...

  7. Qt窗口句柄

    关键字: 透明效果,异形,子窗口,控件,浮窗,同级句柄

  8. 读取Webpage表中的内容

    nutch将从网页中抓取到的信息放入hbase数据库中,默认情况下表名为$crawlId_webpage,但表中的内容以16进制进行表示,直接scan或者通过Java API进行读取均只能读取到16进 ...

  9. python中的函数存入list中的实例

    最近由于接触了python这个强大的东西,在写代码时考虑到代码的扩展性,就想到了将python的函数名存入list中.有点像习惯的c/c++中的函数指针的意思. 下面上代码: # coding=utf ...

  10. Scala学习笔记--集合类型Queue,Set

    补充知识:http://www.importnew.com/4543.html 正文开始 scala.collection.immutable scala.collection.mutable 队列Q ...