Catch IndexError, KeyError in video_is_playable (#2113)

This commit is contained in:
Freddy Boulton 2022-08-29 12:57:04 -04:00 committed by GitHub
parent 4d58ae79b3
commit 85229a7058
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 5 deletions

View File

View File

@ -605,7 +605,7 @@ def video_is_playable(video_filepath: str) -> bool:
(".webm", "vp9"), (".webm", "vp9"),
] ]
# If anything goes wrong, assume the video can be played to not convert downstream # If anything goes wrong, assume the video can be played to not convert downstream
except FFRuntimeError: except (FFRuntimeError, IndexError, KeyError):
return True return True

View File

@ -9,6 +9,7 @@ from unittest.mock import patch
import ffmpy import ffmpy
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import pytest
from PIL import Image from PIL import Image
import gradio as gr import gradio as gr
@ -150,6 +151,24 @@ def test_video_has_playable_codecs(test_file_dir):
) )
def raise_ffmpy_runtime_exception(*args, **kwargs):
raise ffmpy.FFRuntimeError("", "", "", "")
@pytest.mark.parametrize(
"exception_to_raise", [raise_ffmpy_runtime_exception, KeyError(), IndexError()]
)
def test_video_has_playable_codecs_catches_exceptions(
exception_to_raise, test_file_dir
):
with patch("ffmpy.FFprobe.run", side_effect=exception_to_raise):
with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid:
shutil.copy(
str(test_file_dir / "bad_video_sample.mp4"), tmp_not_playable_vid.name
)
assert gr.processing_utils.video_is_playable(tmp_not_playable_vid.name)
def test_convert_video_to_playable_mp4(test_file_dir): def test_convert_video_to_playable_mp4(test_file_dir):
with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid: with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid:
shutil.copy( shutil.copy(
@ -161,10 +180,6 @@ def test_convert_video_to_playable_mp4(test_file_dir):
assert gr.processing_utils.video_is_playable(playable_vid) assert gr.processing_utils.video_is_playable(playable_vid)
def raise_ffmpy_runtime_exception():
raise ffmpy.FFRuntimeError("", "", "", "")
@patch("ffmpy.FFmpeg.run", side_effect=raise_ffmpy_runtime_exception) @patch("ffmpy.FFmpeg.run", side_effect=raise_ffmpy_runtime_exception)
def test_video_conversion_returns_original_video_if_fails(mock_run, test_file_dir): def test_video_conversion_returns_original_video_if_fails(mock_run, test_file_dir):
with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid: with tempfile.NamedTemporaryFile(suffix="out.avi") as tmp_not_playable_vid: