Beispiel #1
0
def calc_accuracy(path1, path2):
    dur, fig = acoustid.fingerprint_file(path1)
    fp1 = chromaprint.decode_fingerprint(fig)[0]

    dur, fig2 = acoustid.fingerprint_file(path2)
    fp2 = chromaprint.decode_fingerprint(fig2)[0]

    return accuracy(fp1, fp2)
Beispiel #2
0
def main(argv):
    def exit_with_usage():
        print 'usage: $ python',__file__,'/path/to/foo.mp3'
    if len(argv) != 2:
        return exit_with_usage()
    else:
        path = argv[1]
    dur, fp_raw = acoustid.fingerprint_file(path,return_raw=True)
    dur, fp_str = acoustid.fingerprint_file(path)
    print 'duration:',dur
    print 'fingerprint str:',fp_str
    print 'fingerprint raw:',fp_raw
def match_audio(song1, song2):
    duration1, fp_encoded1 = acoustid.fingerprint_file(song1)
    duration2, fp_encoded2 = acoustid.fingerprint_file(song2)
    fingerprint1, version1 = chromaprint.decode_fingerprint(fp_encoded1)
    fingerprint2, version2 = chromaprint.decode_fingerprint(fp_encoded2)

    similarity = fuzz.ratio(fingerprint1, fingerprint2)
    print(similarity)

    if similarity >= 80:
        return similarity

    return 0
Beispiel #4
0
def fingerprint_item(item, lib=None, write=False):
    """Get the fingerprint for an Item. If the item already has a
    fingerprint, it is not regenerated. If fingerprint generation fails,
    return None. If `lib` is provided, then new fingerprints are saved
    to the database. If `write` is set, then the new fingerprints are
    also written to files' metadata.
    """
    # Get a fingerprint and length for this track.
    if not item.length:
        log.info(u'{0}: no duration available'.format(
            util.displayable_path(item.path)))
    elif item.acoustid_fingerprint:
        if write:
            log.info(u'{0}: fingerprint exists, skipping'.format(
                util.displayable_path(item.path)))
        else:
            log.info(u'{0}: using existing fingerprint'.format(
                util.displayable_path(item.path)))
            return item.acoustid_fingerprint
    else:
        log.info(u'{0}: fingerprinting'.format(util.displayable_path(
            item.path)))
        try:
            _, fp = acoustid.fingerprint_file(item.path)
            item.acoustid_fingerprint = fp
            if write:
                log.info(u'{0}: writing fingerprint'.format(
                    util.displayable_path(item.path)))
                item.write()
            if lib:
                lib.store(item)
            return item.acoustid_fingerprint
        except acoustid.FingerprintGenerationError as exc:
            log.info('fingerprint generation failed: {0}'.format(exc))
Beispiel #5
0
    def getNamesLastFM(self, path):

        # Use acoustid API to get song data if ID3 tags are not available.
        fing = fingerprint_file(path, force_fpcalc=True)
        fing = fing[1]
        fing = str(fing)
        fing = fing[2:-1]
        url = 'https://api.acoustid.org/v2/lookup?client='
        url += akey
        url += '&meta=recordings+releasegroups+compress&duration='
        url += str(self.d)
        url += '&fingerprint='
        url += fing
        try:
            text = urllib.request.urlopen(url)
            parsed = json.loads(text.read())
            names = list(acoustid.parse_lookup_result(parsed))
            for x in names:
                if None not in x:
                    names = x
                    title = names[-2]
                    artist = names[-1]
                    # Check if any feat. artists names in original name.
                    if ';' in artist:
                        artist = artist.split(';')
                        artist = artist[0]
                    if ',' in artist:
                        artist = artist.split(',')
                        artist = artist[0]
                    break
            self.artist_name1 = artist
            self.song_name1 = title
        except:
            print("No name data during search...")
def match(string):
	API_KEY = 'HMU8Btr6'
	#filename1 = 'song.mp3'
	dur,fingp = acoustid.fingerprint_file(string)
	commentText = ""
	try:
        #results = acoustid.match(API_KEY, filename)
		results1 = acoustid.lookup(API_KEY, fingp, dur)
		results = acoustid.parse_lookup_result(results1)
	except acoustid.NoBackendError:
		print("chromaprint library/tool not found", file=sys.stderr)
		sys.exit(1)
	except acoustid.FingerprintGenerationError:
		print("fingerprint could not be calculated", file=sys.stderr)
		sys.exit(1)
	except acoustid.WebServiceError as exc:
		print("web service request failed:", exc.message, file=sys.stderr)
		sys.exit(1)

	first = True
	for score, rid, title, artist in results:
		if first:
			first = False
		else:
			commentText += "\n"
		commentText += '%s - %s \n' % (artist, title)

		#commentText += 'http://musicbrainz.org/recording/%s \n'% rid

		#commentText += 'Score: %i%% \n'% (int(score * 100))
	return(commentText)
Beispiel #7
0
    def fingerprint(self):
        """Returns the Acoustid fingerprint"""
        if self._fingerprint:
            return self._fingerprint

        _, self._fingerprint = acoustid.fingerprint_file(self.path)
        return self._fingerprint
    def from_file(file_name: str):
        '''
        Construct a Track object from a file.
        '''

        path = os.path.join(constant.FILE_PREFIX, file_name)
        extension = filetype.guess_extension(path)

        duration, fingerprint = aid.fingerprint_file(path)
        file_hash = hash_file(path)

        # Use AcoustID
        json_resp = aid.lookup(constant.API_KEY, fingerprint, duration)
        matches = aid.parse_lookup_result(json_resp)

        try:
            # Ignore score and recording_id
            _, _, title, artist = next(matches)
        except StopIteration:
            title = 'Track ' + file_hash[:constant.HASH_LEN]
            artist = 'Unknown'

        return Track(title,
                     artist,
                     duration,
                     file_hash,
                     fingerprint,
                     extension,
                     path=file_name,
                     local=True)
def match(string):
    API_KEY = 'HMU8Btr6'
    #filename1 = 'song.mp3'
    dur, fingp = acoustid.fingerprint_file(string)
    commentText = ""
    try:
        #results = acoustid.match(API_KEY, filename)
        results1 = acoustid.lookup(API_KEY, fingp, dur)
        results = acoustid.parse_lookup_result(results1)
    except acoustid.NoBackendError:
        print("chromaprint library/tool not found", file=sys.stderr)
        sys.exit(1)
    except acoustid.FingerprintGenerationError:
        print("fingerprint could not be calculated", file=sys.stderr)
        sys.exit(1)
    except acoustid.WebServiceError as exc:
        print("web service request failed:", exc.message, file=sys.stderr)
        sys.exit(1)

    first = True
    for score, rid, title, artist in results:
        if first:
            first = False
        else:
            commentText += "\n"
        commentText += '%s - %s \n' % (artist, title)

        #commentText += 'http://musicbrainz.org/recording/%s \n'% rid

        #commentText += 'Score: %i%% \n'% (int(score * 100))
    return (commentText)
Beispiel #10
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-length', metavar='SECS', type=int, default=120,
                        help='length of the audio data used for fingerprint '
                             'calculation (default 120)')
    parser.add_argument('-raw', action='store_true',
                        help='output the raw uncompressed fingerprint')
    parser.add_argument('paths', metavar='FILE', nargs='+',
                        help='audio file to be fingerprinted')

    args = parser.parse_args()
    del sys.argv[1:] # to make gst not try to parse the args

    first = True
    for i, path in enumerate(args.paths):
        try:
            duration, fp = acoustid.fingerprint_file(path, args.length)
        except Exception:
            print >>sys.stderr, "ERROR: unable to calculate fingerprint " \
                                "for file %s, skipping" % path
            continue
        if args.raw:
            raw_fp = chromaprint.decode_fingerprint(fp)[0]
            fp = ','.join(map(str, raw_fp))
        if not first:
            print
        first = False
        print 'FILE=%s' % path
        print 'DURATION=%d' % duration
        print 'FINGERPRINT=%s' % fp
