def scan_file_main(self, option, start_time, stop_time):
        try:
            filepath = option.file_path
            step = option.step
            rec_length = option.rec_length
            with_duration = option.with_duration
            out_dir = option.out_dir
            if start_time == 0 and stop_time == 0:
                file_total_seconds = int(
                    ACRCloudRecognizer.get_duration_ms_by_file(filepath) /
                    1000)
                results = self.recognize_file(filepath, start_time,
                                              file_total_seconds, step,
                                              rec_length, with_duration)
            else:
                results = self.recognize_file(filepath, start_time, stop_time,
                                              step, rec_length, with_duration)

            filename = 'result-' + os.path.basename(filepath.strip()) + '.csv'
            fpath = os.path.join(out_dir, filename)
            if os.path.exists(fpath):
                os.remove(fpath)
            if results:
                self.export_to_csv(results, filename, out_dir)

            if with_duration == 1:
                new_results = []
                if results:
                    new_results = self.apply_filter(results)
                filename_with_duration = 'result-' + os.path.basename(
                    filepath.strip()) + '_with_duration.csv'
                self.export_to_csv(new_results, filename_with_duration,
                                   out_dir)
        except Exception as e:
            self.dlog.logger.error("scan_file_main.error", exc_info=True)
    def scan_file_main(self, option, start_time, stop_time):
        try:
            filepath = option.file_path
            step = option.step
            rec_length = option.rec_length
            with_duration = option.with_duration
            if start_time == 0 and stop_time == 0:
                file_total_seconds =  int(ACRCloudRecognizer.get_duration_ms_by_file(filepath)/1000)
                results = self.recognize_file(filepath, start_time, file_total_seconds, step, rec_length, with_duration)
            else:
                results = self.recognize_file(filepath, start_time, stop_time, step, rec_length, with_duration)

            filename = 'result-' + os.path.basename(filepath.strip()) + '.csv'
            if os.path.exists(filename):
                os.remove(filename)
            if results:
                self.export_to_csv(results, filename)

            if with_duration == 1:
                new_reuslts = []
                if results:
                    new_results = self.apply_filter(results)
                filename_with_duration =  'result-' + os.path.basename(filepath.strip()) + '_with_duration.csv'
                self.export_to_csv(new_results, filename_with_duration)
        except Exception as e:
            self.dlog.logger.error("scan_file_main.error", exc_info=True)
