def initiateWAVE(filename, directory, thumbnails, options):
    audio = WAVE(str(directory) + "/" + str(filename))
    # verify artist information is present before preceeding
    if ' - ' not in filename and str(audio['TCON']) == '':
        messagebox.showinfo("No artist information found, aborting procedure")
        return False, False, False
    # transcribe formal tagnames into informal counterpart
    formalTagDict = {
        'TPE1': 'Artist',
        'TALB': 'Album',
        'TPE2': 'Album Artist',
        'TBPM': 'BPM',
        'COMM::eng': 'Comment',
        'TCMP': 'Compilation',
        'TCOP': 'Copyright',
        'TPOS': 'Discnumber',
        'TCON': 'Genre',
        'APIC:': 'Image',
        'TKEY': 'Key',
        'TDRC': 'Release_Date',
        'TIT2': 'Title',
        'TXXX:replaygain_track_gain': 'ReplayGain',
    }
    # transcribe informal tagnames into formal counterpart
    informalTagDict = {v: k for k, v in formalTagDict.items()}

    ID3Frames = {
        'TPE1': TPE1,
        'TALB': TALB,
        'TPE2': TPE2,
        'TBPM': TBPM,
        'COMM': COMM,
        'TCMP': TCMP,
        'TCOP': TCOP,
        'TPOS': TPOS,
        'TCON': TCON,
        'APIC:': APIC,
        'TKEY': TKEY,
        'TDRC': TDRC,
        'TIT2': TIT2,
        'TXXX': TXXX,
    }
    fileParameters = []
    tagList = list(audio.keys())
    for tag in tagList:
        # delete extraneous tags if the tag is not in the list of selected tags and the delete unselected tags option is activated
        if (tag not in formalTagDict
                or formalTagDict[tag] not in options["Selected Tags (L)"]
            ) and options["Delete Unselected Tags (B)"].get() == True:
            audio.pop(tag)
            audio.save()
        else:
            fileParameters.append(tag)
    for tag in options["Selected Tags (L)"]:
        if tag in informalTagDict:
            tag = informalTagDict[tag]
            # add tags of interest if missing
            if tag not in fileParameters:
                try:
                    if "COMM" in tag:
                        audio[tag] = COMM(encoding=3, lang="eng", text="")
                    elif "TXXX" in tag:
                        audio[tag] = TXXX(encoding=3,
                                          desc="replaygain_track_gain",
                                          text="")
                    else:
                        audio[tag] = ID3Frames[tag](encoding=3, text="")
                    audio.save()
                except:
                    messagebox.showinfo(
                        "Permission Error",
                        "Unable to save tags, file may be open somewhere")
                    return False, False, False
    # check for discrepancies between tags and filename
    # check both artist and title tags
    if ' - ' in filename:
        artist = filename.split(' - ')[0]
        title = filename[filename.index(filename.split(' - ')[1]):filename.
                         rfind('.')]
        if artist != str(audio["TPE1"]) or title != str(audio["TIT2"]):
            # save artist and title to tag if both are empty
            if str(audio["TPE1"]) == '' and str(audio["TIT2"]) == '':
                audio["TPE1"] = TPE1(encoding=3, text=artist)
                audio["TIT2"] = TIT2(encoding=3, text=title)
                audio.save()
            else:
                audio, filename = compareArtistAndTitle(
                    audio, artist, title, filename, directory, options)
    # only check title tag
    else:
        title = filename[:filename.rfind('.')]
        if title != str(audio["TIT2"]):
            # save title to tag if tag is empty
            if str(audio["TIT2"]) == '':
                audio["TIT2"] = TIT2(encoding=3, text=title)
                audio.save()
            else:
                audio, filename = compareTitle(audio, title, filename,
                                               directory, options)

    # handle naming format and typo check
    if options["Audio naming format (S)"].get() == "Artist - Title" or options[
            'Audio naming format (S)'].get() == 'Title':
        namingConvention = options['Audio naming format (S)'].get()
        artist = str(audio["TPE1"])
        audio, filename = handleStaticNamingConvention(audio, filename, artist,
                                                       title, directory,
                                                       namingConvention)
        if options["Scan Filename and Tags (B)"].get(
        ) == True and type(audio) != bool:
            audio, filename, options = extractArtistAndTitle(
                audio, filename, directory, options, namingConvention)

    if type(audio) != bool:
        # save thumbnail to list
        image = audio["APIC:"]
        if image.data != b'':
            stream = BytesIO(image.data)
            image = Image.open(stream).convert("RGBA")
            thumbnails = saveThumbnail(image, thumbnails)
            stream.close()
        else:
            thumbnails = saveThumbnail("NA", thumbnails)
    return audio, filename, informalTagDict, thumbnails, options
