我正在尝试通过实时流协议 (RTSP) 从带有 OV5647 摄像头模块的 Raspberry Pi Zero W 以 H.264 传输视频。在接收端,我将使用装有 Ubuntu 24 的笔记本电脑来查看流。
我正在做一个实验,模拟从无人机相机卸载计算任务,并使用边缘节点来预处理和请求基于云的计算机视觉服务。因此,我有必要利用 RTSP 来创建流,并且最好将视频编码为 H.264。
Raspberry Pi 零 W 上的 Python 脚本:
import subprocess
import time
# RTSP Streaming Parameters
streamPort = 554 # Default RTSP Port
def start_rtsp_stream(): # Starts RTSP Stream using libcamera
try:
cmd = [ # Set stream parameters to
'libcamera-vid',
'-t', '0', '-v', # 0ms timeout, Verbose output for debugging
'--inline',
'--listen',
'--o', f'rtsp://0.0.0.0:{streamPort}/stream' # Enable access from any IP
]
# Start subprocess
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(f"RSTP stream started on rtsp://XXX.XXX.XXX.XXX:{streamPort}/stream")
print("Use a player like VLC to view the stream")
# Loop until interrupted
while True:
# stdoutput = process.stdout.readline() # read standard output
output = process.stderr.readline() # Read from stderr
# if stdoutput: # This was too volumous and I commented out to avoid the additional output
#print(stdoutput)
if output:
print(output.decode()) # If error, output.
time.sleep(1)
except KeyboardInterrupt: # Quit on Keyboard Interrupt
print("Stopping stream.")
process.terminate()
process.wait()
if __name__ == '__main__':
start_rtsp_stream()
输出
当我在 Raspberry Pi 上运行脚本时,我得到以下输出
camerapi@raspberrypi:~/Scripts $ python3 /home/camerapi/Scripts/StreamCamera.py
RSTP stream started on rtsp://XXX.XXX.XXX.XXX:554/stream
Use a player like VLC to view the stream
[16:07:06.541632480] [21589] INFO Camera camera_manager.cpp:325 libcamera v0.3.2+27-7330f29b
A bunch of camera details in here...
[16:07:06.979875052] [21589] INFO Camera camera.cpp:1197 configuring streams: (0) 640x480-YUV420 (1) 640x480-SGBRG10_CSI2P
[16:07:06.981470025] [21594] INFO RPI vc4.cpp:622 Sensor: /base/soc/i2c0mux/i2c@1/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
A bunch more camera details here...
Camera started!
Viewfinder frame 0
#0 (0.00 fps) exp 33167.00 ag 6.38 dg 1.00
terminate called after throwing an instance of 'std::runtime_error'
what(): failed to open output file rtsp://0.0.0.0:554/stream
我也尝试过:
libcamera-vid -t 0 -o rtsp://0.0.0.0:554/stream
输出
camerapi@raspberrypi:~/Scripts $ libcamera-vid -t 0 -o rtsp://0.0.0.0:554/stream
[16:12:09.513768085] [21704] INFO Camera camera_manager.cpp:325 libcamera v0.3.2+27-7330f29b
[16:12:09.885832770] [21708] WARN RPiSdn sdn.cpp:40 Using legacy SDN tuning - please consider moving SDN inside rpi.denoise
[16:12:09.906402420] [21708] INFO RPI vc4.cpp:447 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media3 and ISP device /dev/media1
[16:12:09.907872395] [21708] INFO RPI pipeline_base.cpp:1126 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
Preview window unavailable
Mode selection for 640:480:12:P
SGBRG10_CSI2P,640x480/0 - Score: 1000
SGBRG10_CSI2P,1296x972/0 - Score: 1287
SGBRG10_CSI2P,1920x1080/0 - Score: 1636.67
SGBRG10_CSI2P,2592x1944/0 - Score: 1854
Stream configuration adjusted
[16:12:09.927770058] [21704] INFO Camera camera.cpp:1197 configuring streams: (0) 640x480-YUV420 (1) 640x480-SGBRG10_CSI2P
[16:12:09.930069019] [21708] INFO RPI vc4.cpp:622 Sensor: /base/soc/i2c0mux/i2c@1/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA
#0 (0.00 fps) exp 33167.00 ag 6.25 dg 1.00
terminate called after throwing an instance of 'std::runtime_error'
what(): failed to open output file rtsp://0.0.0.0:554/stream
Aborted
我可以通过调用 libcamera show 让相机在树莓派上显示视频。然而,我似乎在创建 RTSP 流时遇到了最大的麻烦。
任何有足够专业知识的人都知道接下来要尝试什么吗?
ffmpeg -f v4l2 -framerate 30 -video_size 1280x720 -i /dev/video0 -c:v libx264 -b:v 3000k -maxrate 3000k -bufsize 5000k -f flv rtmp://local_LAN_server_IP_address/live/streamkey
ffmpeg 也能够转换为 RTSP,但我不是这方面的专家,所以如果下一步需要的话,请稍微探索一下。 您可以在这里找到更多详细信息: