gpt4 book ai didi

python - 断开连接后在python中重新连接到PiCamera流

转载 作者:行者123 更新时间:2023-12-03 12:08:54 24 4
gpt4 key购买 nike

我目前正在尝试编程和学习python,同时使树莓派pi w为零。到目前为止,我只是想让它开始使用python中的picamera开始录制视频,并对该视频进行流传输,以便我可以监视手机上的输出。但是,从目前的情况来看,只有在我通过某种流媒体程序连接到视频后,它才开始录制视频。我想要做的是在程序开始时开始录制视频,并随时可以连接到它。就目前而言,我可以毫无问题地连接到它,但是我无法重新连接到它。

我想要的基本想法是这样的。

Start Recording
Listen on port 8080
if connection is started
start streaming video stream 2 (also known as splitter port)
else connection ended
wait for new connection

我意识到这听起来很可怕。我希望它能大致说明我要做什么。就像我说的那样,我只是在学习python,并且只具备Basic的一些基本知识。

这是我目前正在使用的代码。就像我说的那样,它只有在连接到它时才起作用。
#!/usr/bin/python

import socket
import picamera
import datetime as dt
import os.path

filename = 'hauntvideo'
save_path = '/home/pi'
completed_video = os.path.join(save_path, filename)

import warnings
warnings.filterwarnings('error', category=DeprecationWarning)

#Camera Setup
with picamera.PiCamera() as camera:
camera.resolution = (1920, 1080)
camera.framerate = 30
camera.hflip = True
camera.vflip = True
#Connection Listening
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8080))
server_socket.listen(5)

connection = server_socket.accept()[0].makefile('wb')

try:
camera.start_recording(connection, format='h264', splitter_port=2,
resize=(640,360))
camera.start_recording(completed_video + '{}.h264'.format(
dt.datetime.now().strftime('%Y%m%d%H%M%S') ), bitrate=4500000)
camera.wait_recording(7*60*60)
camera.stop_recording()
finally:
connection.close()
server_socket.close()
quit()

最佳答案

However as it currently stands it only starts recording video once I connect to it via some sort of streaming program. What I'd like for it to do is start recording the video at the start of the program and be able to connect to it whenever I'd like to monitor it.



这是由于以下代码行:
connection = server_socket.accept()[0].makefile('wb')

accept()函数将阻止,这意味着它只有在建立连接后才返回。因此,开始记录的代码:
camera.start_recording(connection, format='h264', ... )

仅在建立连接后才执行(在accept()返回之后)。

为了完成您想要的,您将需要使用线程。在一个线程中,开始本地录制视频,即
camera.start_recording(completed_video + '{}.h264'.format( 
dt.datetime.now().strftime('%Y%m%d%H%M%S') ), bitrate=4500000)

在另一个线程中,接受传入连接,并在连接调用时:
camera.start_recording(connection, format='h264', splitter_port=2, 
resize=(640,360))

As it stands I can connect to it no problem, but then I'm unable to reconnect to it.



如果希望能够无限期地连接,断开连接和重新连接,则需要在一个循环中(在其自己的线程中)接受连接。也许是这样的:
while(True):
connection = server_socket.accept()[0].makefile('wb')
camera.start_recording(connection, format='h264', splitter_port=2,
resize=(640,360))
camera.wait_recording(7*60*60) #assuming this records for 7 hours?

I'm currently trying to program and learn python, while making a project with the raspberry pi zero w.



我不知道您是一般编程还是Python新手。在前一种情况下,一开始处理网络编程概念和线程可能会有点挑战。但是,必须使用/理解它们才能实现所需的功能。

关于python - 断开连接后在python中重新连接到PiCamera流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47041267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com