Try to fix an issue with MediaInfo perhaps returning a str instead of an int

This commit is contained in:
Raoul Snyman 2023-08-11 23:21:16 -07:00
parent 8052d29fa8
commit 1eb8ef5f0d
2 changed files with 44 additions and 2 deletions

View File

@ -368,7 +368,7 @@ class MediaController(QtWidgets.QWidget, RegistryBase, LogMixin, RegistryPropert
return is_autoplay
@staticmethod
def media_length(media_path: Union[str, Path]):
def media_length(media_path: Union[str, Path]) -> int:
"""
Uses Media Info to obtain the media length
@ -385,7 +385,15 @@ class MediaController(QtWidgets.QWidget, RegistryBase, LogMixin, RegistryPropert
with Path(media_path).open('rb') as media_file:
media_data = MediaInfo.parse(media_file)
# duration returns in milli seconds
return media_data.tracks[0].duration or 0
duration = media_data.tracks[0].duration
# It appears that sometimes we get a string. Let's try to interpret that as int, or fall back to 0
# See https://gitlab.com/openlp/openlp/-/issues/1387
if isinstance(duration, str):
if duration.strip().isdigit():
duration = int(duration.strip())
else:
duration = 0
return duration or 0
return 0
def media_setup_optical(self, filename, title, audio_track, subtitle_track, start, end,

View File

@ -584,6 +584,40 @@ def test_media_length_duration_none(MockPath, mocked_parse, media_env):
assert duration == 0, 'The duration should be 0'
@patch('openlp.core.ui.media.mediacontroller.MediaInfo.parse')
@patch('openlp.core.ui.media.mediacontroller.Path')
def test_media_length_duration_string_digits(MockPath, mocked_parse, media_env):
"""
Test that when MediaInfo gives us a duration, but it is a string, we typecast it
"""
# GIVEN: A fake media file and a mocked MediaInfo.parse() function
mocked_parse.return_value = MagicMock(tracks=[MagicMock(duration='546')])
file_path = 'path/to/fake/video.mkv'
# WHEN the media data is retrieved
duration = media_env.media_controller.media_length(file_path)
# THEN you can determine the run time
assert duration == 546, 'The duration should be 546'
@patch('openlp.core.ui.media.mediacontroller.MediaInfo.parse')
@patch('openlp.core.ui.media.mediacontroller.Path')
def test_media_length_duration_string_alpha(MockPath, mocked_parse, media_env):
"""
Test that when MediaInfo gives us a duration, but it is a string of nonsense, we default to 0
"""
# GIVEN: A fake media file and a mocked MediaInfo.parse() function
mocked_parse.return_value = MagicMock(tracks=[MagicMock(duration='sdffgh')])
file_path = 'path/to/fake/video.mkv'
# WHEN the media data is retrieved
duration = media_env.media_controller.media_length(file_path)
# THEN you can determine the run time
assert duration == 0, 'The duration should be 0'
@patch('openlp.core.ui.media.mediacontroller.MediaInfo.parse')
def test_media_length_duration_old_version(mocked_parse, media_env):
"""