Beispiel #1
0
def crawl(path):
	# Crawls through my music directory, adding any new songs that aren't already in the library
	for item in os.listdir(MUSIC_PATH + path):
		if os.path.isfile(MUSIC_PATH + path + '/' + item):
			if item.split('.')[-1].lower() == 'mp3' and path + '/' + item not in songs_elo:
				print("adding new song")

				audiofile = mutagen.File(MUSIC_PATH + path + '/' + item)
				title = str(audiofile.get("TIT2", "Unknown"))
				print(title)
				print(item)
				text = audiofile.tags.getall('TXXX')
				elo = 1000
				for t in text:
					if t.desc == 'elo':
						elo = t.text
				artist = str(audiofile.get("TPE1", "Unknown"))
				songs_elo[path + '/' + item] = {"elo": elo, "title": title, "artist": artist, "n": 0}
		elif os.path.isdir(MUSIC_PATH + path + '/' + item):
			print(path + '/' + item)
			crawl(path + '/' + item)
Beispiel #2
0
def check_tags(filename, check_tracknumber_format=True):
    """Verify that the file has the required Xanax.rip tags.

    Returns (True, None) if OK, (False, msg) if a tag is missing or
    invalid.

    """
    info = mutagen.File(filename, easy=True)
    for tag in ['artist', 'album', 'title', 'tracknumber']:
        if tag not in info.keys():
            return (False, '"%s" has no %s tag' % (filename, tag))
        elif info[tag] == [u'']:
            return (False, '"%s" has an empty %s tag' % (filename, tag))

    if check_tracknumber_format:
        tracknumber = info['tracknumber'][0]
        if not valid_fractional_tag(tracknumber):
            return (False, '"%s" has a malformed tracknumber tag ("%s")' %
                    (filename, tracknumber))

    return (True, None)
Beispiel #3
0
    def test_foobar2k_replaygain(self):
        # foobar2k saved gain there
        f = mutagen.File(self.filename)
        f.tags.add(
            mutagen.id3.TXXX(encoding=3,
                             desc="replaygain_track_gain",
                             text=["-6 db"]))
        f.save()
        song = self.KIND(self.filename)
        self.failIfAlmostEqual(song.replay_gain(["track"]), 1.0, 1)

        # check if all keys are str
        for k in self.KIND(self.filename).keys():
            self.failUnless(isinstance(k, str))

        # remove value, save, reload and check if still gone
        del song["replaygain_track_gain"]
        song.write()
        song.reload()

        self.failUnlessAlmostEqual(song.replay_gain(["track"]), 1.0, 1)
Beispiel #4
0
def getTitle(sn):
    filePath = MUSICFILES_DIR + str(sn) + '.tmp'
    if isAMusicFile(filePath) != True:
        return 'Unknown filename'

    try:
        audio = EasyID3(filePath)
    except:
        audio = mutagen.File(filePath, easy=True)

    if audio is None:
        return getOriginalName(sn)

    if 'title' not in audio:
        if getOriginalName(sn) != False:
            audio['title'] = getOriginalName(sn)
            return getOriginalName(sn)
        else:
            return 'Unknown filename'
    else:
        return audio['title'][0]
def extractMetadata():
    printd("Inputfile: {}".format(inputfile))
    mp3 = mutagen.File(inputfile)
    metadata = dict()
    metadata['CHAP'] = dict()
    for key in mp3.keys():
        data = mp3.tags.getall(key)
        for d in data:
            if isText(d):
                metadata[type(d).__name__] = extractText(d)
            elif isTimestamp(d):
                metadata[type(d).__name__] = extractTimestamp(d)
            elif isinstance(d, id3.CTOC):
                metadata['CTOC'] = extractChapterTOC(d)
            elif isinstance(d, id3.CHAP):
                metadata['CHAP'] = appendChapterData(d, metadata['CHAP'])
            elif isinstance(d, id3.APIC):
                metadata['APIC'] = "Has Image Data"
            else:
                metadata[type(d).__name__] = "Unknown type, fixme"
    return metadata
Beispiel #6
0
    def get_shootid_through_metadata(file_path):
        """ Works only on Kink.com movies from around 3500-4500 (or with our own tags) """
        try:
            metadata = mutagen.File(file_path)
        except mutagen.MutagenError:
            metadata = None

        if metadata is not None:
            try:
                # the original, legacy Title of shootids 3500-4500
                title = metadata.get('Title')
                if title:
                    return re.search(r"Production: (<?P=shoot_id>\d+)\.",
                                     title).group("shoot_id")
                else:
                    # Kinksorter writes metadata with kinksorter_*
                    return int(metadata.get("kinksorter_shootid"))
            except (ValueError, TypeError, AttributeError):
                return 0

        return 0