Beispiel #11
0
def fingerprint_item(log, item, write=False):
    """Get the fingerprint for an Item. If the item already has a
    fingerprint, it is not regenerated. If fingerprint generation fails,
    return None. If the items are associated with a library, they are
    saved to the database. If `write` is set, then the new fingerprints
    are also written to files' metadata.
    """
    # Get a fingerprint and length for this track.
    if not item.length:
        log.info(u'{0}: no duration available',
                 util.displayable_path(item.path))
    elif item.acoustid_fingerprint:
        if write:
            log.info(u'{0}: fingerprint exists, skipping',
                     util.displayable_path(item.path))
        else:
            log.info(u'{0}: using existing fingerprint',
                     util.displayable_path(item.path))
            return item.acoustid_fingerprint
    else:
        log.info(u'{0}: fingerprinting',
                 util.displayable_path(item.path))
        try:
            _, fp = acoustid.fingerprint_file(item.path)
            item.acoustid_fingerprint = fp
            if write:
                log.info(u'{0}: writing fingerprint',
                         util.displayable_path(item.path))
                item.try_write()
            if item._db:
                item.store()
            return item.acoustid_fingerprint
        except acoustid.FingerprintGenerationError as exc:
            log.info(u'fingerprint generation failed: {0}', exc)
Beispiel #12
0
def fingerprint_file(filename):
    # fp is tuple with (duration, fingerprint)
    fp = ai.fingerprint_file(filename)

    # get fingerprint for modification
    fp_bytes = fp[1]

    # pad fingerprint
    padlen = (len(fp_bytes) % 4)
    if padlen < 1: padlen = 0
    padstr = b'=' * padlen
    print(('padstr {0} {1} {2}'.format(len(fp_bytes), padlen, padstr)))
    fp_bytes += padstr

    # decode to list of char
    fp_int = base64.b64decode(fp_bytes)

    # decode to list of int32s
    fb_bin = [list('{:032b}'.format(abs(x))) for x in fp_int]

    # allocate array
    arr = np.zeros([len(fb_bin), len(fb_bin[0])])

    # copy to array
    for i in range(arr.shape[0]):
        arr[i, 0] = int(fp_int[i] > 0)  # The sign is added to the first bit
        for j in range(1, arr.shape[1]):
            arr[i, j] = float(fb_bin[i][j])

    return arr
Beispiel #13
0
def fingerprint_item(log, item, write=False):
    """Get the fingerprint for an Item. If the item already has a
    fingerprint, it is not regenerated. If fingerprint generation fails,
    return None. If the items are associated with a library, they are
    saved to the database. If `write` is set, then the new fingerprints
    are also written to files' metadata.
    """
    # Get a fingerprint and length for this track.
    if not item.length:
        log.info(u'{0}: no duration available',
                 util.displayable_path(item.path))
    elif item.acoustid_fingerprint:
        if write:
            log.info(u'{0}: fingerprint exists, skipping',
                     util.displayable_path(item.path))
        else:
            log.info(u'{0}: using existing fingerprint',
                     util.displayable_path(item.path))
            return item.acoustid_fingerprint
    else:
        log.info(u'{0}: fingerprinting',
                 util.displayable_path(item.path))
        try:
            _, fp = acoustid.fingerprint_file(item.path)
            item.acoustid_fingerprint = fp
            if write:
                log.info(u'{0}: writing fingerprint',
                         util.displayable_path(item.path))
                item.try_write()
            if item._db:
                item.store()
            return item.acoustid_fingerprint
        except acoustid.FingerprintGenerationError as exc:
            log.info(u'fingerprint generation failed: {0}', exc)
Beispiel #14
0
    def analyze(self, anURI):
        fileid = FileId(filename=os.path.abspath(anURI))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        asset_descr = AssetDescription(asset=AssetId(
            subname=os.path.basename(anURI),
            mimetype=mimetypes.guess_type(anURI, False)[0],
            file=fileid))

        try:
            duration, fingerprint = fingerprint_file(anURI)
            fingerprint_uuid = uuid.uuid5(uuid.NAMESPACE_DNS,
                                          str(duration) + str(fingerprint))

        except Exception as e:
            print(('E: AcoustID analyzer failed %s with error %s'
                   % (anURI, e)))
            return False
        meta = {
            'duration': str(duration) + 's',
            'fingerprint': fingerprint,
            'fingerprint_uuid': fingerprint_uuid
        }
        asset_descr.metadata = metadata.MetaDataAcoustID.extract(meta)
        file_descr.assets.append(asset_descr)

        return file_descr
Beispiel #15
0
def generate_fingerprints(this_file_list):
    """generates acoustID fingerprints and set flag 2

    """
    import acoustid
    for file_entry in this_file_list:
        new_file_list[file_entry] = acoustid.fingerprint_file(file_entry)
    return new_file_list
Beispiel #16
0
def handler(fileName):
	global speech, listen

	translator = gapi.Translator(speech.lang, 'en-uk')
	cfileName = None
	try:
		engine = pyttsx.init()
		cfileName = psw.convert(fileName)
		print "processing the file: ", fileName
		
		flag = True
		if not listen:
			finger_prints = []
			finger_print_char = None
			for files in os.listdir(os.getcwd() + '/audio'):
				finger_print = acoustid.fingerprint_file('audio/' + files)
				finger_prints.append(finger_print[0])
				finger_print_char = finger_print[1]
				print files, finger_print

			current_fingerprint = acoustid.fingerprint_file(fileName)
			print "now recorded", current_fingerprint
			if current_fingerprint[0] > max(finger_prints) or current_fingerprint[0] < min(finger_prints) or current_fingerprint[1] != finger_print_char:
				flag = False

		if flag:
			phrase = speech.getText(cfileName)
			if phrase!=None:
				phrase = phrase.lower()
				if len(phrase.strip())>0:
					print 'text:',phrase
					# psw.play(speech.getAudio(phrase))
					if listen:
						listen = False
						if "weather" in phrase.lower():
							engine.say(get_weather())
							engine.runAndWait()
					if "ok jarvis" in phrase.lower() or "okay jarvis" in phrase.lower():
						if engine.isBusy():
							engine.say("yes sir?")
							engine.runAndWait()
							listen = True
	except Exception, e:
		import traceback
		traceback.print_exc()
		print "Unexpected error:", sys.exc_info()[0], e
Beispiel #17
0
 def getAcoustidFingerprint(self):
     if self.fingerprint:
         return self.fingerprint
     if not self._path:
         return None
     print(f'Calculating fingerprint of {self._path}')
     fp = acoustid.fingerprint_file(self._path)
     return fp[1]
Beispiel #18
0
def _fingerprint(path, force_fpcalc=False):
    "Get the AcoustID fingerprint for an audio track"

    duration, fp = fingerprint_file(path, force_fpcalc=force_fpcalc)
    duration = timedelta(seconds=duration)
    fingerprint, version = decode_fingerprint(fp)
    assert version == settings.FINGERPRINT_VERSION, 'Version mismatch: %s' % version
    return duration, fingerprint
