"""Test detect_black_intervals_at_beginning_and_end_of_video.""" 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_black_intervals_at_beginning_and_end_of_video @pytest.mark.parametrize(( 'test_description', 'test_stderr', 'expected_black_interval_at_beginning_ends_at_seconds', 'expected_black_interval_at_end_begins_at_seconds', ), [ ( 'black intervals found', b""" some other stuff [blackdetect @ 0x7fa4dfd12f40] black_start:0 black_end:0.72 black_duration:0.72 some other stuff [blackdetect @ 0x7fa4dfd12f40] black_start:200 black_end:201 black_duration:1.00 [blackdetect @ 0x7fa4dfd12f40] black_start:210 black_end:215.55 black_duration:5.55 some other stuff [blackdetect @ 0x7fa4dfd12f40] black_start:249.16 black_end:249.6 black_duration:0.44 some other stuff """, # noqa 0.72, 249.16, ), ( 'black intervals not found', b'nope', None, None, ), ]) def test_detect_black_intervals_at_beginning_and_end_of_video( mocker, test_description, test_stderr, expected_black_interval_at_beginning_ends_at_seconds, expected_black_interval_at_end_begins_at_seconds, ): """Test detect_black_intervals_at_beginning_and_end_of_video.""" 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_BLACK_INTERVALS_AT_BEGINNING_AND_END_OF_VIDEO_JOB_ID: 123, # noqa job_io_fields.VIDEO_STREAM_FRAME_RATE_FRAMES_PER_SECOND: 25, job_io_fields.VIDEO_STREAM_DURATION_SECONDS: 249.64, 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.BLACK_INTERVAL_AT_BEGINNING_ENDS_AT_SECONDS: ( expected_black_interval_at_beginning_ends_at_seconds), job_io_fields.BLACK_INTERVAL_AT_END_BEGINS_AT_SECONDS: ( expected_black_interval_at_end_begins_at_seconds), } outputs = ( detect_black_intervals_at_beginning_and_end_of_video. detect_black_intervals_at_beginning_and_end_of_video(inputs) ) assert expected_outputs == outputs input_mock.assert_called_once_with('test_url') filter_mock.assert_called_once_with( 'blackdetect', black_min_duration=0.04, picture_black_ratio_th=1, pixel_black_th=0, ) output_mock.assert_called_once_with( 'pipe:', format='null', **{'an': None}, ) run_mock.assert_called_once_with( capture_stderr=True, )