Add --metadata-filename param to split_silence example

This commit is contained in:
Karl Kroening 2018-01-13 22:54:22 -08:00
parent 19f316e9c5
commit 5ca3b215be

View File

@ -4,6 +4,7 @@ from __future__ import unicode_literals
import argparse import argparse
import errno import errno
import ffmpeg import ffmpeg
import json
import logging import logging
import os import os
import re import re
@ -16,7 +17,7 @@ logger = logging.getLogger(__file__)
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
DEFAULT_DURATION = 0.3 DEFAULT_DURATION = 0.3
DEFAULT_THRESHOLD = -60 DEFAULT_THRESHOLD = -30
parser = argparse.ArgumentParser(description='Split media into separate chunks wherever silence occurs') parser = argparse.ArgumentParser(description='Split media into separate chunks wherever silence occurs')
parser.add_argument('in_filename', help='Input filename (`-` for stdin)') parser.add_argument('in_filename', help='Input filename (`-` for stdin)')
@ -26,6 +27,8 @@ parser.add_argument('--silence-duration', default=DEFAULT_DURATION, type=float,
parser.add_argument('--start-time', type=float, help='Start time (seconds)') 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('--end-time', type=float, help='End time (seconds)')
parser.add_argument('-v', dest='verbose', action='store_true', help='Verbose mode') parser.add_argument('-v', dest='verbose', action='store_true', help='Verbose mode')
parser.add_argument('--metadata-filename', help='Optional metadata output file')
silence_start_re = re.compile(' silence_start: (?P<start>[0-9]+(\.?[0-9]*))$') silence_start_re = re.compile(' silence_start: (?P<start>[0-9]+(\.?[0-9]*))$')
silence_end_re = re.compile(' silence_end: (?P<end>[0-9]+(\.?[0-9]*)) ') silence_end_re = re.compile(' silence_end: (?P<end>[0-9]+(\.?[0-9]*)) ')
@ -97,7 +100,8 @@ def get_chunk_times(in_filename, silence_threshold, silence_duration, start_time
def _makedirs(path): def _makedirs(path):
"""Python2-compatible version of ``os.makedirs(path, exist_ok=True)``.""" """Python2-compatible version of ``os.makedirs(path, exist_ok=True)``."""
try: try:
os.makedirs(path) if path:
os.makedirs(path)
except OSError as exc: except OSError as exc:
if exc.errno != errno.EEXIST or not os.path.isdir(path): if exc.errno != errno.EEXIST or not os.path.isdir(path):
raise raise
@ -110,17 +114,28 @@ def split_audio(
silence_duration=DEFAULT_DURATION, silence_duration=DEFAULT_DURATION,
start_time=None, start_time=None,
end_time=None, end_time=None,
metadata_filename=None,
verbose=False, verbose=False,
): ):
chunk_times = get_chunk_times(in_filename, silence_threshold, silence_duration, start_time, end_time) chunk_times = get_chunk_times(in_filename, silence_threshold, silence_duration, start_time, end_time)
metadata = []
for i, (start_time, end_time) in enumerate(chunk_times): for i, (start_time, end_time) in enumerate(chunk_times):
time = end_time - start_time time = end_time - start_time
out_filename = out_pattern.format(i, i=i) out_filename = out_pattern.format(i, i=i)
_makedirs(os.path.dirname(out_filename)) _makedirs(os.path.dirname(out_filename))
logger.info('{}: start={:.02f}, end={:.02f}, duration={:.02f}'.format(out_filename, start_time, end_time, start_text = '{:.02f}'.format(start_time)
time)) end_text = '{:.02f}'.format(end_time)
duration_text = '{:.02f}'.format(time)
metadata.append({
'filename': out_filename,
'start': start_text,
'end': end_text,
'duration': duration_text,
})
logger.info('{}: start={}, end={}, duration={}'.format(out_filename, start_text, end_text, duration_text))
_logged_popen( _logged_popen(
(ffmpeg (ffmpeg
.input(in_filename, ss=start_time, t=time) .input(in_filename, ss=start_time, t=time)
@ -132,6 +147,11 @@ def split_audio(
stderr=subprocess.PIPE if not verbose else None, stderr=subprocess.PIPE if not verbose else None,
).communicate() ).communicate()
if metadata_filename is not None:
_makedirs(os.path.dirname(metadata_filename))
with open(metadata_filename, 'w') as f:
json.dump(metadata, f)
if __name__ == '__main__': if __name__ == '__main__':
kwargs = vars(parser.parse_args()) kwargs = vars(parser.parse_args())