8000 Add `--min-chunk-time` in split_silence example · PearlStudio/ffmpeg-python@dad4c3c · GitHub
[go: up one dir, main page]

Skip to content

Commit dad4c3c

Browse files
committed
Add --min-chunk-time in split_silence example
1 parent 4c161bb commit dad4c3c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

examples/split_silence.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import unicode_literals
33

44
import argparse
5+
import copy
56
import errno
67
import ffmpeg
78
import json
@@ -27,6 +28,7 @@
2728
parser.add_argument('--start-time', type=float, help='Start time (seconds)')
2829
parser.add_argument('--end-time', type=float, help='End time (seconds)')
2930
parser.add_argument('--padding', type=float, default=0., help='Output silence padding (seconds)')
31+
parser.add_argument('--min-chunk-time', type=float, default=0., help='Minimum chunk duration')
3032
parser.add_argument('--metadata-filename', help='Optional metadata output file')
3133
parser.add_argument('-v', dest='verbose', action='store_true', help='Verbose mode')
3234

@@ -108,6 +110,22 @@ def _makedirs(path):
108110
raise
109111

110112

113+
def combine_chunks(chunk_times, min_chunk_time):
114+
chunk_times = copy.copy(chunk_times)
115+
pop_chunk = lambda chunk_times: (chunk_times[0][0], chunk_times[0][1], chunk_times[1:])
116+
new_chunk_times = []
117+
while len(chunk_times) != 0:
118+
# Start new chunk.
119+
start_time, end_time, chunk_times = pop_chunk(chunk_times)
120+
time = end_time - start_time
121+
while time < min_chunk_time and len(chunk_times) != 0:
122+
# Combine with next chunk.
123+
_, end_time, chunk_times = pop_chunk(chunk_times)
124+
time = end_time - start_time
125+
new_chunk_times.append((start_time, end_time))
126+
return new_chunk_times
127+
128+
111129
def split_audio(
112130
in_filename,
113131
out_pattern,
@@ -116,10 +134,13 @@ def split_audio(
116134
start_time=None,
117135
end_time=None,
118136
padding=0.,
137+
min_chunk_time=0.,
119138
metadata_filename=None,
120139
verbose=False,
121140
):
122141
chunk_times = get_chunk_times(in_filename, silence_threshold, silence_duration, start_time, end_time)
142+
if min_chunk_time > 0.:
143+
chunk_times = combine_chunks(chunk_times, min_chunk_time)
123144

124145
metadata = []
125146
for i, (start_time, end_time) in enumerate(chunk_times):

0 commit comments

Comments
 (0)
0