1 | #if 0 |
---|
2 | # ----------------------------------------------------------------------- |
---|
3 | # $Id: flacinfo.py,v 1.7 2003/10/05 21:06:24 outlyer Exp $ |
---|
4 | # ----------------------------------------------------------------------- |
---|
5 | # $Log: flacinfo.py,v $ |
---|
6 | # Revision 1.7 2003/10/05 21:06:24 outlyer |
---|
7 | # Cram the VORBIS_COMMENT fields into our standard artist/album/etc. fields |
---|
8 | # so they work in Freevo. |
---|
9 | # |
---|
10 | # The only thing missing from having perfect FLAC support is a way to |
---|
11 | # calculate the song length. |
---|
12 | # |
---|
13 | # Revision 1.6 2003/10/05 20:45:09 outlyer |
---|
14 | # Fix some minor python issues. It works from mediatest.py now, but Freevo |
---|
15 | # isn't using the tag information yet. I don't know why. |
---|
16 | # |
---|
17 | # Revision 1.5 2003/09/22 16:21:20 the_krow |
---|
18 | # o ogg parsing should basically work |
---|
19 | # o utf-8 for vorbis comments |
---|
20 | # |
---|
21 | # Revision 1.4 2003/08/30 09:36:22 dischi |
---|
22 | # turn off some debug based on DEBUG |
---|
23 | # |
---|
24 | # Revision 1.3 2003/08/27 02:53:48 outlyer |
---|
25 | # Still doesn't do anything, but at least compiles now; problem is I don't |
---|
26 | # know how to conver the endian "headers" in to the types we expect, and I'm |
---|
27 | # hardly an expert on binary data. |
---|
28 | # |
---|
29 | # But I flushed out the header types from the FLAC documentation and |
---|
30 | # hopefully Thomas will know what to do... |
---|
31 | # |
---|
32 | # I can provide a FLAC file if necessary... |
---|
33 | # |
---|
34 | # Revision 1.2 2003/08/26 21:21:18 outlyer |
---|
35 | # Fix two more Python 2.3 warnings. |
---|
36 | # |
---|
37 | # Revision 1.1 2003/08/18 13:39:52 the_krow |
---|
38 | # Initial Import. Started on frame parsing. |
---|
39 | # |
---|
40 | # ----------------------------------------------------------------------- |
---|
41 | # MMPython - Media Metadata for Python |
---|
42 | # Copyright (C) 2003 Thomas Schueppel, et. al |
---|
43 | # |
---|
44 | # This program is free software; you can redistribute it and/or modify |
---|
45 | # it under the terms of the GNU General Public License as published by |
---|
46 | # the Free Software Foundation; either version 2 of the License, or |
---|
47 | # (at your option) any later version. |
---|
48 | # |
---|
49 | # This program is distributed in the hope that it will be useful, but |
---|
50 | # WITHOUT ANY WARRANTY; without even the implied warranty of MER- |
---|
51 | # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
52 | # Public License for more details. |
---|
53 | # |
---|
54 | # You should have received a copy of the GNU General Public License along |
---|
55 | # with this program; if not, write to the Free Software Foundation, Inc., |
---|
56 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
57 | # |
---|
58 | # ----------------------------------------------------------------------- |
---|
59 | #endif |
---|
60 | |
---|
61 | from mmpython import mediainfo |
---|
62 | import mmpython.audio.ogginfo as ogginfo |
---|
63 | import mmpython |
---|
64 | import struct |
---|
65 | import re |
---|
66 | |
---|
67 | # See: http://flac.sourceforge.net/format.html |
---|
68 | |
---|
69 | |
---|
70 | class FlacInfo(mediainfo.MusicInfo): |
---|
71 | def __init__(self,file): |
---|
72 | mediainfo.MusicInfo.__init__(self) |
---|
73 | if file.read(4) != 'fLaC': |
---|
74 | self.valid = 0 |
---|
75 | return |
---|
76 | self.valid = 1 |
---|
77 | while 1: |
---|
78 | (blockheader,) = struct.unpack('>I',file.read(4)) |
---|
79 | lastblock = (blockheader >> 31) & 1 |
---|
80 | type = (blockheader >> 24) & 0x7F |
---|
81 | numbytes = blockheader & 0xFFFFFF |
---|
82 | if mmpython.mediainfo.DEBUG: |
---|
83 | print "Last?: %d, NumBytes: %d, Type: %d" % (lastblock, numbytes, type) |
---|
84 | # Read this blocks the data |
---|
85 | data = file.read(numbytes) |
---|
86 | if type == 0: |
---|
87 | # STREAMINFO |
---|
88 | bits = struct.unpack('>L', data[10:14])[0] |
---|
89 | self.samplerate = (bits >> 12) & 0xFFFFF |
---|
90 | self.channels = ((bits >> 9) & 7) + 1 |
---|
91 | self.samplebits = ((bits >> 4) & 0x1F) + 1 |
---|
92 | md5 = data[18:34] |
---|
93 | elif type == 1: |
---|
94 | # PADDING |
---|
95 | pass |
---|
96 | elif type == 2: |
---|
97 | # APPLICATION |
---|
98 | pass |
---|
99 | elif type == 3: |
---|
100 | # SEEKTABLE |
---|
101 | pass |
---|
102 | elif type == 4: |
---|
103 | # VORBIS_COMMENT |
---|
104 | skip, self.vendor = self._extractHeaderString(data) |
---|
105 | num, = struct.unpack('<I', data[skip:skip+4]) |
---|
106 | start = skip+4 |
---|
107 | header = {} |
---|
108 | for i in range(num): |
---|
109 | (nextlen, s) = self._extractHeaderString(data[start:]) |
---|
110 | start += nextlen |
---|
111 | a = re.split('=',s) |
---|
112 | header[(a[0]).upper()]=a[1] |
---|
113 | if header.has_key('TITLE'): |
---|
114 | self.title = header['TITLE'] |
---|
115 | if header.has_key('ALBUM'): |
---|
116 | self.album = header['ALBUM'] |
---|
117 | if header.has_key('ARTIST'): |
---|
118 | self.artist = header['ARTIST'] |
---|
119 | if header.has_key('COMMENT'): |
---|
120 | self.comment = header['COMMENT'] |
---|
121 | if header.has_key('DATE'): |
---|
122 | self.date = header['DATE'] |
---|
123 | if header.has_key('ENCODER'): |
---|
124 | self.encoder = header['ENCODER'] |
---|
125 | if header.has_key('TRACKNUMBER'): |
---|
126 | self.trackno = header['TRACKNUMBER'] |
---|
127 | |
---|
128 | self.appendtable('VORBISCOMMENT', header) |
---|
129 | elif type == 5: |
---|
130 | # CUESHEET |
---|
131 | pass |
---|
132 | else: |
---|
133 | # UNKNOWN TYPE |
---|
134 | pass |
---|
135 | if lastblock: |
---|
136 | break |
---|
137 | |
---|
138 | def _extractHeaderString(self,header): |
---|
139 | len = struct.unpack( '<I', header[:4] )[0] |
---|
140 | return (len+4,unicode(header[4:4+len], 'utf-8')) |
---|
141 | |
---|
142 | |
---|
143 | mmpython.registertype( 'application/flac', ('flac',), mediainfo.TYPE_MUSIC, FlacInfo ) |
---|