From 6ece34c7281a1db8a6706fb3df3a4d93c2fa0722 Mon Sep 17 00:00:00 2001 From: Frederik Haarslev Date: Wed, 3 May 2023 11:12:55 +0200 Subject: [PATCH 1/2] Look for duration in format if not available in stream --- src/mediacatch_s2t/uploader.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/mediacatch_s2t/uploader.py b/src/mediacatch_s2t/uploader.py index 8815823..0474d3e 100644 --- a/src/mediacatch_s2t/uploader.py +++ b/src/mediacatch_s2t/uploader.py @@ -76,11 +76,17 @@ def get_duration(self): if probe.return_code: return 0, probe.error else: - for stream in probe.json['streams']: - if stream['codec_type'] == 'audio': - return int(float(stream['duration']) * 1000), stream - else: - return 0, "The file doesn't have an audio track" + try: + for stream in probe.json['streams']: + if stream['codec_type'] == 'audio': + return int(float(stream['duration']) * 1000), stream + else: + return 0, "The file doesn't have an audio track" + except: + if 'duration' in probe.json['format']: + return int(float(probe.json['format']['duration']) * 1000), probe.json['format'] + else: + return 0, "Duration couldn't be found for audio track" except OSError as e: return 0, 'FFmpeg not installed (sudo apt install ffmpeg)' From efe00310a8356b5810cf1b9138fbf351fd9330e9 Mon Sep 17 00:00:00 2001 From: Frederik Haarslev Date: Wed, 3 May 2023 11:28:26 +0200 Subject: [PATCH 2/2] Increased test coverage --- src/mediacatch_s2t/uploader.py | 2 +- tests/test_uploader.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/mediacatch_s2t/uploader.py b/src/mediacatch_s2t/uploader.py index 0474d3e..c8040a4 100644 --- a/src/mediacatch_s2t/uploader.py +++ b/src/mediacatch_s2t/uploader.py @@ -82,7 +82,7 @@ def get_duration(self): return int(float(stream['duration']) * 1000), stream else: return 0, "The file doesn't have an audio track" - except: + except Exception: if 'duration' in probe.json['format']: return int(float(probe.json['format']['duration']) * 1000), probe.json['format'] else: diff --git a/tests/test_uploader.py b/tests/test_uploader.py index 4dd1bcf..3467318 100644 --- a/tests/test_uploader.py +++ b/tests/test_uploader.py @@ -13,9 +13,17 @@ def test_is_file_exist_mocked_return_true(mock_is_file): @mock.patch("subprocess.run") def test_get_duration_mocked_return_value(mock_subprocess): mock_subprocess.return_value.returncode = 0 - mock_subprocess.return_value.stdout = '{"streams": [{"codec_type": "audio", "duration": 1}]}' + mock_subprocess.return_value.stdout = '{"streams": [{"codec_type": "audio", "duration": "1"}]}' mock_subprocess.return_value.stderr = None - assert Uploader('fake file', 'fake key').get_duration() == (1000, {'codec_type': 'audio', 'duration': 1}) + assert Uploader('fake file', 'fake key').get_duration() == (1000, {'codec_type': 'audio', 'duration': '1'}) + + +@mock.patch("subprocess.run") +def test_get_duration_audio_not_available_mocked_return_value(mock_subprocess): + mock_subprocess.return_value.returncode = 0 + mock_subprocess.return_value.stdout = '{"streams": [{"codec_type": "audio"}], "format": {"duration": "1"}}' + mock_subprocess.return_value.stderr = None + assert Uploader('fake file', 'fake key').get_duration() == (1000, {"duration": "1"}) def test_estimated_result_time():