26
26
parser .add_argument ('--silence-duration' , default = DEFAULT_DURATION , type = float , help = 'Silence duration' )
27
27
parser .add_argument ('--start-time' , type = float , help = 'Start time (seconds)' )
28
28
parser .add_argument ('--end-time' , type = float , help = 'End time (seconds)' )
29
- parser .add_argument ('-v ' , dest = 'verbose' , action = 'store_true' , help = 'Verbose mode ' )
29
+ parser .add_argument ('--padding ' , type = float , default = 0. , help = 'Output silence padding (seconds) ' )
30
30
parser .add_argument ('--metadata-filename' , help = 'Optional metadata output file' )
31
+ parser .add_argument ('-v' , dest = 'verbose' , action = 'store_true' , help = 'Verbose mode' )
31
32
32
33
33
34
silence_start_re = re .compile (' silence_start: (?P<start>[0-9]+(\.?[0-9]*))$' )
@@ -50,7 +51,7 @@ def get_chunk_times(in_filename, silence_threshold, silence_duration, start_time
50
51
if end_time is not None :
51
52
input_kwargs ['t' ] = end_time - start_time
52
53
53
- p = _logged_popen (
54
+ child = _logged_popen (
54
55
(ffmpeg
55
56
.input (in_filename , ** input_kwargs )
56
57
.filter_ ('silencedetect' , n = '{}dB' .format (silence_threshold ), d = silence_duration )
@@ -59,8 +60,8 @@ def get_chunk_times(in_filename, silence_threshold, silence_duration, start_time
59
60
) + ['-nostats' ], # FIXME: use .nostats() once it's implemented in ffmpeg-python.
60
61
stderr = subprocess .PIPE
61
62
)
62
- output = p .communicate ()[1 ].decode ('utf-8' )
63
- if p .returncode != 0 :
63
+ output = child .communicate ()[1 ].decode ('utf-8' )
64
+ if child .returncode != 0 :
64
65
sys .stderr .write (output )
65
66
sys .exit (1 )
66
67
logger .debug (output )
@@ -114,6 +115,7 @@ def split_audio(
114
115
silence_duration = DEFAULT_DURATION ,
115
116
start_time = None ,
116
117
end_time = None ,
118
+ padding = 0. ,
117
119
metadata_filename = None ,
118
120
verbose = False ,
119
121
):
@@ -125,9 +127,9 @@ def split_audio(
125
127
out_filename = out_pattern .format (i , i = i )
126
128
_makedirs (os .path .dirname (out_filename ))
127
129
128
- start_text = '{:.02f }' .format (start_time )
129
- end_text = '{:.02f }' .format (end_time )
130
- duration_text = '{:.02f }' .format (time )
130
+ start_text = '{:.04f }' .format (start_time )
131
+ end_text = '{:.04f }' .format (end_time )
132
+ duration_text = '{:.04f }' .format (time )
131
133
metadata .append ({
132
134
'filename' : out_filename ,
133
135
'start' : start_text ,
@@ -136,16 +138,24 @@ def split_audio(
136
138
})
137
139
logger .info ('{}: start={}, end={}, duration={}' .format (out_filename , start_text , end_text , duration_text ))
138
140
139
- _logged_popen (
140
- (ffmpeg
141
- .input (in_filename , ss = start_time , t = time )
141
+ input = ffmpeg .input (in_filename , ss = start_time , t = time )
142
+ if padding > 0. :
143
+ silence = ffmpeg .input ('anullsrc' , format = 'lavfi' , t = padding )
144
+ input = ffmpeg .concat (silence , input , silence , v = 0 , a = 1 )
145
+
146
+ child = _logged_popen (
147
+ (input
142
148
.output (out_filename )
143
149
.overwrite_output ()
144
150
.compile ()
145
151
),
146
152
stdout = subprocess .PIPE if not verbose else None ,
147
153
stderr = subprocess .PIPE if not verbose else None ,
148
- ).communicate ()
154
+ )
155
+ out = child .communicate ()
156
+ if child .returncode != 0 :
157
+ if not verbose :
158
+ sys .stderr .write (out [1 ].decode ('utf-8' ))
149
159
150
160
if metadata_filename is not None :
151
161
_makedirs (os .path .dirname (metadata_filename ))
0 commit comments