Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions silence_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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__":
Expand Down