Beispiel #19
0
def acoustid_match(log, path):
    """Gets metadata for a file from Acoustid and populates the
    _matches, _fingerprints, and _acoustids dictionaries accordingly.
    """
    try:
        duration, fp = acoustid.fingerprint_file(util.syspath(path))
    except acoustid.FingerprintGenerationError as exc:
        log.error('fingerprinting of {0} failed: {1}',
                  util.displayable_path(repr(path)), exc)
        return None
    fp = fp.decode()
    _fingerprints[path] = fp
    try:
        res = acoustid.lookup(API_KEY,
                              fp,
                              duration,
                              meta='recordings releases')
    except acoustid.AcoustidError as exc:
        log.debug('fingerprint matching {0} failed: {1}',
                  util.displayable_path(repr(path)), exc)
        return None
    log.debug('chroma: fingerprinted {0}', util.displayable_path(repr(path)))

    # Ensure the response is usable and parse it.
    if res['status'] != 'ok' or not res.get('results'):
        log.debug('no match found')
        return None
    result = res['results'][0]  # Best match.
    if result['score'] < SCORE_THRESH:
        log.debug('no results above threshold')
        return None
    _acoustids[path] = result['id']

    # Get recording and releases from the result
    if not result.get('recordings'):
        log.debug('no recordings found')
        return None
    recording_ids = []
    releases = []
    for recording in result['recordings']:
        recording_ids.append(recording['id'])
        if 'releases' in recording:
            releases.extend(recording['releases'])

    # The releases list is essentially in random order from the Acoustid lookup
    # so we optionally sort it using the match.preferred configuration options.
    # 'original_year' to sort the earliest first and
    # 'countries' to then sort preferred countries first.
    country_patterns = config['match']['preferred']['countries'].as_str_seq()
    countries = [re.compile(pat, re.I) for pat in country_patterns]
    original_year = config['match']['preferred']['original_year']
    releases.sort(key=partial(
        releases_key, countries=countries, original_year=original_year))
    release_ids = [rel['id'] for rel in releases]

    log.debug('matched recordings {0} on releases {1}', recording_ids,
              release_ids)
    _matches[path] = recording_ids, release_ids
Beispiel #20
0
def acoustid_match(log, path):
    """Gets metadata for a file from Acoustid and populates the
    _matches, _fingerprints, and _acoustids dictionaries accordingly.
    """
    try:
        duration, fp = acoustid.fingerprint_file(util.syspath(path))
    except acoustid.FingerprintGenerationError as exc:
        log.error(u'fingerprinting of {0} failed: {1}',
                  util.displayable_path(repr(path)), exc)
        return None
    fp = fp.decode()
    _fingerprints[path] = fp
    try:
        res = acoustid.lookup(API_KEY, fp, duration,
                              meta='recordings releases')
    except acoustid.AcoustidError as exc:
        log.debug(u'fingerprint matching {0} failed: {1}',
                  util.displayable_path(repr(path)), exc)
        return None
    log.debug(u'chroma: fingerprinted {0}',
              util.displayable_path(repr(path)))

    # Ensure the response is usable and parse it.
    if res['status'] != 'ok' or not res.get('results'):
        log.debug(u'no match found')
        return None
    result = res['results'][0]  # Best match.
    if result['score'] < SCORE_THRESH:
        log.debug(u'no results above threshold')
        return None
    _acoustids[path] = result['id']

    # Get recording and releases from the result
    if not result.get('recordings'):
        log.debug(u'no recordings found')
        return None
    recording_ids = []
    releases = []
    for recording in result['recordings']:
        recording_ids.append(recording['id'])
        if 'releases' in recording:
            releases.extend(recording['releases'])

    # The releases list is essentially in random order from the Acoustid lookup
    # so we optionally sort it using the match.preferred configuration options.
    # 'original_year' to sort the earliest first and
    # 'countries' to then sort preferred countries first.
    country_patterns = config['match']['preferred']['countries'].as_str_seq()
    countries = [re.compile(pat, re.I) for pat in country_patterns]
    original_year = config['match']['preferred']['original_year']
    releases.sort(key=partial(releases_key,
                              countries=countries,
                              original_year=original_year))
    release_ids = [rel['id'] for rel in releases]

    log.debug(u'matched recordings {0} on releases {1}',
              recording_ids, release_ids)
    _matches[path] = recording_ids, release_ids
Beispiel #21
0
def submit_items(userkey, items, chunksize=64):
    """Submit fingerprints for the items to the Acoustid server.
    """
    data = []  # The running list of dictionaries to submit.

    def submit_chunk():
        """Submit the current accumulated fingerprint data."""
        log.info('submitting {0} fingerprints'.format(len(data)))
        acoustid.submit(API_KEY, userkey, data)
        del data[:]

    for item in items:
        # Get a fingerprint and length for this track.
        if not item.length:
            log.info(u'{0}: no duration available'.format(
                util.displayable_path(item.path)))
            continue
        elif item.acoustid_fingerprint:
            log.info(u'{0}: using existing fingerprint'.format(
                util.displayable_path(item.path)))
            fp = item.acoustid_fingerprint
        else:
            log.info(u'{0}: fingerprinting'.format(
                util.displayable_path(item.path)))
            try:
                _, fp = acoustid.fingerprint_file(item.path)
            except acoustid.FingerprintGenerationError as exc:
                log.info('fingerprint generation failed: {0}'.format(exc))
                continue

        # Construct a submission dictionary for this item.
        item_data = {
            'duration': int(item.length),
            'fingerprint': fp,
        }
        if item.mb_trackid:
            item_data['mbid'] = item.mb_trackid
            log.debug('submitting MBID')
        else:
            item_data.update({
                'track': item.title,
                'artist': item.artist,
                'album': item.album,
                'albumartist': item.albumartist,
                'year': item.year,
                'trackno': item.track,
                'discno': item.disc,
            })
            log.debug('submitting textual metadata')
        data.append(item_data)

        # If we have enough data, submit a chunk.
        if len(data) >= chunksize:
            submit_chunk()

    # Submit remaining data in a final chunk.
    if data:
        submit_chunk()
Beispiel #22
0
    def test_fingerprinting_methods(self):
        "Verify that fpcalc and acoustid produce identical fingerprints"

        duration, fp = fingerprint_file(self.AUDIO_TRACK1, force_fpcalc=True)
        fp, version = decode_fingerprint(fp)
        self.assertEqual(version, settings.FINGERPRINT_VERSION)

        self.assertEqual(int(self.fp1.duration.total_seconds()), duration)
        self.assertSequenceEqual(self.fp1.fingerprint, fp)
Beispiel #23
0
def submit_items(userkey, items, chunksize=64):
    """Submit fingerprints for the items to the Acoustid server.
    """
    data = []  # The running list of dictionaries to submit.
    def submit_chunk():
        """Submit the current accumulated fingerprint data."""
        log.info('submitting {0} fingerprints'.format(len(data)))
        acoustid.submit(API_KEY, userkey, data)
        del data[:]

    for item in items:
        # Get a fingerprint and length for this track.
        if not item.length:
            log.info(u'{0}: no duration available'.format(
                util.displayable_path(item.path)
            ))
            continue
        elif item.acoustid_fingerprint:
            log.info(u'{0}: using existing fingerprint'.format(
                util.displayable_path(item.path)
            ))
            fp = item.acoustid_fingerprint
        else:
            log.info(u'{0}: fingerprinting'.format(
                util.displayable_path(item.path)
            ))
            try:
                _, fp = acoustid.fingerprint_file(item.path)
            except acoustid.FingerprintGenerationError, exc:
                log.info('fingerprint generation failed')
                continue

        # Construct a submission dictionary for this item.
        item_data = {
            'duration': int(item.length),
            'fingerprint': fp,
        }
        if item.mb_trackid:
            item_data['mbid'] = item.mb_trackid
            log.debug('submitting MBID')
        else:
            item_data.update({
                'track': item.title,
                'artist': item.artist,
                'album': item.album,
                'albumartist': item.albumartist,
                'year': item.year,
                'trackno': item.track,
                'discno': item.disc,
            })
            log.debug('submitting textual metadata')
        data.append(item_data)

        # If we have enough data, submit a chunk.
        if len(data) >= chunksize:
            submit_chunk()
