File size: 1,006 Bytes
1fd4e9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# coding=utf-8
# Silence detection 
import logging
import librosa
import numpy as np


class Step4:
    def __init__(self):
        self.log = logging.getLogger(self.__class__.__name__)
        self.log.setLevel(logging.INFO)
    

    def run(self, 
            audio_path,
            silence_thresh=-50,
            duration_thresh=0.9):
        # self.log.info("Step4: Determine whether the audio is silent.")
        y, sr = librosa.load(audio_path, sr=None)
        energy = librosa.feature.rms(y=y)[0]
        energy_db = librosa.amplitude_to_db(energy)
        silent_ratio = np.sum(energy_db < silence_thresh) / len(energy_db)
        is_silent = silent_ratio > duration_thresh

        if is_silent:
            self.log.info(f"The audio after removing the voiceover ({audio_path}) is silent.")
        else:
            self.log.info(f"The audio after removing the voiceover ({audio_path}) is not silent.")
        self.log.info("Finish Step4 successfully.\n")
        
        return is_silent