I'm trying to write a function that detect all abnormal shot sound in a audio file extracted from the video file using moviepy. The result should be a dataframe that contains the frame count of video and in each frame whether an abnormal shot is detected. The wave form of an example of the audio file is shown in the photo:
I wrote this following function but it predicts all frame to be not abnormal shot detected.This is wrong because at some frame there must be an amplitude that bigger than the 0.25t threshold. What is wrong with my code. I've checked the audio rate is 44100 and frame rate is 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
Thanks for any help.