Beispiel #24
0
def acoustid_match(path):
    """Gets metadata for a file from Acoustid and populates the
    _matches, _fingerprints, and _acoustids dictionaries accordingly.
    """
    try:
        duration, fp = acoustid.fingerprint_file(path)
    except acoustid.FingerprintGenerationError, exc:
        log.error('fingerprinting of %s failed: %s' %
                  (repr(path), str(exc)))
        return None
Beispiel #25
0
def generate_fingerprint(fname: str) -> List[int]:
    """return an audio fingerprint for a video file.
    """
    audioclip = AudioFileClip(fname)
    audioclip.write_audiofile("temp.wav")

    duration, fp_encoded = acoustid.fingerprint_file("temp.wav")
    fingerprint, version = decode_fingerprint(fp_encoded)

    os.remove("temp.wav")
    return fingerprint
Beispiel #26
0
def retrieve(song_path):
    '''
    Takes a song file at <song_path>
    Returns a tuple (artist, title, date) obtained through AcoustID API
    '''
    # Forming the URL for the request on acoustid API
    url = 'https://api.acoustid.org/v2/lookup'
    duration, fingerprint = fingerprint_file(song_path)
    duration, fingerprint = int(float(duration)), fingerprint.decode()
    url += '?client={0}&meta={1}&duration={2}&fingerprint={3}'.format(
        APIKEY, 'recordings+releases', duration, fingerprint)
    answer = post(url)
    if answer.status_code != codes.ok:
        answer.raise_for_status()
    data = answer.json()
    titles, artists, dates = [], [], []
    # Filling theses 3 tabs
    for item in data['results']:
        if 'recordings' in item:
            for subitem in item['recordings']:
                if 'title' in subitem:
                    titles.append(subitem['title'])
                if 'artists' in subitem:
                    artists.append(subitem['artists'])
                if 'releases' in subitem:
                    for subsubitem in subitem['releases']:
                        if 'date' in subsubitem:
                            dates.append(subsubitem['date']['year'])
                        #if 'artists' in subitem:
                        #    artists.append(subsubitem['artists'])
    artists = [artist for arr in artists for artist in arr]
    artists = [artist['name'] for artist in artists]
    # Extracting the artist, title and date
    max_count = max([dates.count(date) for date in dates])
    date = None
    for dte in dates:
        if dates.count(dte) == max_count:
            if (date == None):
                date = dte
            elif (dte < date):
                date = dte
    max_count = max([artists.count(artist) for artist in artists])
    artist = None
    for artst in artists:
        if artists.count(artst) == max_count:
            artist = artst
            break
    max_count = max([titles.count(title) for title in titles])
    title = None
    for ttle in titles:
        if titles.count(ttle) == max_count:
            title = ttle
            break
    return artist, title, date
    def _acoustid_tag_file(self, filepath):
        """
        Gets metadata for a file from Acoustid.
        returns a Result object with Fingerprint, acoustid, recording_ids and release_ids

        Suppresses all exceptions - will return None if failed
        """
        path = Path(filepath)
        try:
            duration, fingerprint = acoustid.fingerprint_file(str(path))
        except acoustid.FingerprintGenerationError as exc:
            logger.error('fingerprinting of {0} failed: {1}', filepath, exc)
            return None

        try:
            res = acoustid.lookup(self.api_key,
                                  fingerprint,
                                  duration,
                                  meta='recordings releases')
        except acoustid.AcoustidError as exc:
            logger.debug('fingerprint matching {0} failed: {1}', filepath, exc)
            return None

        logger.debug('fingerprinted {0}', filepath)

        # Ensure the response is usable and parse it.
        if res['status'] != 'ok' or not res.get('results'):
            logger.debug('no match found')
            return None

        acoustid_result = res['results'][0]  # Best match.
        if acoustid_result['score'] < self.SCORE_THRESH:
            logger.debug('no results above threshold')
            return None

        # Get recording and releases from the result.
        if not acoustid_result.get('recordings'):
            logger.debug('no recordings found')
            return None

        recording_ids = []
        release_ids = []

        for recording in acoustid_result['recordings']:
            recording_ids.append(recording['id'])
            if 'releases' in recording:
                release_ids += [rel['id'] for rel in recording['releases']]

        logger.debug('matched recordings {0} on releases {1}', recording_ids,
                     release_ids)

        return AcoustIDTaggerResult(fingerprint, acoustid_result['id'],
                                    recording_ids, release_ids)
Beispiel #28
0
	def fingerprint_func(self):
		
		fingerprint = acoustid.fingerprint_file(self.path)
		self.line = fingerprint
		
		self.line = str(self.line)
		self.line_word = ""
		self.after_word = "')"
		self.before_word = ", '"
		self.num = 1
		self.lines_func()
		self.duration_func()
Beispiel #29
0
    def crossSim(path1, path2):
        i = 0
        maxSim = 0
        maxIndex = 0
        fPrints1 = []
        fPrints2 = []
        indexMaps = []

        for file in listdir(path1):
            fpEncode = acoustid.fingerprint_file(file)
            fPrints1.append(fpEncode)
        for file in listdir(path2):
            fpEncode = acoustid.fingerprint_file(file)
            fPrints2.append(fpEncode)
        for i in fPrints2:
            for n in fPrints1:
                similarity = fuzz.ratio(fPrints2[i], fPrints2[n])
                if similarity > maxSim:
                    maxIndex = n
            indexMaps.append(maxIndex)
        print(indexMaps)
Beispiel #30
0
def process_file( file_name, dict ) :
    """ process a file, given file_name and write the results to out_file."""
    (duration, fingerprint) = acoustid.fingerprint_file(file_name)

    try :
        dict[fingerprint].append(file_name)
    except KeyError :
        dict[fingerprint] = list()
        dict[fingerprint].append(file_name)

    global file_count
    file_count += 1
Beispiel #31
0
def run():
    # TODO: break this into smaller functions
    LOGGER.info('Running music downloader...')
    tracks_to_download = get_tracks_to_download()
    if not tracks_to_download:
        LOGGER.info('No queued tracks found in database')
        return
    LOGGER.info('Found {} tracks from database to download...'.format(len(tracks_to_download)))
    options = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'quiet': False}

    for queued_track in tracks_to_download:
        exists = session.query(SavedTrack).filter(SavedTrack.artist == queued_track.artist,
                                                  SavedTrack.title == queued_track.title).count() > 0
        if exists:
            LOGGER.info('Track already exists as Saved Track, deleting Queued track and skipping download.')
            session.delete(queued_track)
            session.commit()
            continue
        track_save_name = u'{} - {}'.format(queued_track.artist, queued_track.title)
        final_track_path = TRACK_DIRECTORY + track_save_name + '.mp3'
        holding_track_path = HOLD_DIRECTORY + track_save_name + '.mp3'
        LOGGER.info('Downloading track: {}'.format(track_save_name))
        options['outtmpl'] = u'{}/{}.%(ext)s'.format(HOLD_DIRECTORY, track_save_name)
        ydl = youtube_dl.YoutubeDL(options)
        download_link = build_download_link(queued_track.youtube_video_id)
        # download the track
        try:
            ydl.download([download_link])
        except youtube_dl.utils.DownloadError as e:
            LOGGER.warning('youtube-dl encountered an error: {}' .format(e.message))
            continue
        saved_track = SavedTrack()
        saved_track.update_from_dict(queued_track.as_dict())
        saved_track.path = final_track_path
        saved_track.md5 = calculate_md5(holding_track_path)
        fingerprint_duration = fingerprint_file(holding_track_path, 30)
        saved_track.fingerprint = fingerprint_duration[1]
        saved_track.duration = fingerprint_duration[0]

        session.merge(saved_track)
        session.delete(queued_track)
        session.commit()
        os.rename(holding_track_path, final_track_path)
        LOGGER.info('Complete. Downloaded track data committed to database.')
