"""Test detect_silent_intervals_at_beginning_and_end_of_audio.""" import ffmpeg import pytest from video.connectors import s3 as s3_connector from video.constants import job_io_fields from video.logic.activities import \ detect_silent_intervals_at_beginning_and_end_of_audio @pytest.mark.parametrize(( 'test_description', 'test_stderr', 'expected_silent_interval_at_beginning_ends_at_seconds', 'expected_silent_interval_at_end_begins_at_seconds', ), [ ( 'silent intervals found', b""" some other stuff [silencedetect @ 0x7fed3cd16f80] silence_start: 0 some other stuff [silencedetect @ 0x7fed3cd16f80] silence_end: 0.613708 | silence_duration: 0.613708 some other stuff [silencedetect @ 0x7fed3cd16f80] silence_start: 53.07 [silencedetect @ 0x7fed3cd16f80] silence_end: 53.2019 | silence_duration: 0.131937 some other stuff [silencedetect @ 0x7fed3cd16f80] silence_start: 55.7872 [silencedetect @ 0x7fed3cd16f80] silence_end: 56.0287 | silence_duration: 0.241458 some other stuff [silencedetect @ 0x7fed3cd16f80] silence_start: 56.9522 some other stuff [silencedetect @ 0x7fed3cd16f80] silence_end: 57.0451 | silence_duration: 0.09287 some other stuff [silencedetect @ 0x7fed3cd16f80] silence_start: 248.886 some other stuff [silencedetect @ 0x7fed3cd16f80] silence_end: 249.6 | silence_duration: 0.713792 """, # noqa 0.613708, 248.886, ), ( 'silent intervals not found', b'nope', None, None, ), ]) def test_detect_silent_intervals_at_beginning_and_end_of_audio( mocker, test_description, test_stderr, expected_silent_interval_at_beginning_ends_at_seconds, expected_silent_interval_at_end_begins_at_seconds, ): """Test detect_silent_intervals_at_beginning_and_end_of_audio.""" mocker.patch.object( s3_connector, 'get_url_for_s3_object', return_value='test_url', autospec=True, ) mocker.patch.object( ffmpeg, 'input', autospec=True, ) input_mock = ffmpeg.input filter_mock = input_mock.return_value.filter output_mock = filter_mock.return_value.output run_mock = output_mock.return_value.run run_mock.return_value = 'test stdout'.encode(), test_stderr inputs = { job_io_fields.DETECT_SILENT_INTERVALS_AT_BEGINNING_AND_END_OF_AUDIO_JOB_ID: 123, # noqa job_io_fields.VIDEO_STREAM_FRAME_RATE_FRAMES_PER_SECOND: 25, job_io_fields.AUDIO_STREAM_DURATION_SECONDS: 249.6, job_io_fields.INPUT_VIDEO_S3_BUCKET: 'test_bucket', job_io_fields.INPUT_VIDEO_S3_KEY: 'test_key/123.mov', } expected_outputs = { job_io_fields.SILENT_INTERVAL_AT_BEGINNING_ENDS_AT_SECONDS: ( expected_silent_interval_at_beginning_ends_at_seconds), job_io_fields.SILENT_INTERVAL_AT_END_BEGINS_AT_SECONDS: ( expected_silent_interval_at_end_begins_at_seconds), } outputs = detect_silent_intervals_at_beginning_and_end_of_audio\ .detect_silent_intervals_at_beginning_and_end_of_audio(inputs) assert expected_outputs == outputs input_mock.assert_called_once_with('test_url') filter_mock.assert_called_once_with( 'silencedetect', duration=0.04, noise='-90dB', ) output_mock.assert_called_once_with( 'pipe:', format='null', **{'vn': None}, ) run_mock.assert_called_once_with( capture_stderr=True, )