示例#3
0
def get_tracks_from_audio(file):
    response = ({}, 404)

    if file is None or file == '':
        print('invalid audio file')
        response = ({}, 400)

    else:
        config = {
            'host': 'identify-us-west-2.acrcloud.com',
            'access_key': os.environ.get('ACCESS_KEY'),
            'access_secret': os.environ.get('ACCESS_SECRET'),
            'recognize_type': ACRCloudRecognizeType.ACR_OPT_REC_BOTH,
            'debug': False,
            'timeout': 10  # seconds
        }

        '''This module can recognize ACRCloud by most of audio/video file.
            Audio: mp3, wav, m4a, flac, aac, amr, ape, ogg ...
            Video: mp4, mkv, wmv, flv, ts, avi ...'''

        recognizer = ACRCloudRecognizer(config)

        f = tempfile.NamedTemporaryFile()
        f.write(file.read())

        duration = ACRCloudRecognizer.get_duration_ms_by_file(str(f.name))
        print("duration_ms=" + str(duration))

        if duration // 1000 > 10:
            max_duration = max(10, (duration * 20 // 100) // 1000)
        else:
            max_duration = 10

        result = json.loads(recognizer.recognize_by_file(str(f.name), 0, max_duration))
        print(result)

        f.close()

        tracks = process_metadata(result)

        data = {
            'data': tracks
        }

        response = (data, 200)

        print(json.dumps(response[0], indent=4))

    return response
示例#4
0
 def recognize_song(self, file):
     re = ACRCloudRecognizer(self.config)
     print('Recognizing song')
     result = re.recognize_by_file(file, 0, 10)
     result = json.loads(result)
     success = result.get('status').get('msg')
     if success == 'Success':
         #print(result.get('metadata').get('music')[0])
         artist = result.get('metadata').get('music')[0].get(
             'artists')[0].get('name')
         title = result.get('metadata').get('music')[0].get('title')
         offset = result.get('metadata').get('music')[0].get(
             'play_offset_ms')
         duration = result.get('metadata').get('music')[0].get(
             'duration_ms')
         print("duration_ms=" +
               str(ACRCloudRecognizer.get_duration_ms_by_file(file)))
         value = (artist, title, float(offset) / duration, duration)
     else:
         value = None
     print(value)
     return value
示例#5
0
if __name__ == '__main__':
    config = {
        'host':'XXXXXXXX',
        'access_key':'XXXXXXXX',
        'access_secret':'XXXXXXXX',
        'debug':False,
        'timeout':10 # seconds
    }
    
    '''This module can recognize ACRCloud by most of audio/video file. 
        Audio: mp3, wav, m4a, flac, aac, amr, ape, ogg ...
        Video: mp4, mkv, wmv, flv, ts, avi ...'''
    re = ACRCloudRecognizer(config)

    #recognize by file path, and skip 0 seconds from from the beginning of sys.argv[1].
    print(re.recognize_by_file(sys.argv[1], 0))

    print("duration_ms=" + str(ACRCloudRecognizer.get_duration_ms_by_file(sys.argv[1])))

    buf = open(sys.argv[1], 'rb').read()
    #recognize by file_audio_buffer that read from file path, and skip 0 seconds from from the beginning of sys.argv[1].
    print(re.recognize_by_filebuffer(buf, 0))

    #aa.wav must be (RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 8000 Hz)
    #buf = open('aa.wav', 'rb').read()
    #buft = buf[1024000:192000+1024001]
    #recognize by audio_buffer(RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 8000 Hz)
    #print re.recognize(buft)

 def get_duration_by_file(self, filepath):
     return int(ACRCloudRecognizer.get_duration_ms_by_file(filepath)/1000)
示例#7
0
    def scan_file_main(self, option, start_time, stop_time):
        try:
            filepath = option.file_path
            step = option.step
            rec_length = option.rec_length
            with_duration = option.with_duration
            out_dir = option.out_dir
            if out_dir and not os.path.exists(out_dir):
                try:
                    os.makedirs(out_dir)
                except Exception as e:
                    self.dlog.logger.error(
                        "scan_file_main.create_out_dir_error:{0}, please check it!"
                        .format(out_dir),
                        exc_info=True)
                    return

            file_type = option.file_type
            if start_time == 0 and stop_time == 0:
                file_total_seconds = int(
                    ACRCloudRecognizer.get_duration_ms_by_file(filepath) /
                    1000)
                results = self.recognize_file(filepath, start_time,
                                              file_total_seconds, step,
                                              rec_length, with_duration)
            else:
                results = self.recognize_file(filepath, start_time, stop_time,
                                              step, rec_length, with_duration)

            filename_csv = 'result-' + os.path.basename(
                filepath.strip()) + '.csv'
            filename_xlsx = 'result-' + os.path.basename(
                filepath.strip()) + '.xlsx'
            filename_json = 'result-' + os.path.basename(
                filepath.strip()) + '.json'

            if results:
                if file_type == "csv":
                    self.export_to_csv(results, filename_csv, out_dir)
                elif file_type == "json":
                    self.export_to_json(results, filename_json, out_dir)
                else:
                    self.export_to_xlsx(results, filename_xlsx, out_dir)

            if with_duration == 1:
                new_results = []
                if results:
                    new_results = self.apply_filter(results)

                filename_with_duration_csv = 'result-' + os.path.basename(
                    filepath.strip()) + '_with_duration.csv'
                filename_with_duration_xlsx = 'result-' + os.path.basename(
                    filepath.strip()) + '_with_duration.xlsx'
                filename_with_duration_json = 'result-' + os.path.basename(
                    filepath.strip()) + '_with_duration.json'

                if file_type == "csv":
                    self.export_to_csv(new_results, filename_with_duration_csv,
                                       out_dir)
                elif file_type == "json":

                    self.export_to_json(new_results,
                                        filename_with_duration_json, out_dir)
                else:
                    self.export_to_xlsx(new_results,
                                        filename_with_duration_xlsx, out_dir)
        except Exception as e:
            self.dlog.logger.error("scan_file_main.error", exc_info=True)
        return