Beispiel #32
0
def mainFunc():
    print("")
    print("simpleMusicMatcher v0.1")

    # Do scan
    print("")
    print("Beginning library scan...")
    for root, dirs, files in os.walk(DATA_PATH, topdown=False):
        for fname in files:
            name, ext = os.path.splitext(fname)
            ext = ext.lower()

            print("")

            if ext == ".mp3":  # We only check for mp3s. Could work for anything supported by AcoustID.
                fpath = os.path.join(root, fname)
                #print(fpath)

                #Check if it already exists in the DB
                if exists_in_DB(fpath):
                    print("File already scanned. Skipping...")
                else:
                    print("Found " + fpath)
                    duration, chroma = acoustid.fingerprint_file(fpath)
                    writeEntry(fname, fpath, chroma)

    # Cross-reference all fingerprints
    print("")
    print("Beginnning fingerprint cross-reference...")

    all_items = query_db(
        'select id, fingerprint from audiofingerprints order by id asc')
    item_count = len(all_items)

    for i in range(item_count):
        for j in range(i + 1, item_count):
            #print(all_items[i])
            #print(all_items[i]['fingerprint'])
            if (all_items[i]['fingerprint'] == all_items[j]['fingerprint']):
                print("")
                print("MATCH!")

                tmpval1 = query_db(
                    'select full_path from audiofingerprints where id=?',
                    [all_items[i]['id']])[0]
                tmpval2 = query_db(
                    'select full_path from audiofingerprints where id=?',
                    [all_items[j]['id']])[0]

                print(tmpval1['full_path'])
                print(tmpval2['full_path'])
    def _acoustid_tag_file(self, filepath):
        """
        Gets metadata for a file from Acoustid.
        returns a Result object with Fingerprint, acoustid, recording_ids and release_ids

        Suppresses all exceptions - will return None if failed
        """
        path = Path(filepath)
        try:
            duration, fingerprint = acoustid.fingerprint_file(str(path))
        except acoustid.FingerprintGenerationError as exc:
            logger.error('fingerprinting of {0} failed: {1}', filepath, exc)
            return None

        try:
            res = acoustid.lookup(self.api_key, fingerprint, duration, meta='recordings releases')
        except acoustid.AcoustidError as exc:
            logger.debug('fingerprint matching {0} failed: {1}', filepath, exc)
            return None

        logger.debug('fingerprinted {0}', filepath)

        # Ensure the response is usable and parse it.
        if res['status'] != 'ok' or not res.get('results'):
            logger.debug('no match found')
            return None

        acoustid_result = res['results'][0]  # Best match.
        if acoustid_result['score'] < self.SCORE_THRESH:
            logger.debug('no results above threshold')
            return None

        # Get recording and releases from the result.
        if not acoustid_result.get('recordings'):
            logger.debug('no recordings found')
            return None

        recording_ids = []
        release_ids = []

        for recording in acoustid_result['recordings']:
            recording_ids.append(recording['id'])
            if 'releases' in recording:
                release_ids += [rel['id'] for rel in recording['releases']]

        logger.debug('matched recordings {0} on releases {1}', recording_ids, release_ids)

        return AcoustIDTaggerResult(fingerprint, acoustid_result['id'], recording_ids, release_ids)
Beispiel #34
0
def handler(fileName):
	global speech, listen

	cfileName = None
	try:
		engine = pyttsx.init()
		cfileName = psw.convert(fileName)
		print "processing the file: ", fileName
		
		flag = True
		if not listen:
			with open('jarvis_fingerprints_samples', 'r') as f:
				fingerprints_json = json.loads("".join(f.readlines())).get("data")
				finger_prints = fingerprints_json.get("vals")
				finger_print_char = fingerprints_json.get("char")

			current_fingerprint = acoustid.fingerprint_file(fileName)
			print "now recorded", current_fingerprint
			if current_fingerprint[0] > max(finger_prints) or current_fingerprint[0] < min(finger_prints) or current_fingerprint[1] != finger_print_char:
				flag = False

		if flag:
			phrase = speech.getText(cfileName)
			if phrase!=None:
				phrase = phrase.lower()
				if len(phrase.strip())>0:
					print 'text:', phrase
					if listen:
						listen = False
						# commands.execute(phrase, speech)
						if "weather" in phrase.lower():
							engine.say(get_weather())
							engine.runAndWait()
					if "jarvis" in phrase.lower():
						print "got in"
						print engine.isBusy()
						if engine.isBusy():
							engine.say("yes sir?")
							engine.runAndWait()
							listen = True
	except Exception, e:
		import traceback
		traceback.print_exc()
		listen = False
		print "Unexpected error:", sys.exc_info()[0], e
		engine.say("I didn't catch that, sir.")
		engine.runAndWait()
Beispiel #35
0
def acoustid_match(log, path):
    """Gets metadata for a file from Acoustid and populates the
    _matches, _fingerprints, and _acoustids dictionaries accordingly.
    """
    try:
        duration, fp = acoustid.fingerprint_file(util.syspath(path))
    except acoustid.FingerprintGenerationError as exc:
        log.error(u'fingerprinting of {0} failed: {1}',
                  util.displayable_path(repr(path)), exc)
        return None
    _fingerprints[path] = fp
    try:
        res = acoustid.lookup(API_KEY,
                              fp,
                              duration,
                              meta='recordings releases')
    except acoustid.AcoustidError as exc:
        log.debug(u'fingerprint matching {0} failed: {1}',
                  util.displayable_path(repr(path)), exc)
        return None
    log.debug(u'chroma: fingerprinted {0}', util.displayable_path(repr(path)))

    # Ensure the response is usable and parse it.
    if res['status'] != 'ok' or not res.get('results'):
        log.debug(u'no match found')
        return None
    result = res['results'][0]  # Best match.
    if result['score'] < SCORE_THRESH:
        log.debug(u'no results above threshold')
        return None
    _acoustids[path] = result['id']

    # Get recording and releases from the result.
    if not result.get('recordings'):
        log.debug(u'no recordings found')
        return None
    recording_ids = []
    release_ids = []
    for recording in result['recordings']:
        recording_ids.append(recording['id'])
        if 'releases' in recording:
            release_ids += [rel['id'] for rel in recording['releases']]

    log.debug(u'matched recordings {0} on releases {1}', recording_ids,
              release_ids)
    _matches[path] = recording_ids, release_ids
