worldveil / dejavu

Audio fingerprinting and recognition in Python
MIT License
6.36k stars 1.43k forks source link

erors using fingerprint_file #303

Open danijel1124 opened 6 months ago

danijel1124 commented 6 months ago

hi, when I try to use fingerprint_file I get the following error: " _fingerprint_worker() got an unexpected keyword argument 'song_name' " I just pass one argument to the function. " djv.fingerprint_file(file_path)" From the library source code:


        song_name_from_path = decoder.get_audio_name_from_path(file_path)
        song_hash = decoder.unique_hash(file_path)
        song_name = song_name or song_name_from_path```
Here you can see that file_path is necessary, but song_name is optional. So I'm actually doing everything right, I gues...
I've even made sure that the filenames passed in are certainly strings, thought that unexpected things happen... but that don't solves my issue.
Here's just my script, maybe you can do something with it...
```python
import os
import argparse
import datetime
from dejavu import Dejavu

# load config from a JSON file (or anything outputting a python dictionary)
config = {
    "database": {
        "host": "db",
        "user": "postgres",
        "password": "password",
        "database": "dejavu"
    },
    "database_type": "postgres"
}

def read_fingerprinted_files(directory):
    file_list = []
    try:
        with open(os.path.join(directory, 'fingerprinted.txt'), 'r') as f:
            lines = f.readlines()
            file_list = [line.strip() for line in lines[2:]]  # Skip the first two lines (date and 'Files:')
    except FileNotFoundError:
        pass
    return file_list

def write_fingerprinted_file(directory, files, supported_extensions):
    with open(os.path.join(directory, 'fingerprinted.txt'), 'w') as f:
        f.write(f"Fingerprinted on: {datetime.datetime.now()}\n")
        f.write("Files:\n")
        for file in files:
            if os.path.splitext(file)[1].lower() in supported_extensions:
                f.write(f"{file}\n")

def fingerprint_file(djv, file_path):
    try:
        if not isinstance(file_path, str):
            file_path = str(file_path)
        djv.fingerprint_file(file_path)
        print(f"Fingerprinted: {file_path}")
    except Exception as e:
        print(f"Error fingerprinting {file_path}: \n {e}")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Fingerprint audio files in a directory and its subdirectories.")
    parser.add_argument("directory", help="The path to the directory containing audio files.")
    args = parser.parse_args()

    # Supported audio file extensions: mp3, m4a, wav, etc.
    supported_extensions = [".mp3", ".m4a", ".wav"]

    # create a Dejavu instance
    djv = Dejavu(config)

    # Navigate through the main directory and each subdirectory
    for root, dirs, files in os.walk(args.directory):
        if root != args.directory:  # To avoid fingerprinting the parent directory itself
            existing_files = read_fingerprinted_files(root)
            current_files = [file for file in files if os.path.splitext(file)[1].lower() in supported_extensions]

            if set(existing_files) == set(current_files):
                print(f"Skipping directory (no changes): {root}")
                continue  

            # Fingerprint each file individually
            print(f"Fingerprinting directory: {root}")
            for file in current_files:
                full_path = os.path.join(root, file)
                fingerprint_file(djv, full_path)

            write_fingerprinted_file(root, current_files, supported_extensions)```
antanas-vasiliauskas commented 2 months ago

in init.py lines 136-141 song_name, hashes, file_hash = Dejavu._fingerprint_worker( file_path, self.limit, song_name=song_name ) sid = self.db.insert_song(song_name, file_hash) should be changed to song_name, hashes, file_hash = Dejavu._fingerprint_worker( (file_path, self.limit) ) sid = self.db.insert_song(song_name, file_hash, len(hashes))