Oct-23-2023, 12:01 AM
Hi, I have a raspberry pi 3b with an arducam. I'm trying to stream to a network and record my stream to the pi at the same time. How do I do this?
I'm able to stream using this code:
I can comment out the line with picam2.start_recording(encoder1...) and I can record.
I can comment out the line with picam2.start_recording(encoder2...) and I can stream.
But not at the same time. What's wrong with this script?
I'm able to stream using this code:
#!/usr/bin/python3
# This is the same as mjpeg_server.py, but uses the h/w MJPEG encoder.
import io
import logging
import socketserver
from http import server
from threading import Condition
from picamera2 import Picamera2
from picamera2.encoders import MJPEGEncoder
from picamera2.encoders import H264Encoder
from picamera2.outputs import FileOutput, FfmpegOutput
PAGE = """\
<html>
<head>
<title>picamera2 MJPEG streaming demo</title>
</head>
<body>
<h1>Picamera2 MJPEG Streaming Demo 640 </h1>
<img src="stream.mjpg" width="640" height="480" />
<h1>Picamera2 MJPEG Streaming Demo 1280</h1>
<img src="stream.mjpg" width="1280" height="1080" />
</body>
</html>
"""
class StreamingOutput(io.BufferedIOBase):
def __init__(self):
self.frame = None
self.condition = Condition()
def write(self, buf):
with self.condition:
self.frame = buf
self.condition.notify_all()
class StreamingHandler(server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.send_response(301)
self.send_header('Location', '/index.html')
self.end_headers()
elif self.path == '/index.html':
content = PAGE.encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', len(content))
self.end_headers()
self.wfile.write(content)
elif self.path == '/stream.mjpg':
self.send_response(200)
self.send_header('Age', 0)
self.send_header('Cache-Control', 'no-cache, private')
self.send_header('Pragma', 'no-cache')
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
self.end_headers()
try:
while True:
with output.condition:
output.condition.wait()
frame = output.frame
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b'\r\n')
except Exception as e:
logging.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
else:
self.send_error(404)
self.end_headers()
class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
allow_reuse_address = True
daemon_threads = True
encoder1 = MJPEGEncoder(10000000)
encoder2 = H264Encoder(10000000)
picam2 = Picamera2()
picam2.configure(picam2.create_video_configuration(main={"size": (1280, 720)}))
output = StreamingOutput()
picam2.start_recording(encoder1, FileOutput(output))
picam2.start_recording(encoder2, 'test.mp4')
#picam2.start_and_record_video("test.mp4", duration=5)
try:
address = ('', 8000)
server = StreamingServer(address, StreamingHandler)
server.serve_forever()
finally:
picam2.stop_recording()With the above, script I get a runtime error: Camera already started.I can comment out the line with picam2.start_recording(encoder1...) and I can record.
I can comment out the line with picam2.start_recording(encoder2...) and I can stream.
But not at the same time. What's wrong with this script?
