我正在尝试编写一个函数来检测使用moviepy从视频文件中提取的音频文件中的所有异常镜头声音。结果应该是一个数据帧,其中包含视频的帧数以及每个帧中是否检测到异常镜头。音频文件示例的波形如图所示:
我编写了以下函数,但它预测所有帧均未检测到异常镜头。这是错误的,因为在某些帧上必须存在大于 0.25t 阈值的幅度。我的代码有什么问题。我检查过音频速率是 44100,帧速率是 25。
def create_shot(PATH):
rate=44100
signal=VideoFileClip(PATH).audio.to_soundarray() #audio.to_soundarray normalize the amplitude
signal=signal[:,0] #dual channel are identical so only select the first channel
length = len(signal) / rate
threshold=0.25 #manually set threshold, use 0.25 by analyzing the plotted normalized amplitude
# downsample to match video frame rate
downsampling_factor = int(rate / 25)
signal = signal[::downsampling_factor]
print(len(signal))
# detect shots based on amplitude threshold
shot_detected = (abs(signal) >= threshold).astype(int)
# create pandas dataframe with shot detections
shot = pd.DataFrame({'frame_count': range(1,len(shot_detected)+1),'shot_detected': shot_detected,'signal':signal})
return shot
感谢您的帮助。
抽取音频并不是计算声级的合适方法。相反,你应该计算能量。 函数 librosa.feature.rms 可用于此目的。您可以使用 44100/25 的 hop_length 来获得结果时间序列的 25 fps 分辨率。
我建议将能量转换为分贝,以获得更小范围的值,更好地代表人类的听力。可以使用librosa.power_to_db。