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
  Last record in file doesn't write to newline gonksoup 3 455 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Microphone stream manipulation Talking2442 1 2,753 Nov-19-2023, 02:08 PM
Last Post: palumanic
  EEG stream data with mne and brainfolw PaulC 0 503 Aug-22-2023, 03:17 AM
Last Post: PaulC
  Decoding a serial stream AKGentile1963 7 8,634 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  Only getting last record saved...Why Milfredo 10 4,399 Sep-10-2020, 03:00 AM
Last Post: Milfredo
  Best Video Quality And Stream Harshil 2 2,265 Aug-19-2020, 09:03 AM
Last Post: Harshil
  how can we record a video file from our program output (moving object) Zhaleh 0 1,821 Aug-03-2020, 02:47 PM
Last Post: Zhaleh
  Get the record number of a dBASE dbf file DarkCoder2020 0 1,784 Jun-16-2020, 05:11 PM
Last Post: DarkCoder2020
  Compare 5 variables in a record with an excel sheet!! SEED 1 1,823 Apr-20-2020, 11:10 PM
Last Post: michael1789
  How to insert record into MySQL using Phython abhay_kala 1 2,274 Dec-06-2019, 04:34 PM
Last Post: abhay_kala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020