|
1 | 1 | import os |
| 2 | +import shutil |
| 3 | +from unittest.mock import MagicMock, patch |
2 | 4 |
|
3 | 5 | import cv2 |
4 | 6 | import numpy as np |
5 | 7 | import pytest |
6 | 8 |
|
7 | | -from supervision.utils.video import VideoInfo, get_video_frames_generator, process_video |
| 9 | +from supervision.utils.video import ( |
| 10 | + VideoInfo, |
| 11 | + _mux_audio, |
| 12 | + get_video_frames_generator, |
| 13 | + process_video, |
| 14 | +) |
8 | 15 |
|
9 | 16 |
|
10 | 17 | @pytest.fixture |
@@ -206,6 +213,100 @@ def test_get_video_frames_generator_with_stride(dummy_video_path): |
206 | 213 | assert len(frames) == 5 |
207 | 214 |
|
208 | 215 |
|
| 216 | +def test_process_video_preserve_audio_calls_mux(dummy_video_path, tmp_path): |
| 217 | + """ |
| 218 | + Verify that process_video calls _mux_audio when preserve_audio=True. |
| 219 | +
|
| 220 | + Scenario: Processing a video with preserve_audio=True and ffmpeg available. |
| 221 | + Expected: _mux_audio is called exactly once with the correct source and target |
| 222 | + paths, confirming the audio muxing step is triggered after frame writing completes. |
| 223 | + """ |
| 224 | + target_path = str(tmp_path / "target_audio.mp4") |
| 225 | + |
| 226 | + with patch("supervision.utils.video._mux_audio") as mock_mux: |
| 227 | + process_video( |
| 228 | + source_path=dummy_video_path, |
| 229 | + target_path=target_path, |
| 230 | + callback=lambda frame, idx: frame, |
| 231 | + preserve_audio=True, |
| 232 | + ) |
| 233 | + mock_mux.assert_called_once_with( |
| 234 | + source_path=dummy_video_path, video_path=target_path |
| 235 | + ) |
| 236 | + |
| 237 | + |
| 238 | +def test_process_video_no_audio_by_default(dummy_video_path, tmp_path): |
| 239 | + """ |
| 240 | + Verify that process_video does not call _mux_audio when preserve_audio=False. |
| 241 | +
|
| 242 | + Scenario: Default process_video call without setting preserve_audio. |
| 243 | + Expected: _mux_audio is never called, preserving existing behavior for callers |
| 244 | + that do not need audio. |
| 245 | + """ |
| 246 | + target_path = str(tmp_path / "target_no_audio.mp4") |
| 247 | + |
| 248 | + with patch("supervision.utils.video._mux_audio") as mock_mux: |
| 249 | + process_video( |
| 250 | + source_path=dummy_video_path, |
| 251 | + target_path=target_path, |
| 252 | + callback=lambda frame, idx: frame, |
| 253 | + ) |
| 254 | + mock_mux.assert_not_called() |
| 255 | + |
| 256 | + |
| 257 | +@pytest.mark.parametrize( |
| 258 | + ("which_rv", "run_kwargs"), |
| 259 | + [ |
| 260 | + pytest.param(None, {}, id="ffmpeg_missing"), |
| 261 | + pytest.param( |
| 262 | + "/usr/bin/ffmpeg", |
| 263 | + {"return_value": MagicMock(returncode=1, stderr=b"")}, |
| 264 | + id="ffmpeg_fails", |
| 265 | + ), |
| 266 | + pytest.param( |
| 267 | + "/usr/bin/ffmpeg", |
| 268 | + {"side_effect": OSError("mux failed")}, |
| 269 | + id="subprocess_raises", |
| 270 | + ), |
| 271 | + ], |
| 272 | +) |
| 273 | +def test_mux_audio_file_unchanged_on_failure( |
| 274 | + dummy_video_path, tmp_path, which_rv, run_kwargs |
| 275 | +): |
| 276 | + """_mux_audio leaves the output file unchanged when muxing cannot complete.""" |
| 277 | + target_path = str(tmp_path / "video.mp4") |
| 278 | + shutil.copy(dummy_video_path, target_path) |
| 279 | + original_size = os.path.getsize(target_path) |
| 280 | + |
| 281 | + with ( |
| 282 | + patch("supervision.utils.video.shutil.which", return_value=which_rv), |
| 283 | + patch("supervision.utils.video.subprocess.run", **run_kwargs), |
| 284 | + ): |
| 285 | + _mux_audio(source_path=dummy_video_path, video_path=target_path) |
| 286 | + |
| 287 | + assert os.path.getsize(target_path) == original_size |
| 288 | + |
| 289 | + |
| 290 | +def test_mux_audio_replaces_file_on_success(dummy_video_path, tmp_path): |
| 291 | + """_mux_audio calls os.replace with video_path as destination on success.""" |
| 292 | + target_path = str(tmp_path / "video.mp4") |
| 293 | + shutil.copy(dummy_video_path, target_path) |
| 294 | + |
| 295 | + success_result = MagicMock() |
| 296 | + success_result.returncode = 0 |
| 297 | + success_result.stderr = b"" |
| 298 | + |
| 299 | + with ( |
| 300 | + patch("supervision.utils.video.shutil.which", return_value="/usr/bin/ffmpeg"), |
| 301 | + patch("supervision.utils.video.subprocess.run", return_value=success_result), |
| 302 | + patch("supervision.utils.video.os.replace") as mock_replace, |
| 303 | + ): |
| 304 | + _mux_audio(source_path=dummy_video_path, video_path=target_path) |
| 305 | + |
| 306 | + mock_replace.assert_called_once() |
| 307 | + assert mock_replace.call_args[0][1] == target_path |
| 308 | + |
| 309 | + |
209 | 310 | def test_get_video_frames_generator_with_start_end(dummy_video_path): |
210 | 311 | """ |
211 | 312 | Verify that get_video_frames_generator respects start and end frame indices. |
|
0 commit comments