Beispiel #7
0
def check_for_multi_artist_album(artist, album):
    directory = basedir + '/' + artist + '/' + album
    all_files = list()
    artist_list = list()
    for item in scandir(path=directory):
        if (item.is_file()
                and (mp3_re.match(item.name) or m4a_re.match(item.name))):
            all_files.append(item.name)
    for file in all_files:
        handler = mutagen.File(directory + '/' + file)
        track_artist = str()
        if m4a_re.match(file):
            track_artist = handler['©ART'][0]
        elif mp3_re.match(file):
            track_artist = handler['TPE1'].text[0]
        if track_artist not in artist_list:
            artist_list.append(track_artist)
    if len(artist_list) > 1:
        return artist_list
    else:
        return None
Beispiel #8
0
    def save_md_to_file(self, m):
        try:
            airtime_file = mutagen.File(m['MDATA_KEY_FILEPATH'], easy=True)

            for key in m.keys() :
                if key in self.airtime2mutagen:
                    value = m[key]
                    if (value is not None):
                        self.logger.debug("Saving %s to file", key)
                        self.logger.debug(value)
                        if isinstance(value, basestring) and (len(value) > 0):
                            airtime_file[self.airtime2mutagen[key]] = unicode(value, "utf-8")
                        elif isinstance(value, int):
                            airtime_file[self.airtime2mutagen[key]] = str(value)


            airtime_file.save()
        except Exception, e:
            self.logger.error('Trying to save md')
            self.logger.error('Exception: %s', e)
            self.logger.error('Filepath %s', m['MDATA_KEY_FILEPATH'])
def _extract_metadata(tid, path):
    """Extract metadata from one audio file."""

    #creer une serie qui respecte l'ordre d'indexation donnee par la liste tid (tid du track)
    metadata = pd.Series(name=tid)

    try:
        path = utils.get_audio_path(path, tid)
        #mutagen gere les metadonnees des audios
        f = mutagen.File(path)
        #return signal and rate of the audio
        x, sr = librosa.load(path, sr=None, mono=False)
        #verification des valeurs extraite par librosa et mutagen
        assert f.info.channels == (x.shape[0] if x.ndim > 1 else 1)
        assert f.info.sample_rate == sr

        mode = {
            mutagen.mp3.BitrateMode.CBR: 'CBR',
            mutagen.mp3.BitrateMode.VBR: 'VBR',
            mutagen.mp3.BitrateMode.ABR: 'ABR',
            mutagen.mp3.BitrateMode.UNKNOWN: 'UNKNOWN',
        }

        #stock les metadonnees
        metadata['bit_rate'] = f.info.bitrate
        metadata['mode'] = mode[f.info.bitrate_mode]
        metadata['channels'] = f.info.channels
        metadata['sample_rate'] = f.info.sample_rate
        metadata['samples'] = x.shape[-1]

    except Exception as e:
        #stock des valeurs null pour les audios non analyses
        print('{}: {}'.format(tid, repr(e)))
        metadata['bit_rate'] = 0
        metadata['mode'] = 'ERROR'
        metadata['channels'] = 0
        metadata['sample_rate'] = 0
        metadata['samples'] = 0

    return metadata
Beispiel #10
0
    def create_episode(self):
        """Produce the episode described by the form fields"""
        af_data = self.audio_file.data.read()
        af = mutagen.File(io.BytesIO(af_data))

        try:
            af_format = AudioFormat[af.__class__.__name__]
        except KeyError:
            msg = "Invalid audio format: use one of %s" % (", ".join(
                [x.value for x in AudioFormat]))
            return msg, 400

        af_name = AudioFile.standardized_name(self.published.data,
                                              self.title.data, af_format)

        af_path = os.path.join(self.media_dir, af_name)

        with open(af_path, "wb") as f:
            f.write(af_data)

        published = self.time_zone.localize(self.published.data).astimezone(
            pytz.utc)

        episode = Episode(title=self.title.data,
                          published=published,
                          description=self.description.data,
                          explicit=self.explicit.data)

        audio_file = AudioFile(file_name=af_name,
                               audio_format=af_format,
                               length=os.path.getsize(af_path),
                               duration=round(af.info.length))

        episode.audio_file = audio_file

        if self.keywords.data is not None:
            words = self.keywords.data.split(",")
            episode.set_keywords(words)

        return episode
