Skip to content

Commit

Permalink
Merge pull request #11 from mediacatch/124-current-uploader-package-t…
Browse files Browse the repository at this point in the history
…hrown-error-during-reading-ffprobe-stream-data

Look for duration in format if not available in stream
  • Loading branch information
FredHaa authored May 3, 2023
2 parents 2b31133 + efe0031 commit 2e3e35f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/mediacatch_s2t/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)'

Expand Down
12 changes: 10 additions & 2 deletions tests/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 2e3e35f

Please sign in to comment.