diff --git a/src/mediacatch_s2t/uploader.py b/src/mediacatch_s2t/uploader.py index 8815823..c8040a4 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 Exception: + 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)' 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():