Beispiel #11
0
def writeMeta(filename, meta, verbose=True):
    '''Write ID3 metadata to audio file

    <filename>: str, abspath to audio file.
    <meta>: dict, metadata dict.

    Write metadata into the .mp3 audio file using ID3

    Update time: 2016-07-12 14:09:27.
    '''

    from mutagen.mp3 import MP3
    from mutagen.mp4 import MP4, MP4Cover
    from mutagen.id3 import ID3, APIC, error

    audio = mutagen.File(filename)

    #------------Add ID3 tag if not exists------------
    try:
        audio.add_tags()
    except:
        pass

    for kk, vv in meta.items():
        if kk == 'cover':
            #------------------For mp3 format------------------
            #ap=APIC(encoding=3, mime=tools.deu(vv),type=3,\
            #desc=u'Cover',data=open(vv,'rb').read())
            #audio.tags.add(ap)

            #------------------For mp4 format------------------
            ap2=MP4Cover(open(vv,'rb').read(),\
                imageformat=MP4Cover.FORMAT_JPEG)
            audio['covr'] = [ap2]
        else:
            audio[kk] = tools.deu(vv)

    audio.save()

    return
Beispiel #12
0
    def write_track_id3(self, track_fp, album_artwork: bytes):
        try:
            audio = mutagen.File(track_fp, filename="x.mp3")
            audio.add_tags()

            # SET TITLE
            frame = mutagen.id3.TIT2(encoding=3)
            frame.append(self.title)
            audio.tags.add(frame)
            # SET ARTIST
            frame = mutagen.id3.TPE1(encoding=3)
            frame.append(self.artist)
            audio.tags.add(frame)

            # SET ALBUM
            if self.album:
                frame = mutagen.id3.TALB(encoding=3)
                frame.append(self.album)
                audio.tags.add(frame)
        # SET TRACK NO
            if self.track_no:
                frame = mutagen.id3.TRCK(encoding=3)
                frame.append(str(self.track_no))
                audio.tags.add(frame)
        # SET ARTWORK
            audio.tags.add(
                mutagen.id3.APIC(encoding=3,
                                 mime='image/jpeg',
                                 type=3,
                                 desc=u'Cover',
                                 data=album_artwork))
            audio.save(track_fp, v1=2)
            self.ready = True
            track_fp.seek(0)
            return track_fp
        except (TypeError, ValueError) as e:
            util.eprint(
                'File object passed to "write_track_metadata" must be opened in read/write binary ("wb+") mode'
            )
            raise e
Beispiel #13
0
    def callback(self, info):
        # Downloading, Finished, Converting, Converted

        if info["status"] in ("Downloading", "Finished", "Converting"):
            remaining = info["length"] - info["progress"]
            eta = f"{remaining / info['speed']:.2f}"

            if info["speed"] < 2e3:
                speed = f"{info['speed']:.0f}bps"
            elif info["speed"] < 2e6:
                speed = f"{info['speed']/1e3:.0f}Kbps"
            elif info["speed"] < 2e9:
                speed = f"{info['speed']/1e6:.0f}Mbps"
            elif info["speed"] < 2e12:
                speed = f"{info['speed']/1e9:.0f}Gbps"

            if info["status"] == "Converting":
                values = info["title"], "Converting", eta, speed
            else:
                values = info["title"], f"{info['progress']*100/info['length']:.2f}%", eta, speed

            if info["status"] == "Finished":
                ext = info[f"best_{info['filetype'].lower()}"]['ext']
                muta = mutagen.File(f"Downloads/{info['title']}.{ext}")
                muta["©ART"] = info["uploader"]
                muta.save()

                if info["filetype"] == "Audio":
                    values = info["title"], "Converting", eta, speed
                    self.cv_thread.add(info)

            self.tv.item(info["id"], values=values)
        elif info["status"] == "Converted":
            self.tv.delete(info["id"])
        else:
            self.preview_frame.thumbnail = info["thumbnail"]
            self.preview_frame.title = info["title"]
            self.preview_frame.uploader = info["uploader"]

            self.state["current_info"] = info
