def handle_audio_file(audio_file):
    print('Handling audio file', audio_file)
    extension = os.path.splitext(os.path.basename(audio_file))[1]
    if extension == '.mp3':
        mp3 = MP3(audio_file, ID3=ID3)
        print(list(dict(mp3).keys()))
        if not regex_list(dict(mp3).keys(), re.compile('[Aa][Pp][Ii][Cc].*')):
            artist = mp3['TPE1'][0]
            album = mp3['TALB'][0]
            cover_data = open(get_cover_art(artist, album, audio_file), mode='rb')
            apic = APIC()
            apic.encoding = 3
            apic.mime = 'image/jpg'
            apic.type = PictureType.COVER_FRONT
            apic.desc = u'Cover'
            apic.data = cover_data.read()
            cover_data.close()
            print('Adding cover art', cover_data.name, '->', audio_file)
            mp3['APIC'] = apic
            mp3.save()
        else:
            print(audio_file, 'already has cover artwork.')
    elif extension == '.m4a' or extension is '.aac':
        m4a = MP4(audio_file)
        print(list(dict(m4a).keys()))
        if 'covr' not in m4a:
            artist = m4a['\xa9ART'][0]
            album = m4a['\xa9alb'][0]
            cover_data = open(get_cover_art(artist, album, audio_file), mode='rb')
            covr = MP4Cover()
            covr.imageformat = AtomDataType.JPEG
            covr.data = cover_data.read()
            cover_data.close()
            print('Adding cover art', cover_data.name, '->', audio_file)
            m4a['covr'] = [covr]
            m4a.save()
        else:
            print(audio_file, 'already has cover artwork.')
    elif extension == '.flac':
        flac = FLAC(audio_file)
        print(list(dict(flac).keys()))
        if not flac.pictures:
            artist = flac['artist'][0]
            album = flac['album'][0]
            cover_data = open(get_cover_art(artist, album, audio_file), mode='rb')
            picture = Picture()
            picture.type = 3
            picture.mime = 'image/jpg'
            picture.desc = u'Cover'
            picture.data = cover_data.read()
            cover_data.close()
            print('Adding cover artwork', cover_data.name, '->', audio_file)
            flac.add_picture(picture)
            flac.save()
        else:
            print(audio_file, 'already has cover artwork.')
    move_or_overwrite(audio_file, dest_audio, os.path.join(dest_audio, os.path.basename(audio_file)))
Esempio n. 2
0
def add_id3_art(path, url):
    try:
        art = requests.get(url)
        art.raise_for_status()

        mp3 = MP3(path, ID3=ID3)
        art_tag = APIC()
        art_tag.encoding = 3
        art_tag.type = 3
        art_tag.desc = u"Album Cover"
        if url.endswith('png'):
            art_tag.mime = u"image/png"
        else:
            art_tag.mime = u"image/jpeg"
        art_tag.data = art.content
        mp3.tags.add(art_tag)
        mp3.save()
    except requests.exceptions.RequestException:
        return
    except id3_error:
        return
Esempio n. 3
0
def addCoverArt(filename, config):
    """Add cover art."""
    logger.info("Adding the Cover Image...")

    # Check that the file exists
    imageFilepath = config['episodeImageFilepath']

    if not os.path.isfile(imageFilepath):
        logger.fatal("\'" + imageFilepath + "\' does not exist.")
        exit(1)

    # Initialize the tag
    imageTag = APIC()

    # Determine the file type
    if fileUtils.extValid(imageFilepath.lower(), '.png'):
        imageTag.mime = 'image/png'
    elif fileUtils.extValid(imageFilepath.lower(), '.jpg'):
        imageTag.mime = 'image/jpeg'
    else:
        logger.fatal("Cover image must be a PNG or JPG.")
        exit(1)

    # Set the image tags
    imageTag.encoding = 3  # 3 is for utf-8
    imageTag.type = 3  # 3 is for cover image
    imageTag.desc = u'Cover'

    with open(imageFilepath, 'rb') as f:
        imageTag.data = f.read()

    # Add the tag using ID3
    try:
        mp3 = MP3(filename, ID3=ID3)
        mp3.tags.add(imageTag)
        mp3.save()
    except:
        raise
Esempio n. 4
0
def addCoverArt(filename, config):
    """Add cover art."""
    logger.info("Adding the Cover Image...")

    # Check that the file exists
    imageFilepath = config['episodeImageFilepath']

    if not os.path.isfile(imageFilepath):
        logger.fatal("\'" + imageFilepath + "\' does not exist.")
        exit(1)

    # Initialize the tag
    imageTag = APIC()

    # Determine the file type
    if fileUtils.extValid(imageFilepath.lower(), '.png'):
        imageTag.mime = 'image/png'
    elif fileUtils.extValid(imageFilepath.lower(), '.jpg'):
        imageTag.mime = 'image/jpeg'
    else:
        logger.fatal("Cover image must be a PNG or JPG.")
        exit(1)

    # Set the image tags
    imageTag.encoding = 3  # 3 is for utf-8
    imageTag.type = 3      # 3 is for cover image
    imageTag.desc = u'Cover'

    with open(imageFilepath, 'rb') as f:
        imageTag.data = f.read()

    # Add the tag using ID3
    try:
        mp3 = MP3(filename, ID3=ID3)
        mp3.tags.add(imageTag)
        mp3.save()
    except:
        raise