add ffmpeg backend for linux compatibility
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import av
|
||||
import cv2
|
||||
import time
|
||||
|
||||
DEVICE = "/dev/video4"
|
||||
TARGET_FPS = 30
|
||||
WIDTH = 1280
|
||||
HEIGHT = 720
|
||||
|
||||
# Open v4l2 device with same style options as ffmpeg CLI
|
||||
container = av.open(
|
||||
DEVICE,
|
||||
format="v4l2",
|
||||
options={
|
||||
"input_format": "mjpeg",
|
||||
"video_size": f"{WIDTH}x{HEIGHT}",
|
||||
"framerate": str(TARGET_FPS),
|
||||
},
|
||||
)
|
||||
|
||||
stream = container.streams.video[0]
|
||||
frame_count = 0
|
||||
t_start = time.perf_counter()
|
||||
|
||||
for frame in container.decode(stream):
|
||||
t0 = time.perf_counter()
|
||||
img = frame.to_ndarray(format="bgr24") # OpenCV-compatible
|
||||
t1 = time.perf_counter()
|
||||
|
||||
cv2.imshow("pyav_cam", img)
|
||||
key = cv2.waitKey(1) & 0xFF
|
||||
t2 = time.perf_counter()
|
||||
|
||||
frame_count += 1
|
||||
if frame_count % 30 == 0:
|
||||
elapsed = t2 - t_start
|
||||
print(
|
||||
f"Frame {frame_count}: convert={1000*(t1-t0):.1f}ms, "
|
||||
f"imshow+waitKey={1000*(t2-t1):.1f}ms, avg_fps={frame_count/elapsed:.1f}"
|
||||
)
|
||||
|
||||
if key == ord("q"):
|
||||
break
|
||||
|
||||
container.close()
|
||||
cv2.destroyAllWindows()
|
||||
Reference in New Issue
Block a user