2014-02-22 23:56:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2014-12-16 02:30:33 +00:00
|
|
|
import os
|
2014-02-22 23:56:07 +00:00
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import warnings
|
2014-11-27 23:50:29 +00:00
|
|
|
import argparse
|
2014-02-22 23:56:07 +00:00
|
|
|
|
|
|
|
from dejavu import Dejavu
|
|
|
|
from dejavu.recognize import FileRecognizer
|
|
|
|
from dejavu.recognize import MicrophoneRecognizer
|
2014-11-27 23:50:29 +00:00
|
|
|
from argparse import RawTextHelpFormatter
|
2014-02-22 23:56:07 +00:00
|
|
|
|
|
|
|
warnings.filterwarnings("ignore")
|
|
|
|
|
2014-12-16 02:30:33 +00:00
|
|
|
DEFAULT_CONFIG_FILE = "dejavu.cnf.SAMPLE"
|
2014-02-22 23:56:07 +00:00
|
|
|
|
|
|
|
|
2014-12-16 02:30:33 +00:00
|
|
|
def init(configpath):
|
|
|
|
"""
|
|
|
|
Load config from a JSON file
|
2014-11-27 23:50:29 +00:00
|
|
|
"""
|
|
|
|
try:
|
2014-12-16 02:30:33 +00:00
|
|
|
with open(configpath) as f:
|
2014-11-27 23:50:29 +00:00
|
|
|
config = json.load(f)
|
|
|
|
except IOError as err:
|
2019-09-18 15:39:59 +00:00
|
|
|
print(("Cannot open configuration: %s. Exiting" % (str(err))))
|
2014-11-27 23:50:29 +00:00
|
|
|
sys.exit(1)
|
2014-02-22 23:56:07 +00:00
|
|
|
|
2014-11-27 23:50:29 +00:00
|
|
|
# create a Dejavu instance
|
|
|
|
return Dejavu(config)
|
2014-02-22 23:56:07 +00:00
|
|
|
|
|
|
|
|
2014-11-27 23:50:29 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(
|
2014-12-16 02:30:33 +00:00
|
|
|
description="Dejavu: Audio Fingerprinting library",
|
2014-11-27 23:50:29 +00:00
|
|
|
formatter_class=RawTextHelpFormatter)
|
|
|
|
parser.add_argument('-c', '--config', nargs='?',
|
|
|
|
help='Path to configuration file\n'
|
|
|
|
'Usages: \n'
|
2014-12-16 02:30:33 +00:00
|
|
|
'--config /path/to/config-file\n')
|
2014-11-27 23:50:29 +00:00
|
|
|
parser.add_argument('-f', '--fingerprint', nargs='*',
|
|
|
|
help='Fingerprint files in a directory\n'
|
|
|
|
'Usages: \n'
|
|
|
|
'--fingerprint /path/to/directory extension\n'
|
|
|
|
'--fingerprint /path/to/directory')
|
|
|
|
parser.add_argument('-r', '--recognize', nargs=2,
|
|
|
|
help='Recognize what is '
|
|
|
|
'playing through the microphone\n'
|
|
|
|
'Usage: \n'
|
|
|
|
'--recognize mic number_of_seconds \n'
|
|
|
|
'--recognize file path/to/file \n')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if not args.fingerprint and not args.recognize:
|
2014-12-16 02:30:33 +00:00
|
|
|
parser.print_help()
|
2014-11-27 23:50:29 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
config_file = args.config
|
|
|
|
if config_file is None:
|
|
|
|
config_file = DEFAULT_CONFIG_FILE
|
|
|
|
# print "Using default config file: %s" % (config_file)
|
|
|
|
|
|
|
|
djv = init(config_file)
|
|
|
|
if args.fingerprint:
|
|
|
|
# Fingerprint all files in a directory
|
2014-12-16 02:30:33 +00:00
|
|
|
if len(args.fingerprint) == 2:
|
2014-11-27 23:50:29 +00:00
|
|
|
directory = args.fingerprint[0]
|
|
|
|
extension = args.fingerprint[1]
|
2019-09-18 15:39:59 +00:00
|
|
|
print(("Fingerprinting all .%s files in the %s directory"
|
|
|
|
% (extension, directory)))
|
2014-11-27 23:50:29 +00:00
|
|
|
djv.fingerprint_directory(directory, ["." + extension], 4)
|
|
|
|
|
2014-12-16 02:30:33 +00:00
|
|
|
elif len(args.fingerprint) == 1:
|
2014-11-27 23:50:29 +00:00
|
|
|
filepath = args.fingerprint[0]
|
2014-12-16 02:30:33 +00:00
|
|
|
if os.path.isdir(filepath):
|
|
|
|
print("Please specify an extension if you'd like to fingerprint a directory!")
|
|
|
|
sys.exit(1)
|
2014-11-27 23:50:29 +00:00
|
|
|
djv.fingerprint_file(filepath)
|
|
|
|
|
|
|
|
elif args.recognize:
|
|
|
|
# Recognize audio source
|
|
|
|
song = None
|
|
|
|
source = args.recognize[0]
|
|
|
|
opt_arg = args.recognize[1]
|
|
|
|
|
|
|
|
if source in ('mic', 'microphone'):
|
|
|
|
song = djv.recognize(MicrophoneRecognizer, seconds=opt_arg)
|
|
|
|
elif source == 'file':
|
|
|
|
song = djv.recognize(FileRecognizer, opt_arg)
|
2018-09-22 07:32:11 +00:00
|
|
|
decoded_song = repr(song).decode('string_escape')
|
|
|
|
print(decoded_song)
|
2014-11-27 23:50:29 +00:00
|
|
|
|
|
|
|
sys.exit(0)
|