Ejemplo n.º 2
0
def initiateFLAC(filename, directory, thumbnails, options):
    audio = FLAC(str(directory) + "/" + str(filename))
    # verify artist information is present before preceeding
    if ' - ' not in filename and str(audio['artist'][0]) == '':
        messagebox.showinfo("No artist information found, aborting procedure")
        return False, False, False

    # transcribe formal tagnames into informal counterpart
    formalTagDict = {
        'artist': 'Artist',
        'album': 'Album',
        'albumartist': 'Album_Artist',
        'bpm': 'BPM',
        'comment': 'Comment',
        'compilation': 'Compilation',
        'copyright': 'Copyright',
        'discnumber': 'Discnumber',
        'genre': 'Genre',
        'initialkey': 'Key',
        'date': 'Release_Date',
        'title': 'Title',
        'replaygain_track_gain': 'ReplayGain',
    }
    # transcribe informal tagnames into formal counterpart
    informalTagDict = {v: k for k, v in formalTagDict.items()}
    fileParameters = []
    for tag in audio:
        # delete extraneous tags if the tag is not in the list of selected tags and the delete unselected tags option is activated
        if (tag not in formalTagDict
                or formalTagDict[tag] not in options["Selected Tags (L)"]
            ) and options["Delete Unselected Tags (B)"].get() == True:
            audio[tag] = ""
            audio.pop(tag)
            audio.save()
        else:
            fileParameters.append(tag)
    for tag in options["Selected Tags (L)"]:
        if tag in informalTagDict:
            tag = informalTagDict[tag]
            # add tags of interest if missing
            if tag not in fileParameters:
                try:
                    audio[tag] = ""
                    audio.save()
                except:
                    messagebox.showinfo(
                        "Permission Error",
                        "Unable to save tags, file may be open somewhere")
                    return False, False, False

    # check for discrepancies between tags and filename
    # check both artist and title tags
    if ' - ' in filename:
        artist = filename.split(' - ')[0]
        title = filename[filename.index(filename.split(' - ')[1]):filename.
                         rfind('.')]
        if artist != str(audio['artist'][0]) or title != str(
                audio['title'][0]):
            # save artist and title to tag if both are empty
            if str(audio['artist'][0]) == '' and str(audio['title'][0]) == '':
                audio['artist'] = artist
                audio['title'] = title
                audio.save()
            else:
                audio, filename = compareArtistAndTitle(
                    audio, artist, title, filename, directory, options)
    # only check title tag
    else:
        title = filename[:filename.rfind('.')]
        if title != str(audio['title'][0]):
            # save title to tag if tag is empty
            if str(audio['title'][0]) == '':
                audio['title'] = title
                audio.save()
            else:
                audio, filename = compareTitle(audio, title, filename,
                                               directory, options)

    # handle naming format and typo check
    namingConvention = options['Audio naming format (S)'].get()
    artist = str(audio['artist'][0])
    audio, filename = handleStaticNamingConvention(audio, filename, artist,
                                                   title, directory,
                                                   namingConvention)
    if options["Scan Filename and Tags (B)"].get(
    ) == True and type(audio) != bool:
        audio, filename, options = extractArtistAndTitle(
            audio, filename, directory, options, namingConvention)
    if type(audio) != bool:
        # save thumbnail to list
        images = audio.pictures
        # append thumbnail image to list if artwork exists
        if len(images) > 0:
            stream = BytesIO(images[0].data)
            image = Image.open(stream).convert("RGBA")
            thumbnails = saveThumbnail(image, thumbnails)
            stream.close()
        else:
            thumbnails = saveThumbnail("NA", thumbnails)
    return audio, filename, informalTagDict, thumbnails, options