Beispiel #14
0
    def encode_tracks(self):
        """
        Using mutagen this encodes the .MP3
        files so that iTunes and other
        players can know what Artist,
        album, etc are.

        """
        print("Encoding...")
        i = 0
        for track in self.__album_data['track_titles']:
            file_name = self.__file_path + track + ".mp3"
            try:
                file_to_encode = EasyID3(file_name)
            except mutagen.id3.ID3NoHeaderError:
                print("\nFile didnt have ID3 tag, tagging now\n")
                file_to_encode = mutagen.File(file_name, easy=True)
                file_to_encode.add_tags()
            # add the mp3 tags to the audio files
            file_to_encode['tracknumber'] = str(i + 1)
            file_to_encode['title'] = track
            file_to_encode['artist'] = self.__album_data['artist']
            file_to_encode['album'] = self.__album_data['title']
            file_to_encode['date'] = self.__album_data['release_date'][2]
            file_to_encode.save()
            file_to_encode = MP3(file_name)
            print(self.__album_data['cover'])
            cover_data = open(self.__album_data['cover'], 'rb').read()
            file_to_encode.tags.add(
                APIC(
                    encoding=3,
                    mime='image/jpeg',
                    type=3,
                    desc=u'Cover',
                    data=cover_data
                )
            )
            file_to_encode.save()
            i += 1
        print("\nEncoding finished!\n")
Beispiel #15
0
    def __init__(self, fileName):
        super(LoadFile, self).__init__()
        self.fileName = fileName
        t, self.extension = os.path.splitext(fileName)

        self.__f = mutagen.File(fileName)
        if self.__f is None:
            #			print "Unknown file type: ", fileName
            return

        if 'audio/x-flac' in self.__f.mime:
            ### FLAC file
            self.extension = '.flac'
            self.__flac()
        elif 'audio/mp3' in self.__f.mime:
            ### MP3
            self.extension = '.mp3'
            self.__mp3()
        elif 'audio/vorbis' in self.__f.mime:
            ### Ogg
            self.extension = '.ogg'
            self.__ogg()
        elif 'audio/x-mpc' in self.__f.mime:
            ### Musepack
            print 'MPC filem', fileName
            self.extension = '.mpc'
#			self.__mpc()
        elif 'audio/mp4' in self.__f.mime:
            ### MP4
            self.extension = '.mp4'
            print "MP4", fileName
#			print f
#			return False
        elif 'audio/ape' in self.__f.mime:
            ### APE
            print "APE", fileName
#			print f
#			return False
        else:
            print "Unknown file ", fileName, " type: ", self.__f.mime
Beispiel #16
0
    def _getTagsForFLACFile(self):
        '''
        Returns an AudioFileTags object for the tag values for the FLAC audio file
        '''

        mutagenInterface = mutagen.File(self.audioFilepath)

        title = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'title')
        artist = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'artist')
        album = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'album')
        albumArtist = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'albumartist')
        genre = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'genre')
        dateAllPlays = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'date_all_plays')
        dateLastPlayed = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'date_last_played')
        playCount = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'play_count')
        votes = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'votes')
        rating = self._getTagValueFromMutagenInterfaceFLAC(
            mutagenInterface, 'rating')

        audioFileTags = AudioFileTags(title=title,
                                      artist=artist,
                                      album=album,
                                      albumArtist=albumArtist,
                                      genre=genre,
                                      dateAllPlays=dateAllPlays,
                                      dateLastPlayed=dateLastPlayed,
                                      playCount=playCount,
                                      votes=votes,
                                      rating=rating)

        return audioFileTags
Beispiel #17
0
 def add_metadata(self, file, sound):
     # "sound" requires a "freesound_client.get_sound" object
     # http://wiki.hydrogenaud.io/index.php?title=APE_key
     try:
         # Write it
         audio = FLAC(file)
         audio["title"] = sound.name
         audio["Artist"] = sound.username
         audio["Comment"] = sound.description
         audio["Publisher"] = "freesound.org"
         audio["File"] = sound.url
         # Save it
         audio.pprint()
         audio.save()
         # Read it
         file_info = mutagen.File(file)
         log.debug("Result metadata update:")
         log.debug(file_info)
         return file_info
     except Exception as e:
         log.debug(e)
         return False
