You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
import unittest
|
|
from io import BytesIO
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
import cv2
|
|
|
|
from ffmpeg_capture import FFmpegCapture, _parse_float, _parse_int, _parse_rate
|
|
from helpers import open_source
|
|
|
|
|
|
class FFmpegCaptureTests(unittest.TestCase):
|
|
def test_parse_fractional_rate(self):
|
|
self.assertAlmostEqual(_parse_rate("30000/1001"), 29.97002997)
|
|
|
|
def test_parse_invalid_rate(self):
|
|
self.assertEqual(_parse_rate("0/0"), 0.0)
|
|
self.assertEqual(_parse_rate("unknown"), 0.0)
|
|
|
|
def test_unknown_frame_count_and_duration_are_zero(self):
|
|
self.assertEqual(_parse_int("N/A"), 0)
|
|
self.assertEqual(_parse_int(None), 0)
|
|
self.assertEqual(_parse_float("N/A"), 0.0)
|
|
|
|
def test_raw_reader_does_not_duplicate_frames(self):
|
|
cap = FFmpegCapture.__new__(FFmpegCapture)
|
|
cap.source = "source.mp4"
|
|
cap.width = 640
|
|
cap.height = 480
|
|
with patch("ffmpeg_capture.subprocess.Popen") as popen:
|
|
cap._start()
|
|
command = popen.call_args.args[0]
|
|
self.assertEqual(command[command.index("-vsync") + 1], "0")
|
|
|
|
def test_reads_buffered_frame_after_ffmpeg_process_exits(self):
|
|
cap = FFmpegCapture.__new__(FFmpegCapture)
|
|
cap.width = 1
|
|
cap.height = 1
|
|
cap.frames_read = 0
|
|
cap.process = SimpleNamespace(
|
|
stdout=BytesIO(b"\x01\x02\x03"),
|
|
poll=lambda: 0,
|
|
)
|
|
|
|
ok, frame = cap.read()
|
|
|
|
self.assertTrue(ok)
|
|
self.assertEqual(frame.shape, (1, 1, 3))
|
|
self.assertEqual(frame.tolist(), [[[1, 2, 3]]])
|
|
|
|
@patch("helpers.FFmpegCapture")
|
|
def test_open_source_creates_ffmpeg_reader_with_file_path_only(self, capture):
|
|
reader = SimpleNamespace(isOpened=lambda: True)
|
|
capture.return_value = reader
|
|
|
|
opened, source_kind = open_source("clip.avi", cv2.CAP_FFMPEG)
|
|
|
|
capture.assert_called_once_with("clip.avi")
|
|
self.assertIs(opened, reader)
|
|
self.assertEqual(source_kind, "file")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|