def initiateM4A(filename, directory, thumbnails, options):
    audio = MP4(str(directory) + "/" + str(filename))
    # verify artist information is present before preceeding
    if ' - ' not in filename and str(audio["\xa9ART"][0]) == '':
        messagebox.showinfo("No artist information found, aborting procedure")
        return False, False, False

    # transcribe formal tagnames into informal counterpart
    formalTagDict = {
        "\xa9ART": 'Artist',
        "\alb": 'Album',
        "aART": 'Album_Artist',
        "tmpo": 'BPM',
        "\xa9cmt": 'Comment',
        "cpil": 'Compilation',  #bool
        "cprt": 'Copyright',
        'disk': 'Discnumber',  #[0]
        "\xa9gen": 'Genre',
        "covr": 'Image',
        "----:com.apple.iTunes:INITIALKEY": 'Key',
        "\xa9day": 'Release_Date',
        "\xa9nam": 'Title',
        "----:com.apple.iTunes:replaygain_track_gain": 'ReplayGain',
    }

    # transcribe informal tagnames into formal counterpart
    informalTagDict = {v: k for k, v in formalTagDict.items()}
    fileParameters = []
    tagList = list(audio.keys())
    for tag in tagList:
        # delete extraneous tags if the tag is not in the list of selected tags and the delete unselected tags option is activated
        if (tag not in formalTagDict
                or formalTagDict[tag] not in options["Selected Tags (L)"]
            ) and options["Delete Unselected Tags (B)"].get() == True:
            audio[tag] = ""
            audio.pop(tag)
            audio.save()
        else:
            fileParameters.append(tag)
    for tag in options["Selected Tags (L)"]:
        if tag in informalTagDict:
            tag = informalTagDict[tag]
            # add tags of interest if missing
            if tag not in fileParameters:
                try:
                    audio[tag] = ""
                    audio.save()
                except:
                    messagebox.showinfo(
                        "Permission Error",
                        "Unable to save tags, file may be open somewhere")
                    return False, False, False

    # check for discrepancies between tags and filename
    # check both artist and title tags
    if ' - ' in filename:
        artist = filename.split(' - ')[0]
        title = filename[filename.index(filename.split(' - ')[1]):filename.
                         rfind('.')]
        if artist != str(audio["\xa9ART"][0]) or title != str(
                audio["\xa9nam"][0]):
            # save artist and title to tag if both are empty
            if str(audio["\xa9ART"][0]) == '' and str(
                    audio["\xa9nam"][0]) == '':
                audio["\xa9ART"] = artist
                audio["\xa9nam"] = title
                audio.save()
            else:
                audio, filename = compareArtistAndTitle(
                    audio, artist, title, filename, directory, options)
    # only check title tag
    else:
        title = filename[:filename.rfind('.')]
        if title != str(audio["\xa9nam"][0]):
            # save title to tag if tag is empty
            if str(audio["\xa9nam"][0]) == '':
                audio["\xa9nam"] = title
                audio.save()
            else:
                audio, filename = compareTitle(audio, title, filename,
                                               directory, options)

    # handle naming format and typo check
    if options["Audio naming format (S)"].get() == "Artist - Title" or options[
            'Audio naming format (S)'].get() == 'Title':
        namingConvention = options['Audio naming format (S)'].get()
        artist = str(audio["\xa9ART"][0])
        audio, filename = handleStaticNamingConvention(audio, filename, artist,
                                                       title, directory,
                                                       namingConvention)
        if options["Scan Filename and Tags (B)"].get(
        ) == True and type(audio) != bool:
            audio, filename, options = extractArtistAndTitle(
                audio, filename, directory, options, namingConvention)

    if type(audio) != bool:
        # save thumbnail to list
        image = audio["covr"]
        if len(image) != 0:
            stream = BytesIO(image[0])
            image = Image.open(stream).convert("RGBA")
            thumbnails = saveThumbnail(image, thumbnails)
            stream.close()
        else:
            thumbnails = saveThumbnail("NA", thumbnails)
    return audio, filename, informalTagDict, thumbnails, options