Browse Source

feat: [LoudFE] keep only N top loudness Features

add LoudAudioFeatureExtractor._num_features (set in init, default: 5)

add _keep_num -- takes list of loudnesses and keeps top N (default: 5)
main
Rob Hallam 2 months ago
parent
commit
e716f5d80a
1 changed files with 9 additions and 2 deletions
  1. +9
    -2
      pipeline/feature_extractors.py

+ 9
- 2
pipeline/feature_extractors.py View File

@@ -200,12 +200,14 @@ class LoudAudioFeatureExtractor(FeatureExtractor):


teardown() is used to clean up temporary files created during setup (if specified by config) teardown() is used to clean up temporary files created during setup (if specified by config)
""" """
def __init__(self, input_files=None, config=None):
_CONFIG_DEFAULT_NUM_FEATURES = 5 # keep the top 5 loudnesses
def __init__(self, input_files=None, config=None, num_features=_CONFIG_DEFAULT_NUM_FEATURES):
if not input_files: if not input_files:
raise ValueError("No input files provided!") raise ValueError("No input files provided!")
self.input_files = input_files self.input_files = input_files
self.config = config self.config = config
self.features = [] self.features = []
self._num_features = num_features


def _audio_file_from_path(self, path: str) -> str: def _audio_file_from_path(self, path: str) -> str:
"""Return the audio file path given a video file path """Return the audio file path given a video file path
@@ -249,6 +251,10 @@ class LoudAudioFeatureExtractor(FeatureExtractor):


return loudness_features return loudness_features


def _keep_num(self, loudnesses, num=_CONFIG_DEFAULT_NUM_FEATURES) -> list:
"""Keep the top n loudnesses (default: 5)"""
return sorted(loudnesses, key=lambda x: x[1], reverse=True)[:num]

def setup(self): def setup(self):
"""extract audio from video files to be processed by pyloudnorm """extract audio from video files to be processed by pyloudnorm


@@ -266,7 +272,8 @@ class LoudAudioFeatureExtractor(FeatureExtractor):
for file in self.input_files: for file in self.input_files:
audio_file = self._audio_file_from_path(file.path) audio_file = self._audio_file_from_path(file.path)
loudnesses = self._loudnorm(audio_file) loudnesses = self._loudnorm(audio_file)
for time, loudness in loudnesses:
top_loudnesses = self._keep_num(loudnesses, self._num_features)
for time, loudness in top_loudnesses:
self.features.append(Feature(interval=Interval(start=time, duration=0.500), self.features.append(Feature(interval=Interval(start=time, duration=0.500),
source=file, feature_extractor="loudness", source=file, feature_extractor="loudness",
score=loudness)) score=loudness))


Loading…
Cancel
Save