Changeset 315

Show
Ignore:
Timestamp:
06/17/06 21:14:10 (3 years ago)
Author:
nicfit
Message:

Use album cover from file if one odes not exist in same dir (closes #65)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/album_cover_control.py

    r304 r315  
    1818#  $Id$ 
    1919################################################################################ 
    20 import gtk, gtk.glad
    21 import os, gobject 
     20import os, tempfil
     21import gobject, gtk, gtk.glade 
    2222 
    2323import mesk 
     
    8585 
    8686    def _on_audio_source_changed(self, ctrl, old, new): 
     87        # FIXME: There is no need to do this if the file is in the same dir 
     88        # or the album name has not changed. 
     89 
     90        # Search for a cover in the same directory as the source 
    8791        dir = os.path.dirname(new[1].uri.path) 
    8892        for fn in self.COVER_NAMES: 
     
    9296                    self._set_cover(cover_file) 
    9397                    return 
     98 
     99        # See if the tag has an image 
     100        src = new[1] 
     101        img = new[1].get_cover_image() 
     102        if img: 
     103            mimetype, data = img 
     104            # XXX: gdk_pixbuf_new_from_data scares me, but it would be better 
     105            # than this tmp file solution 
     106            ext = '.%s' % mimetype.split("/")[1] 
     107            file_d, filename = tempfile.mkstemp(ext) 
     108            os.write(file_d, data) 
     109            os.close(file_d) 
     110            self._set_cover(filename) 
     111            os.remove(filename) 
     112            return 
    94113 
    95114        self._set_cover(self.DEFAULT_COVER) 
  • trunk/src/mesk/audio/mp3.py

    r313 r315  
    1919################################################################################ 
    2020import gst, eyeD3 
     21from eyeD3.frames import ImageFrame 
    2122 
    2223gst.element_factory_make("mad") 
     
    3940            raise UnsupportedScheme('\'%s\' URI scheme is not supported' % \ 
    4041                                    uri.scheme) 
     42 
     43    def get_native_tag(self): 
     44        '''Returns the eyeD3.Tag for this source, or None''' 
     45        if self.uri.scheme != 'file': 
     46            return None 
     47 
     48        try: 
     49            tag = eyeD3.Tag() 
     50            if tag.link(self.uri.path): 
     51                return tag 
     52        except TagException, ex: 
     53            return None 
     54 
     55    def get_cover_image(self): 
     56        # Images are loaded lazily 
     57        if self.meta_data.has_images: 
     58            tag = self.get_native_tag() 
     59            if tag: 
     60                imgs = tag.getImages() 
     61                # Search for acceptable covers 
     62                for img in tag.getImages(): 
     63                    # TODO: back cover support 
     64                    for type in [ImageFrame.FRONT_COVER, ImageFrame.OTHER]: 
     65                        if img.pictureType == type: 
     66                            return (img.mimeType, img.imageData) 
     67        return None 
     68 
    4169 
    4270from eyeD3 import TagException, InvalidAudioFormatException 
     
    6694                self.year = tag.getYear() 
    6795                (self.track_num, self.track_total) = tag.getTrackNum() 
     96                self.has_images = len(tag.getImages()) > 0 
    6897                self.frozen = True 
    6998factory = Mp3AudioSource 
  • trunk/src/mesk/audio/oggvorbis.py

    r314 r315  
    2828 
    2929from source import AudioSource, AudioMetaData 
     30from ogg.vorbis import VorbisFile, VorbisError 
    3031class OggAudioSource(AudioSource): 
    3132    def __init__(self, uri): 
     
    4041                                    uri.scheme) 
    4142 
    42 from ogg.vorbis import VorbisFile, VorbisError 
     43    def get_native_tag(self): 
     44        '''Returns the VorbisComment object, if any''' 
     45        if self.uri.scheme != 'file': 
     46            return None 
     47        try: 
     48            tag = VorbisFile(self.uri.path) 
     49            if tag.comment(): 
     50                return tag.comment() 
     51        except VorbisError, ex: 
     52            return None 
     53 
    4354class OggMetaData(AudioMetaData): 
    4455    def __init__(self, uri = None): 
  • trunk/src/mesk/audio/source.py

    r313 r315  
    3535    year = None 
    3636    track_num = track_total = None 
     37 
     38    # Use to freeze the state from being updated 
    3739    frozen = False 
     40    # True when the meta data includes images.   
     41    has_images = False 
    3842 
    39     def __init__(self): pass 
     43    def __init__(self): 
     44        pass 
    4045 
    4146class AudioSource: 
     
    4651            self.uri = uri 
    4752        self.meta_data = AudioMetaData() 
     53 
     54    def get_native_tag(self): 
     55        return None 
     56 
     57    def get_cover_image(self): 
     58        '''Return None, or a 2-tuple of the form (mimetype, imgdata)''' 
     59        return None