Test if a video device can be opened with the given parameters. Args: device: Device identifier (e.g., "0:none", "0", "/dev/video0") format: Format string (e.g., "avfoundation", "v4l2", "dshow") size: Video resolution as "WIDTHxHEIGHT" (e.g., "1280x720")
(device, format="avfoundation", size="1280x720", fps="30", mode="test")
| 22 | |
| 23 | |
| 24 | async def test_video_device(device, format="avfoundation", size="1280x720", fps="30", mode="test"): |
| 25 | """ |
| 26 | Test if a video device can be opened with the given parameters. |
| 27 | |
| 28 | Args: |
| 29 | device: Device identifier (e.g., "0:none", "0", "/dev/video0") |
| 30 | format: Format string (e.g., "avfoundation", "v4l2", "dshow") |
| 31 | size: Video resolution as "WIDTHxHEIGHT" (e.g., "1280x720") |
| 32 | fps: Frames per second (e.g., 30) |
| 33 | mode: "test" (single frame), "live" (continuous display), or "save" (record video) |
| 34 | |
| 35 | Returns: |
| 36 | bool: True if device opens successfully, False otherwise |
| 37 | """ |
| 38 | |
| 39 | print(f"\n{'='*60}") |
| 40 | print(f"Testing: {device}") |
| 41 | print(f"Format: {format}") |
| 42 | print(f"Size: {size}") |
| 43 | print(f"FPS: {fps}") |
| 44 | print(f"Mode: {mode}") |
| 45 | print(f"{'='*60}") |
| 46 | |
| 47 | player = None |
| 48 | video_writer = None |
| 49 | stop_flag = False |
| 50 | |
| 51 | def signal_handler(sig, frame): |
| 52 | nonlocal stop_flag |
| 53 | print("\n🛑 Ctrl+C detected, stopping...") |
| 54 | stop_flag = True |
| 55 | |
| 56 | signal.signal(signal.SIGINT, signal_handler) |
| 57 | |
| 58 | try: |
| 59 | player = MediaPlayer(device, format=format, options={"video_size": size, "framerate": str(fps)}) |
| 60 | |
| 61 | if player.video: |
| 62 | print("✓ SUCCESS! Video device opened successfully.") |
| 63 | print(f" Video track: {player.video}") |
| 64 | |
| 65 | if mode == "test": |
| 66 | # Quick test - receive one frame |
| 67 | try: |
| 68 | frame = await asyncio.wait_for(player.video.recv(), timeout=2.0) |
| 69 | print(f"✓ Received video frame: {frame.width}x{frame.height}") |
| 70 | return True |
| 71 | except asyncio.TimeoutError: |
| 72 | print("⚠️ Device opened but no frame received within 2 seconds") |
| 73 | return False |
| 74 | except Exception as e: |
| 75 | print(f"⚠️ Error receiving frame: {e}") |
| 76 | return False |
| 77 | |
| 78 | elif mode == "live": |
| 79 | # Live display |
| 80 | print("📹 Starting live view (press Ctrl+C to stop)...") |
| 81 | frame_count = 0 |