Beispiel #36
0
def acoustid_match(log, path):
    """Gets metadata for a file from Acoustid and populates the
    _matches, _fingerprints, and _acoustids dictionaries accordingly.
    """
    try:
        duration, fp = acoustid.fingerprint_file(util.syspath(path))
    except acoustid.FingerprintGenerationError as exc:
        log.error(u'fingerprinting of {0} failed: {1}',
                  util.displayable_path(repr(path)), exc)
        return None
    _fingerprints[path] = fp
    try:
        res = acoustid.lookup(API_KEY, fp, duration,
                              meta='recordings releases')
    except acoustid.AcoustidError as exc:
        log.debug(u'fingerprint matching {0} failed: {1}',
                  util.displayable_path(repr(path)), exc)
        return None
    log.debug(u'chroma: fingerprinted {0}',
              util.displayable_path(repr(path)))

    # Ensure the response is usable and parse it.
    if res['status'] != 'ok' or not res.get('results'):
        log.debug(u'no match found')
        return None
    result = res['results'][0]  # Best match.
    if result['score'] < SCORE_THRESH:
        log.debug(u'no results above threshold')
        return None
    _acoustids[path] = result['id']

    # Get recording and releases from the result.
    if not result.get('recordings'):
        log.debug(u'no recordings found')
        return None
    recording_ids = []
    release_ids = []
    for recording in result['recordings']:
        recording_ids.append(recording['id'])
        if 'releases' in recording:
            release_ids += [rel['id'] for rel in recording['releases']]

    log.debug(u'matched recordings {0} on releases {1}',
              recording_ids, release_ids)
    _matches[path] = recording_ids, release_ids
Beispiel #37
0
    def geraFingerprint(localMp3):
        try:
            #calculador = md5()
            #with open(localMp3, "rb") as arqMp3:
            #    # for parte in iter(partial(arqMp3.read, 4096), b''):
            #    while True:
            #        parte = arqMp3.read(4096)
            #        if not parte: break
            #
            #        calculador.update(parte)
            #
            #return calculador.hexdigest()
            arqTempoFingerprint = fingerprint_file(localMp3)
            return arqTempoFingerprint

        except IOError:
            print("ERRO ao abrir arquivo:", localMp3)
            raise IOError
Beispiel #38
0
 def __addsongtodb(cls, filename):
     '''__addsongtodb'''
     logging.info("Adding song to DB " + filename)
     full_path_filename = cls.__inpath("normalized", filename)
     fingerprint = acoustid.fingerprint_file(full_path_filename)
     pickled = pickle.dumps(fingerprint)
     con = sqlite.connect(cls.__dbfile)
     cursor = con.cursor()
     try:
         cursor.execute(
             'INSERT INTO ' + 'Songs("Input File Name", AcoustID) ' +
             'VALUES(?, ?)'
             '', (
                 filename,
                 pickled,
             ))
         con.commit()
     except Exception:
         logging.info(filename + " already present")
Beispiel #39
0
def acoustid_match(path):
    """Gets metadata for a file from Acoustid and populates the
    _matches, _fingerprints, and _acoustids dictionaries accordingly.
    """
    try:
        duration, fp = acoustid.fingerprint_file(path)
    except acoustid.FingerprintGenerationError as exc:
        log.error('fingerprinting of %s failed: %s' %
                  (repr(path), str(exc)))
        return None
    _fingerprints[path] = fp
    try:
        res = acoustid.lookup(API_KEY, fp, duration,
                              meta='recordings releases')
    except acoustid.AcoustidError as exc:
        log.debug('fingerprint matching %s failed: %s' %
                  (repr(path), str(exc)))
        return None
    log.debug('chroma: fingerprinted %s' % repr(path))

    # Ensure the response is usable and parse it.
    if res['status'] != 'ok' or not res.get('results'):
        log.debug('chroma: no match found')
        return None
    result = res['results'][0]
    if result['score'] < SCORE_THRESH:
        log.debug('chroma: no results above threshold')
        return None
    _acoustids[path] = result['id']

    # Get recordings from the result.
    if not result.get('recordings'):
        log.debug('chroma: no recordings found')
        return None
    recording = result['recordings'][0]
    recording_id = recording['id']
    if 'releases' in recording:
        release_ids = [rel['id'] for rel in recording['releases']]
    else:
        release_ids = []

    log.debug('chroma: matched recording {0}'.format(recording_id))
    _matches[path] = recording_id, release_ids
Beispiel #40
0
def process_song(artist, artist_path, albums_path, song_filename):
    song_path = os.path.join(artist_path, albums_path, song_filename)
    song_title = song_filename[:song_filename.rfind('.')]
    song_format = song_filename[song_filename.rfind('.') + 1:]

    print("Processing " + artist + ' - ' + song_filename)
    has_cover_art_process = has_cover_art_async(song_path)
    duration, fingerprint = acoustid.fingerprint_file(song_path)

    ffprobe_out, err = has_cover_art_process.communicate()
    ffprobe_out = ffprobe_out.decode()
    has_cover_art = 'DISPOSITION:attached_pic=1' in ffprobe_out
    bitrate = find_bitrate_in_ffprobe_output(ffprobe_out)
    freq_cutoff = find_song_cutoff_frequency(song_path)

    results_queue.put([
        artist, albums_path, song_title, song_format,
        int(duration), bitrate, freq_cutoff, has_cover_art, fingerprint
    ])
Beispiel #41
0
	def id(self, track):
		try:
			dur, fp = acoustid.fingerprint_file(track.file)
			response = acoustid.parse_lookup_result(acoustid.lookup(API_KEY,fp,dur,'recordings'))
			response = response.next()
			releases = self.search((response[2],response[3],dur*1000))
			track.title = response[2]
			artist = self.library.addArtist(response[3])
			track.artist = artist
			albums = []
			for album in releases:
				x = artist.addAlbum(album)
				albums.append(x)
			track.album = albums
		except EOFError:
			track.title = 'ERROR'

		except StopIteration:
			track.title = 'ERROR'
Beispiel #42
0
def fingerprint_item(item, lib=None, write=False):
    """Get the fingerprint for an Item. If the item already has a
    fingerprint, it is not regenerated. If fingerprint generation fails,
    return None. If `lib` is provided, then new fingerprints are saved
    to the database. If `write` is set, then the new fingerprints are
    also written to files' metadata.
    """
    # Get a fingerprint and length for this track.
    if not item.length:
        log.info(u'{0}: no duration available'.format(
            util.displayable_path(item.path)
        ))
    elif item.acoustid_fingerprint:
        if write:
            log.info(u'{0}: fingerprint exists, skipping'.format(
                    util.displayable_path(item.path)
            ))
        else:
            log.info(u'{0}: using existing fingerprint'.format(
                util.displayable_path(item.path)
            ))
            return item.acoustid_fingerprint
    else:
        log.info(u'{0}: fingerprinting'.format(
            util.displayable_path(item.path)
        ))
        try:
            _, fp = acoustid.fingerprint_file(item.path)
            item.acoustid_fingerprint = fp
            if write:
                log.info(u'{0}: writing fingerprint'.format(
                    util.displayable_path(item.path)
                ))
                item.write()
            if lib:
                lib.store(item)
            return item.acoustid_fingerprint
        except acoustid.FingerprintGenerationError as exc:
            log.info(
                'fingerprint generation failed: {0}'.format(exc)
            )
Beispiel #43
0
 def recursive_walk(self, path):
     for directory in os.listdir(path):
         if os.path.isdir(path + '\\' + directory):
             self.populate_db(path + '\\' + directory)
     for file in os.listdir(path):
         if not os.path.isdir(path + '\\' + file) and file.endswith(self._extentions):
             duration, fingerprint = acoustid.fingerprint_file(path + '\\' + file)
             temp_data_dict = {
                 'fingerprint': fingerprint,
                 'path': path + '\\' + file,
                 'file_size': os.path.getsize(path + '\\' + file)
             }
             with db.atomic():
                 try:
                     Track.create(**temp_data_dict)
                 except IntegrityError:
                     duplicate_track = Track.select().where(Track.fingerprint==fingerprint).get()
                     if duplicate_track.file_size < os.path.getsize(path+'\\'+file):
                         duplicate_track.delete_instance()
                     else:
                         pass
    def fingerprint_func(self):
        """
		Fingerprint retrieval Method
		
		  - Strips the audio fingerprint out of the acoustid.fingerprint API
		  
		  - Executes the lines_func and duration_func methods
		
		"""

        fingerprint = acoustid.fingerprint_file(self.path)
        self.line = fingerprint

        self.line = str(self.line)
        self.line_word = ""
        self.after_word = "')"
        self.before_word = ", '"
        self.num = 1

        self.lines_func()
        self.duration_func()
	def fingerprint_func(self):
		"""
		Fingerprint retrieval Method
		
		  - Strips the audio fingerprint out of the acoustid.fingerprint API
		  
		  - Executes the lines_func and duration_func methods
		
		"""
		
		fingerprint = acoustid.fingerprint_file(self.path)
		self.line = fingerprint
		
		self.line = str(self.line)
		self.line_word = ""
		self.after_word = "')"
		self.before_word = ", '"
		self.num = 1
		
		self.lines_func()
		self.duration_func()
