diff --git a/requirements.txt b/requirements.txt index ef6c133c9..a623d3b64 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,5 +15,6 @@ inotify requests matplotlib pillow +eyed3 pyarrow==20.0.0 soundfile diff --git a/scripts/utils/reporting.py b/scripts/utils/reporting.py index 171f90f6d..faf60581d 100644 --- a/scripts/utils/reporting.py +++ b/scripts/utils/reporting.py @@ -45,7 +45,7 @@ def extract_safe(in_file, out_file, start, stop): extract(in_file, out_file, safe_start, safe_stop) -def spectrogram(in_file, title, comment, raw=0): +def spectrogram(in_file, title, comment, raw=0) -> str: fd, tmp_file = tempfile.mkstemp(suffix='.png') os.close(fd) args = ['sox', '-V1', f'{in_file}', '-n', 'remix', '1', 'rate', '24k', 'spectrogram', @@ -68,8 +68,26 @@ def spectrogram(in_file, title, comment, raw=0): comment_font = ImageFont.truetype(get_font()['path'], 11) _, _, _, h = draw.textbbox((0, 0), comment, font=comment_font) draw.text((1, height - (h + 1)), comment, fill="white", font=comment_font) - img.save(f'{in_file}.png') + out_file = f'{in_file}.png' + img.save(out_file) os.remove(tmp_file) + return out_file + + +def attach_metadata(file_name: str, spectrogram_file: str, detection: Detection): + conf = get_settings() + if conf["AUDIOFMT"] != 'mp3': + # only ID3 tags for mp3 are implemented + return + import eyed3 + from eyed3.id3.frames import ImageFrame + audiofile = eyed3.load(file_name) + if (audiofile.tag == None): + audiofile.initTag() + audiofile.tag.title = f"{detection.common_name_safe} ({detection.scientific_name}) ({detection.confidence_pct}%)" + audiofile.tag.comments.set(f"{detection.confidence_pct}% confidence by {conf['MODEL']}") + audiofile.tag.images.set(ImageFrame.FRONT_COVER, open(spectrogram_file, 'rb').read(), 'image/png') + audiofile.tag.save() def extract_detection(file: ParseFileName, detection: Detection): @@ -82,7 +100,8 @@ def extract_detection(file: ParseFileName, detection: Detection): else: os.makedirs(new_dir, exist_ok=True) extract_safe(file.file_name, new_file, detection.start, detection.stop) - spectrogram(new_file, detection.common_name, new_file.replace(os.path.expanduser('~/'), ''), conf['RAW_SPECTROGRAM']) + spectrogram_file = spectrogram(new_file, detection.common_name, new_file.replace(os.path.expanduser('~/'), ''), conf['RAW_SPECTROGRAM']) + attach_metadata(new_file, spectrogram_file, detection) return new_file