2
2
from __future__ import unicode_literals
3
3
4
4
import argparse
5
+ import copy
5
6
import errno
6
7
import ffmpeg
7
8
import json
27
28
parser .add_argument ('--start-time' , type = float , help = 'Start time (seconds)' )
28
29
parser .add_argument ('--end-time' , type = float , help = 'End time (seconds)' )
29
30
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' )
30
32
parser .add_argument ('--metadata-filename' , help = 'Optional metadata output file' )
31
33
parser .add_argument ('-v' , dest = 'verbose' , action = 'store_true' , help = 'Verbose mode' )
32
34
@@ -108,6 +110,22 @@ def _makedirs(path):
108
110
raise
109
111
110
112
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
+
111
129
def split_audio (
112
130
in_filename ,
113
131
out_pattern ,
@@ -116,10 +134,13 @@ def split_audio(
116
134
start_time = None ,
117
135
end_time = None ,
118
136
padding = 0. ,
137
+ min_chunk_time = 0. ,
119
138
metadata_filename = None ,
120
139
verbose = False ,
121
140
):
122
141
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 )
123
144
124
145
metadata = []
125
146
for i , (start_time , end_time ) in enumerate (chunk_times ):
0 commit comments