Beispiel #18
0
def _get_tags(file_):
    tags = {}
    tag_assignments = {
        "tt": ["title"],
        "ta": ["artist"],
        "tl": ["album"],
        "ty": ["date", "year"],
        "tn": ["tracknumber"],
        "tc": ["comment"],
        "tg": ["genre"],
        "label": ["label"],
    }

    track = mutagen.File(file_)
    for key, tag_keys in tag_assignments.items():
        for tag_key in tag_keys:
            try:
                tags[key] = shlex.quote(track.tags[tag_key][0])
            except (KeyError, IndexError):
                if key not in tags or tags[key] != "''":
                    tags[key] = "''"
    return tags
Beispiel #19
0
    def add_tags(self, filepath: str, track: dict):
        try:
            tags = EasyID3(filepath)
        except ID3NoHeaderError:
            tags = mutagen.File(filepath, easy=True)
            tags.add_tags()

        tags["tracknumber"] = str(
            track["trackNumber"]).encode("utf-8").decode("utf-8")
        tags["title"] = track["title"]
        tags["artist"] = track["artist"]
        tags["album"] = track["album"]
        tags["discnumber"] = str(
            track["discNumber"]).encode("utf-8").decode("utf-8")
        tags["genre"] = track["genre"]
        tags["composer"] = track["composer"]
        tags["albumartist"] = track["albumArtist"]
        if "beatsPerMinute" in track and not track["beatsPerMinute"] == 0:
            tags["bpm"] = str(
                track["beatsPerMinute"]).encode("utf-8").decode("utf-8")
        # TODO store Year. will have to use standard ID3 instead of easy
        tags.save(v2_version=3)
Beispiel #20
0
    def _enrich(self, path):
        # path is something like s3://{bucket}/{folder}/{task-id}.{extension}
        l = len("s3://") + len(self.bucket) + 1
        path = path[l:]

        public_path = path[len(self.s3_public_prefix):]
        url = "https://{}/".format(self.domain_name) + public_path

        import mutagen
        from io import BytesIO
        o = self.storage.read(path)
        b = o.get('Body').read()
        audio = mutagen.File(BytesIO(b))
        
        duration = int(audio.info.length)
        size = len(b)

        return {
            "duration": duration,
            "bytes": size,
            "link": url,
        }
Beispiel #21
0
def has_loudness_tag(filepath):
  """ Return a pair of booleans indicating if filepath has a RG or R128 track/album tag, or None if file is invalid. """
  track, album = False, False

  try:
    mf = mutagen.File(filepath)
  except mutagen.MutagenError as e:
    logger().warning("File '%s' %s: %s" % (filepath,
                                           e.__class__.__qualname__,
                                           e))
    return
  if mf is None:
    return

  if (isinstance(mf.tags, mutagen.id3.ID3) or
          isinstance(mf, mutagen.id3.ID3FileType)):
    track = ("TXXX:REPLAYGAIN_TRACK_GAIN" in mf) and ("TXXX:REPLAYGAIN_TRACK_PEAK" in mf)
    album = ("TXXX:REPLAYGAIN_ALBUM_GAIN" in mf) and ("TXXX:REPLAYGAIN_ALBUM_PEAK" in mf)

  elif isinstance(mf, mutagen.oggopus.OggOpus):
    track = "R128_TRACK_GAIN" in mf
    album = "R128_ALBUM_GAIN" in mf

  elif (isinstance(mf.tags, (mutagen._vorbis.VComment, mutagen.apev2.APEv2)) or
        isinstance(mf, (mutagen.ogg.OggFileType, mutagen.apev2.APEv2File))):
    track = ("REPLAYGAIN_TRACK_GAIN" in mf) and ("REPLAYGAIN_TRACK_PEAK" in mf)
    album = ("REPLAYGAIN_ALBUM_GAIN" in mf) and ("REPLAYGAIN_ALBUM_PEAK" in mf)

  elif (isinstance(mf.tags, mutagen.mp4.MP4Tags) or
        isinstance(mf, mutagen.mp4.MP4)):
    track = ("----:COM.APPLE.ITUNES:REPLAYGAIN_TRACK_GAIN" in mf) and ("----:COM.APPLE.ITUNES:REPLAYGAIN_TRACK_PEAK" in mf)
    album = ("----:COM.APPLE.ITUNES:REPLAYGAIN_ALBUM_GAIN" in mf) and ("----:COM.APPLE.ITUNES:REPLAYGAIN_ALBUM_PEAK" in mf)

  else:
    logger().warning("Unhandled '%s' tag format for file '%s'" % (mf.__class__.__name__,
                                                                  filepath))
    return

  return track, album