Python Forum
How do I stream and record at the same time with arducam?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I stream and record at the same time with arducam?
#1
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:
#!/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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to process 12 million record pymach 4 1,945 Aug-01-2024, 07:58 AM
Last Post: Pedroski55
  Last record in file doesn't write to newline gonksoup 3 2,741 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  EEG stream data with mne and brainfolw PaulC 0 1,329 Aug-22-2023, 03:17 AM
Last Post: PaulC
  Create RTSP stream from camera? korenron 1 5,257 Jan-04-2022, 10:38 AM
Last Post: Larz60+
  Stream via socket with multiple clients principemestizo 0 3,701 Nov-01-2021, 06:25 PM
Last Post: principemestizo
  Decoding a serial stream AKGentile1963 7 15,730 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  Microphone stream manipulation Talking2442 0 5,510 Jan-07-2021, 07:36 PM
Last Post: Talking2442
  Only getting last record saved...Why Milfredo 10 8,524 Sep-10-2020, 03:00 AM
Last Post: Milfredo
  Best Video Quality And Stream Harshil 2 3,668 Aug-19-2020, 09:03 AM
Last Post: Harshil
  how can we record a video file from our program output (moving object) Zhaleh 0 2,726 Aug-03-2020, 02:47 PM
Last Post: Zhaleh

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.