Tag Archives: Python

Embedcover-0.1 released

Embedcover is a command tool to automatically embed album cover images into music files. It now only supports MP3 audio files and grabs album arts from Douban(http://www.douban.com) using Douban API.

Requires
2.3 <= Python < 3.0
mutagen (a Python module to handle audio metadata.)

Install
Make sure you have Python 2.x and mutagen installed.


   $ sudo python setup.py install

How to use:

  Usage: embedcover [options]
  Options:
    -h, --help            show this help message and exit
    -v, --verbose         be verbose(the default)
    -q, --quiet           be quiet
    -r, --recursive       deal with directories recursively
    -d DIRECTORY, --dir=DIRECTORY
                          the directory with music files to deal with

Example


  $ embedcover -r -d ~/music

Handle all MP3 files under ~/music and subdirectories

Note
To grab the correct album art, you’d better have the album titles specified in the ID3 tag of the music files.

The project is hosted at http://github.com/pipitu/embedcover/ . If any issues or bugs, please feel free to contact me:)

Gentoo配置手记(10/08/07)

我又开始折腾了-___,-

1. 不小心删除了Python
按Gentoo handbook折腾了一天把系统安装好了,于是我就决定emerge一下Python,结果把Python3也给装上了。想删除Python3结果直接把Python2.6一并咔嚓了。彼时还没有认识到问题的严重性,结果后来发现emerge命令执行之后完全没反应,问题严重了。翻开Portage Introduction:

Portage is completely written in Python and Bash.

Gentoo的Portage系统是依赖于Python的,so don’t ever unmerge Python completely!
问题解决方法就是重新编译安装Python:

# cd
# tar xzf /usr/portage/distfiles/Python-2.6.5.tgz
# cd Python-2.6.5
# ./configure --with-fpectl --infodir=/usr/share/info/ --mandir=/usr/share/man
# make
# make install prefix=/usr
# rm /usr/bin/python 2>/dev/null
# ln -s /usr/bin/python2 /usr/bin/python

Reference: [Gentoo Forums] I unmerged python… ensuing problems….

Read more »

给mp3批量嵌入专辑封面

鉴于本人素ID3 tag严重洁癖者,之前有用easytag手动给几千个mp3改过tag= = easytag的界面不错,就是不能批量修改,导致那两天改的我手都酸的不行了….入了n1以后有一点不爽的就是不管啥音乐软件,听歌的时候就是抓不到一张封面,那个默认的灰色封面实在让我火大,就用python写了个从豆瓣抓封面图片直接嵌入ID3 tag的程序,一劳永逸了~~

读写ID3 tag信息

对ID3信息的读取和修改依赖了mutagen库,当然用之前还是要对ID3标准有一个基本的了解。

from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error

audio = MP3(self.filename, ID3=ID3)
try:
    audio.add_tags()
except error:
    pass

有可能音乐文件一开始并没有tag(虽然机率比较小),所以调用add_tags()方法初始化一下,一般情况下都会抛出个error,提示已经有tag了。

album = self.audio['TALB'][0]
frames = self.audio.tags.getall("APIC")
for frame in frames:
    ext = ".img"
    if frame.mime == "image/jpeg" or frame.mime == "image/jpg":
        ext = ".jpg"
    elif frame.mime == "image/png":
        ext = ".png"
    elif frame.mime == "image/gif":
        ext = ".gif"

如果tag中已经包含唱片标题信息,那么audio.keys()的返回结果中就会包含”TALB”这一项,其他还有比如”TLE1″(表演者),“TCON”(流派)等等。ID3标准中规定代表唱片封面信息的帧结构如下:

audio.tags.add(APIC(encoding = 3,mime = "image/jpeg",type = 3,
desc = u'Cover',data=imagedata))
audio.save()

encoding = 3表示utf8编码,mime可以为”image/jpeg”(“image/jpg”)或者”image/png”格式,type=3表示写入的是front cover,desc是描述信息,data是图片的二进制数据,可以从某个uri读取,也可以从本地读取。

Read more »

List performance: Python vs Java

这篇post的主要由来是某次算法作业误用Python做了无用功,性能完全达不到要求,后来换用Java才搞定。作业是一个经典的算法问题:求最近点对,要求的输入数量级达到百万。Java做出来的结果大约是5秒左右,Python的话就完全废了,原因是其中涉及到庞大的list的new操作。

Python list VS Java array

首先做一个简单的实验,分别计算用Python和Java新建一个list的时间。

Python代码:

li = []
for i in range(size):
    li.append(0)

Java代码:

int[] li = new int[size];

结果对比如下:
[table id=1 /]
二者的时间复杂度都是O(n),但在常数上相差两个数量级。究其原因,
The time needed to append an item to the list is “amortized constant”; whenever the list needs to allocate more memory, it allocates room for a few items more than it actually needs, to avoid having to reallocate on each call (this assumes that the memory allocator is fast; for huge lists, the allocation overhead may push the behaviour towards O(n*n)).
也就是说,Python中list的append操作仅当空间不够时才另分配一块新的空间,根据平摊分析(Amortized Analysis)原理,创建一个长度为n的list的时间为c*n(c为平摊常数)。而Java的数组的new操作是一次分配一整快空间,这就不难解释为何两者的性能差异如此之大了。

Read more »