Beispiel #46
0
def acoustid_match(log, path):
    """Gets metadata for a file from Acoustid and populates the
    _matches, _fingerprints, and _acoustids dictionaries accordingly.
    """
    try:
        duration, fp = acoustid.fingerprint_file(util.syspath(path))
    except acoustid.FingerprintGenerationError as exc:
        log.error(u"fingerprinting of {0} failed: {1}", util.displayable_path(repr(path)), exc)
        return None
    _fingerprints[path] = fp
    try:
        res = acoustid.lookup(API_KEY, fp, duration, meta="recordings releases")
    except acoustid.AcoustidError as exc:
        log.debug(u"fingerprint matching {0} failed: {1}", util.displayable_path(repr(path)), exc)
        return None
    log.debug(u"chroma: fingerprinted {0}", util.displayable_path(repr(path)))

    # Ensure the response is usable and parse it.
    if res["status"] != "ok" or not res.get("results"):
        log.debug(u"no match found")
        return None
    result = res["results"][0]  # Best match.
    if result["score"] < SCORE_THRESH:
        log.debug(u"no results above threshold")
        return None
    _acoustids[path] = result["id"]

    # Get recording and releases from the result.
    if not result.get("recordings"):
        log.debug(u"no recordings found")
        return None
    recording_ids = []
    release_ids = []
    for recording in result["recordings"]:
        recording_ids.append(recording["id"])
        if "releases" in recording:
            release_ids += [rel["id"] for rel in recording["releases"]]

    log.debug(u"matched recordings {0} on releases {1}", recording_ids, release_ids)
    _matches[path] = recording_ids, release_ids
Beispiel #47
0
def returnMatch(id=None):
    file = mockDB[id]["title"]
    filename = "./pyacoustid-1.1.5/" + file
    duration, fingerprint = acoustid.fingerprint_file(filename)
    try:
        results = list(acoustid.match(API_KEY, filename))
    except acoustid.NoBackendError:
        print("chromaprint library/tool not found")
        sys.exit(1)
    except acoustid.FingerprintGenerationError:
        print("fingerprint could not be calculated")
        sys.exit(1)
    except acoustid.WebServiceError as exc:
        print("web service request failed:", exc.message)
        sys.exit(1)
    if len(results) > 0:
        resp = make_response(json.dumps(results), 200)
        resp.headers['mimetype'] = 'application/json'
        # check if there's a payment pointer in our db for this ID. If so then this is verified.
        print("match", results)
        return resp
    else:
        # Submit fingerprint to database.
        data = {
            'fingerprint': fingerprint,
            'duration': int(duration),
            'title': basename(filename)
        }
        print("DATA", data)
        # check if license is there. If so then return license, but note this is NOT verified since it is not in MusicBrainz database.
        try:
            result = acoustid.submit(API_KEY, USER_KEY, data)
        except acoustid.FingerprintGenerationError:
            print("fingerprint could not be calculated")
            sys.exit(1)
        except acoustid.WebServiceError as exc:
            print("web service request failed:", exc.message)
            sys.exit(1)
        return "success!"
Beispiel #48
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-length',
                        metavar='SECS',
                        type=int,
                        default=120,
                        help='length of the audio data used for fingerprint '
                        'calculation (default 120)')
    parser.add_argument('-raw',
                        action='store_true',
                        help='output the raw uncompressed fingerprint')
    parser.add_argument('paths',
                        metavar='FILE',
                        nargs='+',
                        help='audio file to be fingerprinted')

    args = parser.parse_args()
    # make gst not try to parse the args
    del sys.argv[1:]

    first = True
    for i, path in enumerate(args.paths):
        try:
            duration, fp = acoustid.fingerprint_file(path, args.length)
        except Exception:
            print("ERROR: unable to calculate fingerprint "
                  "for file %s, skipping" % path,
                  file=sys.stderr)
            continue
        if args.raw:
            raw_fp = chromaprint.decode_fingerprint(fp)[0]
            fp = ','.join(map(str, raw_fp))
        if not first:
            print
        first = False
        print('FILE=%s' % path)
        print('DURATION=%d' % duration)
        print('FINGERPRINT=%s' % fp.decode('utf8'))
Beispiel #49
0
			urlpath = path.replace("%", "%25")
			urlpath = urlpath.replace("#", "%23")
			urlpath = urlpath.replace("ABCDEFG (2010)", "ABCDEFG%20(2010)") # One particular album by Chumbawumba needs its space url encoded.  Unsure why - no other tracks in the DB have spaces encodes
			trackurl = mediaprefix + urlpath
			filemetadata = taglib.File(path)
			tags = {}
			for key, values in filemetadata.tags.items():
				if len(values) < 1:
					continue
				key = key.lower()
				value = " & ".join(values)
				if key not in ["title", "album", "artist", "year", "genre", "comment", "lyrics"]:
					continue
				tags[key] = value

			duration, fingerprint = acoustid.fingerprint_file(path, maxlength=60)
			if fingerprint.decode('UTF-8') in ["AQAAAA", "AQAAAQkz9UsCAQ"]:
				log("Skipping empty track " + trackurl, debug=True)
				continue
			if duration < 1:
				log("Track with duration less than 1 second " + trackurl, error=True)
				errorCount += 1
				continue
			trackdata = {
				"fingerprint": fingerprint.decode('UTF-8'),
				"duration": int(duration),
				"tags": tags,
			}
			log(trackurl + ", " + str(trackdata), debug=True)
			trackresult = requests.put(apiurl+"/tracks", params={"url": trackurl}, data=json.dumps(trackdata), allow_redirects=False, headers={"If-None-Match": "*"})
			if trackresult.ok:
Beispiel #50
0
            decode_command = ['flac', '--decode', '-f', filename, '-o', wavefile]
        elif filename.endswith('.ogg'):
            file_metadata = OggVorbis(filename)
            decode_command = ['oggdec', filename, '-o', wavefile]
        elif filename.endswith('.mp3'):
            file_metadata = EasyID3(filename)
            decode_command = ['lame', '--decode', filename, wavefile]
        else:
            print "Don't know to handle this file"
            continue

        print 'Looking up track on AcoustID/MusicBrainz'
        print '--------------------------------------------------------------------------------'
        # TODO: max 3 requests/sec to AcoustID/MusicBrainz, just sleeping a sec for the time being
        time.sleep(1)
        acoustid_duration, acoustid_fingerprint = acoustid.fingerprint_file(filename) # TODO: use fingerprint & duration to check for duplicates in database? seems like matching fingerprints is not trivial
        acoustid_result = acoustid.parse_lookup_result(acoustid.lookup(acoustid_key, acoustid_fingerprint, acoustid_duration))
        mb_metadata = []
        for result in acoustid_result:
            track = mb.getTrackById(result[1], TrackIncludes(artist=True, releases=True)) # TODO: needs to be limited, what if we get 20 results from acoustid?
            releases = []
            for release in track.getReleases():
                releases.append({
                    'name': release.getTitle(),
                    'tracknum': release.getTracksOffset() + 1,
                    'musicbrainz_release_id': release.getId()[release.getId().rfind('/') + 1:]
                })
            mb_metadata.append({
                'artist': track.getArtist().getName(),
                'title': track.getTitle(),
                'releases': releases,
Beispiel #51
0
 def query_fingerprint(self):
     self.duration, self.fingerprint = acoustid.fingerprint_file(self.path, 30)
Beispiel #52
0
# -*- coding: utf-8 -*-
__author__ = 'Glebov Boris'

import acoustid
import chromaprint

if __name__ == '__main__':
    duration, fp = acoustid.fingerprint_file('/home/warlock/Music/л1.mp3', 120)

    raw_fp = chromaprint.decode_fingerprint('')[0]
    fp = ','.join(map(str, raw_fp))

    print 'DURATION=%d' % duration
    print 'FINGERPRINT=%s' % fp
Beispiel #53
0
    def find_media(self, session, library):
        if not os.path.isdir(library.path):
            log.warning("Unable to find directory: '%s'", library.path)
            return
        for root, dirs, files in os.walk(library.path):
            if files:
                log.info("This is most likely an album: '%s'", root)
                items = [os.path.join(root, f) for f in files]
                media_files = []

                for item in items:
                    if any(item.lower().endswith('.' + x.lower()) for x in MEDIA_FORMATS):
                        song_path = os.path.join(root, item)
                        try:
                            media_files.append(MediaFile(song_path))
                        except (FileTypeError, UnreadableFileError):
                            log.warning("Unable to read media file '%s'", song_path)
                        except IOError:
                            log.warning("Unable to read media file '%s'", song_path)

                if media_files:
                    media_files = self.set_common_album(media_files)

                # album_metadata = self.album_metadata(media_files)

                for media_file in media_files:
                    media_dict = {
                        'artist': None,
                        'album': None,
                        'title': None,
                        'track': None,
                    }
                    duration, fingerprint = acoustid.fingerprint_file(media_file.path)

                    # m.format = MP3
                    # m.type = mp3

                    missing_metadata = False

                    # TODO: Make this all into a nice dictionary
                    # Set the artist
                    if media_file.albumartist:
                        media_dict['artist'] = media_file.albumartist
                    elif media_file.artist:
                        media_dict['artist'] = media_file.artist
                    else:
                        missing_metadata = True

                    # Set the album
                    if media_file.album:
                        media_dict['album'] = media_file.album
                    else:
                        media_dict['album'] = 'Unknown'
                        missing_metadata = True

                    # Set track information
                    if media_file.title:
                        media_dict['title'] = media_file.title
                    else:
                        missing_metadata = True

                    if media_file.track:
                        media_dict['track'] = media_file.track
                    else:
                        missing_metadata = True

                    if missing_metadata:
                        metadata = utils.metadata_from_filename(media_file.path)
                        if not media_dict['track']:
                            media_dict['track'] = metadata.get('track', 0)
                        if not media_dict['artist']:
                            media_dict['artist'] = metadata.get('artist', 'Unknown')
                        if not media_dict['title']:
                            media_dict['title'] = metadata.get('title', 'Unknown')

                    song = get_or_create(session, Song, path=os.path.dirname(media_file.path), filename=os.path.split(media_file.path)[1])
                    song.album = media_dict['album']
                    song.artist = media_dict['artist']
                    song.title = media_dict['title']
                    song.track = media_dict['track']
                    song.content_type = CONTENT_TYPES.get(media_file.type, 'mp3')
                    song.length = media_file.length
                    song.fingerprint = fingerprint
Beispiel #54
0
from __future__ import print_function

import json
import wget
import acoustid
import sys
import youtube_dl
API_KEY = 'HMU8Btr6'
filename1 = 'song.mp3'
dur,fingp = acoustid.fingerprint_file(filename1)

	
def aidmatch(filename):
    try:
        #results = acoustid.match(API_KEY, filename)
        results1 = acoustid.lookup(API_KEY, fingp, dur)
        results = acoustid.parse_lookup_result(results1)
    except acoustid.NoBackendError:
        print("chromaprint library/tool not found", file=sys.stderr)
        sys.exit(1)
    except acoustid.FingerprintGenerationError:
        print("fingerprint could not be calculated", file=sys.stderr)
        sys.exit(1)
    except acoustid.WebServiceError as exc:
        print("web service request failed:", exc.message, file=sys.stderr)
        sys.exit(1)

    first = True
    for score, rid, title, artist in results:
        if first:
            first = False
Beispiel #55
0
    def find_media(self, library):
        if not os.path.isdir(library.path):
            log.warning("Unable to find directory: '%s'", library.path)
            return
        for root, dirs, files in os.walk(library.path):
            if files:
                log.info("This is most likely an album: '%s'", root)
                items = [os.path.join(root, f) for f in files]
                media_files = []

                for item in items:
                    if any(item.lower().endswith("." + x.lower()) for x in MEDIA_FORMATS):
                        song_path = os.path.join(root, item)
                        try:
                            media_files.append(MediaFile(song_path))
                        except (FileTypeError, UnreadableFileError):
                            log.warning("Unable to read media file '%s'", song_path)
                        except IOError:
                            log.warning("Unable to read media file '%s'", song_path)

                if media_files:
                    media_files = self.set_common_album(media_files)

                # album_metadata = self.album_metadata(media_files)

                for media_file in media_files:
                    media_dict = {"artist": None, "album": None, "title": None, "track": None}
                    # TODO: This should be a celery job
                    duration, fingerprint = acoustid.fingerprint_file(media_file.path)

                    # m.format = MP3
                    # m.type = mp3

                    missing_metadata = False

                    # TODO: Make this all into a nice dictionary
                    # Set the artist
                    if media_file.albumartist:
                        media_dict["artist"] = media_file.albumartist
                    elif media_file.artist:
                        media_dict["artist"] = media_file.artist
                    else:
                        missing_metadata = True

                    # Set the album
                    if media_file.album:
                        media_dict["album"] = media_file.album
                    else:
                        media_dict["album"] = "Unknown"
                        missing_metadata = True

                    # Set track information
                    if media_file.title:
                        media_dict["title"] = media_file.title
                    else:
                        missing_metadata = True

                    if media_file.track:
                        media_dict["track"] = media_file.track
                    else:
                        missing_metadata = True

                    if missing_metadata:
                        metadata = utils.metadata_from_filename(media_file.path)
                        if not media_dict["track"]:
                            media_dict["track"] = metadata.get("track", 0)
                        if not media_dict["artist"]:
                            media_dict["artist"] = metadata.get("artist", "Unknown")
                        if not media_dict["title"]:
                            media_dict["title"] = metadata.get("title", "Unknown")

                    artist, artist_created = Artist.objects.get_or_create(name=media_dict["artist"])
                    # TODO: Should only need to do this once per folder/album
                    album, album_created = Album.objects.get_or_create(artist=artist, title=media_dict["album"])
                    song, song_created = Song.objects.get_or_create(
                        path=os.path.dirname(media_file.path),
                        filename=os.path.split(media_file.path)[1],
                        defaults={
                            "album": album,
                            "artist": artist,
                            "title": media_dict["title"],
                            "track": media_dict["track"],
                            "content_type": CONTENT_TYPES.get(media_file.type, "mp3"),
                            "length": media_file.length,
                            "fingerprint": fingerprint,
                        },
                    )

                    library.songs.add(song)
Beispiel #56
0
 def getAcoustidFingerprint(self):
     fp = acoustid.fingerprint_file(self._path)
     return fp[1]