diff --git a/silence_cutter.py b/silence_cutter.py index c38afd1..38f86ea 100644 --- a/silence_cutter.py +++ b/silence_cutter.py @@ -17,7 +17,7 @@ logger.addHandler(log_handler) -def findSilences(filename, dB = -35): +def findSilences(filename, dB = -35, padding = 0.025): """ returns a list: even elements (0,2,4, ...) denote silence start time @@ -27,6 +27,7 @@ def findSilences(filename, dB = -35): logging.debug(f"findSilences ()") logging.debug(f" - filename = {filename}") logging.debug(f" - dB = {dB}") + logging.debug(f" - padding = {padding}") command = ["ffmpeg","-i",filename, "-af","silencedetect=n=" + str (dB) + "dB:d=1", @@ -43,9 +44,9 @@ def findSilences(filename, dB = -35): logging.debug(" words: " + str(words)) for i in range (len(words)): if ("silence_start" in words[i]): - time_list.append (float(words[i+1])) + time_list.append (float(words[i+1]) + padding) if "silence_end" in words[i]: - time_list.append (float (words[i+1])) + time_list.append (float (words[i+1]) - padding) silence_section_list = list (zip(*[iter(time_list)]*2)) #return silence_section_list @@ -123,14 +124,15 @@ def ffmpeg_run (file, videoFilter, audioFilter, outfile): -def cut_silences(infile, outfile, dB = -35): +def cut_silences(infile, outfile, dB = -35, padding = 0.025): logging.debug(f"cut_silences ()") logging.debug(f" - infile = {infile}") logging.debug(f" - outfile = {outfile}") logging.debug(f" - dB = {dB}") + logging.debug(f" - padding = {padding}") print ("detecting silences") - silences = findSilences (infile,dB) + silences = findSilences (infile,dB,padding) duration = getVideoDuration (infile) videoSegments = getSectionsOfNewVideo (silences, duration) @@ -142,7 +144,7 @@ def cut_silences(infile, outfile, dB = -35): def printHelp(): print ("Usage:") - print (" silence_cutter.py [infile] [optional: outfile] [optional: dB]") + print (" silence_cutter.py [infile] [optional: outfile] [optional: dB] [optional: padding]") print (" ") print (" [outfile]") print (" Default: [infile]_cut") @@ -157,6 +159,10 @@ def printHelp(): print (" -50: Cuts are almost not recognizable.") print (" Cuts nothing, if there is background noise.") print (" ") + print (" [padding]") + print (" Default: 0.025") + print (" How much time to pad around each cut") + print (" ") print ("") print ("Dependencies:") print (" ffmpeg") @@ -183,15 +189,19 @@ def main(): tmp = os.path.splitext (infile) outfile = tmp[0] + "_cut" + tmp[1] dB = -30 + padding = 0.025 if (len(args) >= 2): outfile = args[1] if (len(args) >= 3): - dB = args[2] + dB = int(args[2]) + + if (len(args) >= 4): + padding = float(args[3]) - cut_silences (infile, outfile, dB) + cut_silences (infile, outfile, dB, padding) if __name__ == "__main__":