This is my code for video capture. I am using MAV_CMD_VIDEO_START_CAPTURE
and MAV_CMD_VIDEO_STOP_CAPTURE
mavlink commands:
from fastapi import FastAPI, HTTPException, status
from pymavlink import mavutil
import numpy as np
import cv2
import atexit
app = FastAPI()
# Connect to the MAVLink system
master = mavutil.mavlink_connection('udpin:localhost:14550')
master.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" %
(master.target_system, master.target_component))
# Initialize video-related variables
capturing = False
out = None
@app.get("/start_stream")
def start_stream():
global capturing, out
if not capturing:
capturing = True
master.mav.command_long_send(
master.target_system, master.target_component,
mavutil.mavlink.MAV_CMD_VIDEO_START_CAPTURE, 0, 1, 0, 0, 0, 0, 0, 0
)
out = cv2.VideoWriter("video_output.mp4", cv2.VideoWriter_fourcc(*'mp4v'), 30, (640, 480))
return {"message": "Stream started"}
else:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Stream already started")
@app.get("/stop_stream")
def stop_stream():
global capturing, out
if capturing:
capturing = False
master.mav.command_long_send(
master.target_system, master.target_component,
mavutil.mavlink.MAV_CMD_VIDEO_STOP_CAPTURE, 0, 1, 0, 0, 0, 0, 0, 0
)
if out:
out.release()
return {"message": "Stream stopped and saved"}
else:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="No active stream to stop")
@app.get("/status")
def get_status():
global capturing
return {"stream_status": "capturing" if capturing else "stopped"}
@app.on_event("shutdown")
def shutdown_event():
global master
master.close()
def capture_frames():
while True:
if capturing:
msg = master.recv_match(type='DATA_STREAM', blocking=True, timeout=5)
if msg and msg.stream_id == 0:
video_data = msg.data
frame = np.frombuffer(video_data, dtype=np.uint8).reshape(480, 640, 3)
out.write(frame)
# Start capturing frames in a separate thread
import threading
capture_thread = threading.Thread(target=capture_frames)
capture_thread.start()
atexit.register(shutdown_event)
I am getting this output: {"message":"Stream started"}
and Got COMMAND_ACK: VIDEO_START_CAPTURE: FAILED
Got COMMAND_ACK: VIDEO_STOP_CAPTURE: FAILED
in ubuntu using mission planner …please could you give me suggestions guys