From dad4c3cb55224c8047344a9bb5bcefc2e4b23105 Mon Sep 17 00:00:00 2001 From: Karl Kroening Date: Mon, 15 Jan 2018 22:18:19 -0800 Subject: [PATCH] Add `--min-chunk-time` in split_silence example --- examples/split_silence.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/examples/split_silence.py b/examples/split_silence.py index 0234532..a83e5f4 100755 --- a/examples/split_silence.py +++ b/examples/split_silence.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import argparse +import copy import errno import ffmpeg import json @@ -27,6 +28,7 @@ parser.add_argument('--silence-duration', default=DEFAULT_DURATION, type=float, parser.add_argument('--start-time', type=float, help='Start time (seconds)') parser.add_argument('--end-time', type=float, help='End time (seconds)') parser.add_argument('--padding', type=float, default=0., help='Output silence padding (seconds)') +parser.add_argument('--min-chunk-time', type=float, default=0., help='Minimum chunk duration') parser.add_argument('--metadata-filename', help='Optional metadata output file') parser.add_argument('-v', dest='verbose', action='store_true', help='Verbose mode') @@ -108,6 +110,22 @@ def _makedirs(path): raise +def combine_chunks(chunk_times, min_chunk_time): + chunk_times = copy.copy(chunk_times) + pop_chunk = lambda chunk_times: (chunk_times[0][0], chunk_times[0][1], chunk_times[1:]) + new_chunk_times = [] + while len(chunk_times) != 0: + # Start new chunk. + start_time, end_time, chunk_times = pop_chunk(chunk_times) + time = end_time - start_time + while time < min_chunk_time and len(chunk_times) != 0: + # Combine with next chunk. + _, end_time, chunk_times = pop_chunk(chunk_times) + time = end_time - start_time + new_chunk_times.append((start_time, end_time)) + return new_chunk_times + + def split_audio( in_filename, out_pattern, @@ -116,10 +134,13 @@ def split_audio( start_time=None, end_time=None, padding=0., + min_chunk_time=0., metadata_filename=None, verbose=False, ): chunk_times = get_chunk_times(in_filename, silence_threshold, silence_duration, start_time, end_time) + if min_chunk_time > 0.: + chunk_times = combine_chunks(chunk_times, min_chunk_time) metadata = [] for i, (start_time, end_time) in enumerate(chunk_times):