Update of /cvsroot/freevo/freevo/WIP/Aubin
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22585

Added Files:
        flacinfo.py 
Log Message:
A bad hack to get the total samples in a FLAC file in order to calculate
 the song length. It uses 'metaflac' to do so since I have no idea how to
 parse the STREAMINFO block. It does calculate the song lengths properly, but
 this is hardly the most elegant way of doing it.

I'm hoping someone who knows how to parse binary can add it since it's the
only relevant field in a FLAC file we can't parse.



--- NEW FILE: flacinfo.py ---
#if 0
# -----------------------------------------------------------------------
# $Id: flacinfo.py,v 1.1 2004/07/24 20:48:36 outlyer Exp $
# -----------------------------------------------------------------------
# $Log: flacinfo.py,v $
# Revision 1.1  2004/07/24 20:48:36  outlyer
# A bad hack to get the total samples in a FLAC file in order to calculate
#  the song length. It uses 'metaflac' to do so since I have no idea how to
#  parse the STREAMINFO block. It does calculate the song lengths properly, but
#  this is hardly the most elegant way of doing it.
#
# I'm hoping someone who knows how to parse binary can add it since it's the
# only relevant field in a FLAC file we can't parse.
#
# Revision 1.7  2003/10/05 21:06:24  outlyer
# Cram the VORBIS_COMMENT fields into our standard artist/album/etc. fields
# so they work in Freevo.
#
# The only thing missing from having perfect FLAC support is a way to
# calculate the song length.
#
# Revision 1.6  2003/10/05 20:45:09  outlyer
# Fix some minor python issues. It works from mediatest.py now, but Freevo
# isn't using the tag information yet. I don't know why.
#
# Revision 1.5  2003/09/22 16:21:20  the_krow
# o ogg parsing should basically work
# o utf-8 for vorbis comments
#
# Revision 1.4  2003/08/30 09:36:22  dischi
# turn off some debug based on DEBUG
#
# Revision 1.3  2003/08/27 02:53:48  outlyer
# Still doesn't do anything, but at least compiles now; problem is I don't
# know how to conver the endian "headers" in to the types we expect, and I'm
# hardly an expert on binary data.
#
# But I flushed out the header types from the FLAC documentation and
# hopefully Thomas will know what to do...
#
# I can provide a FLAC file if necessary...
#
# Revision 1.2  2003/08/26 21:21:18  outlyer
# Fix two more Python 2.3 warnings.
#
# Revision 1.1  2003/08/18 13:39:52  the_krow
# Initial Import. Started on frame parsing.
#
# -----------------------------------------------------------------------
# MMPython - Media Metadata for Python
# Copyright (C) 2003 Thomas Schueppel, et. al
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# 
# -----------------------------------------------------------------------
#endif

from mmpython import mediainfo
import mmpython.audio.ogginfo as ogginfo
import mmpython
import struct
import re

# See: http://flac.sourceforge.net/format.html


class FlacInfo(mediainfo.MusicInfo):
    def __init__(self,file):
        mediainfo.MusicInfo.__init__(self)
        if file.read(4) != 'fLaC':
            self.valid = 0
            return
        self.valid = 1
        while 1:
            (blockheader,) = struct.unpack('>I',file.read(4))
            lastblock = (blockheader >> 31) & 1
            type = (blockheader >> 24) & 0x7F
            numbytes = blockheader & 0xFFFFFF
            if mmpython.mediainfo.DEBUG:
                print "Last?: %d, NumBytes: %d, Type: %d" % (lastblock, numbytes, 
type)                                
            # Read this blocks the data
            data = file.read(numbytes)
            if type == 0:
                # STREAMINFO
                bits = struct.unpack('>L', data[10:14])[0]
                self.samplerate = (bits >> 12) & 0xFFFFF
                self.channels = ((bits >> 9) & 7) + 1
                self.samplebits = ((bits >> 4) & 0x1F) + 1
                self.total = self._totalhelper(file)
                self.length = self.total / self.samplerate
                md5 = data[18:34]
            elif type == 1:
                # PADDING
                pass            
            elif type == 2:
                # APPLICATION 
                pass            
            elif type == 3:
                # SEEKTABLE 
                pass            
            elif type == 4:
                # VORBIS_COMMENT                
                skip, self.vendor = self._extractHeaderString(data)
                num, = struct.unpack('<I', data[skip:skip+4])
                start = skip+4
                header = {}
                for i in range(num):
                    (nextlen, s) = self._extractHeaderString(data[start:])
                    start += nextlen
                    a = re.split('=',s)
                    header[(a[0]).upper()]=a[1]
                if header.has_key('TITLE'):
                    self.title = header['TITLE']
                if header.has_key('ALBUM'):
                    self.album = header['ALBUM']
                if header.has_key('ARTIST'):
                    self.artist = header['ARTIST']            
                if header.has_key('COMMENT'):
                    self.comment = header['COMMENT']
                if header.has_key('DATE'):
                    self.date = header['DATE']
                if header.has_key('ENCODER'):
                    self.encoder = header['ENCODER']
                if header.has_key('TRACKNUMBER'):
                    self.trackno = header['TRACKNUMBER']
 
                self.appendtable('VORBISCOMMENT', header)
            elif type == 5:
                # CUESHEET 
                pass
            else:
                # UNKNOWN TYPE
                pass
            if lastblock:
                break

    def _totalhelper(self, file):
        try:
            import popen2
            METAFLAC_EXE='/usr/bin/metaflac'
            filename = file.name
            command='--show-total-samples'
            child = popen2.Popen3('%s %s "%s"' % (METAFLAC_EXE, command, filename))
            list = child.fromchild.readlines()
            total = int(list[0].replace('\n',''))
            return total
        except:
            pass



    def _extractHeaderString(self,header):
        len = struct.unpack( '<I', header[:4] )[0]
        return (len+4,unicode(header[4:4+len], 'utf-8'))

mmpython.registertype( 'application/flac', ('flac',), mediainfo.TYPE_MUSIC, FlacInfo ) 
               



-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click
_______________________________________________
Freevo-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to