"""Detect max volume.""" import re import ffmpeg from video.connectors import s3 as s3_connector from video.constants import job_io_fields from video.logic.activity_task_logger import activity_task_logger from video.utils import text JOB_INPUTS = [ job_io_fields.INPUT_VIDEO_S3_BUCKET, job_io_fields.INPUT_VIDEO_S3_KEY, ] @activity_task_logger(JOB_INPUTS) def detect_max_volume(inputs): """Detect max volume. Args: inputs (dict): Inputs. Returns: dict: Outputs. """ s3_bucket = inputs[job_io_fields.INPUT_VIDEO_S3_BUCKET] s3_key = inputs[job_io_fields.INPUT_VIDEO_S3_KEY] output_options = { 'vn': None, # don't process video stream } video_s3_url = s3_connector.get_url_for_s3_object(s3_bucket, s3_key) (stdout, stderr) = ( ffmpeg .input(video_s3_url) .filter('volumedetect') .output('pipe:', format='null', **output_options) .run(capture_stderr=True) ) max_volume_regex = r'max_volume:\s*(\S*)' stderr_lines = text.bytes_to_lines_of_text(stderr) outputs = { job_io_fields.MAX_VOLUME_DECIBELS: None, } for line in reversed(stderr_lines): max_volume_match = re.search(max_volume_regex, line) if not max_volume_match: continue [max_volume_decibels] = max_volume_match.groups() outputs[job_io_fields.MAX_VOLUME_DECIBELS] = float(max_volume_decibels) return outputs return outputs