#! /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. 高效求幂取余 算法,复杂度 log(n)

    做TopCoder SRM 576 D2 L3 题目时,程序有个地方需要对一个数大量求幂并取余,导致程序运行时间很长,看了Editoral之后,发现一个超级高效的求幂并取余的算法,之前做System ...

  2. 网络直播电视之M3U8解析篇 (下)

    在上一篇文章中讲述了网络直播电视的M3U8解析和当中的keyword段.本章我将对我遇见到的不同数据源的M3U8文件进行列举和分析. 第一种:ts片段地址为文件名,下载地址为:http:\\www.X ...

  3. 为iPhone6设计自适应布局(一)

    译者的话:本文是自适应布局的巩固篇,所以对布局约束的添加操作步骤等没有详细的说明.如果看着吃力的话请先移步Swift自适应布局(Adaptive Layout)教程. Apple从iOS6加入了Aut ...

  4. C语言随记-1

    涉及指针.数组.函数指针 几种声明形式 int *a[5]; // a是一个有5个元素的数组,每个元素是整数类型指针(int *) int *a[] = {0x100, 0x104, 0x108, 0 ...

  5. css布局详解(二)——标准流布局(Nomal flow)

    css标准流布局(Nomal flow) 一.正常流 这是指西方语言中文本从左向右,从上向下显示,这也是我们熟悉的传统的HTML文档中的文本布局.注意,在非西方的语言中,流方向可能不同.大多数元素都在 ...

  6. (转)Javascript 面向对象编程(一):封装(作者:阮一峰)

    Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类). 那么,如果 ...

  7. Jquery:Jquery中的事件<一>

    由于今天有一个比较重要的面试,所以昨天晚上对以前做的一些项目做了一下总结,直接导致昨天的学习笔记断更了,哎,计划永远赶不上变化啊!今天学习了Jquery中是事件,就此做一个笔记,便于日后复习. 一.加 ...

  8. VS2010使用附加进程的方式调试IIS中的页面介绍

    1.       对要测试的页面设置断点,然后选择”调试”->”附加到进程”,在进程中选择w3wp.exe进程(需要先打开需测试的页面) 2.       配置完成,当打开你需要测试的页面的时候 ...

  9. Linux Shell(初识)

    什么是Shell:Shell是一个命令解释器. Linux下支持shell的种类: 1.  Bourne Shell(简称sh) 2.C Shell(简称csh) 3.Korn Shell(简称ksh ...

  10. [Mugeda HTML5技术教程之12]制作跨屏互动应用

    mugeda动画平台还可以用来制作跨屏互动的动画应用,比如在PC端的大屏幕上显示动画的主界面,同时会显示出供手机扫描的二维码,手机扫描后会在手机上显示手机端动画界面.通过手机就可以和PC端的显示界面跨 ...