[BUG] Start and stop AV-Recording via Flask
Describe the bug
When the http://127.0.0.1:5000/start_recording and http://127.0.0.1:5000/stop_recording actions are repeated, the recording process fails to start or stop, and an error is generated.
`
To Reproduce
import time
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import FfmpegOutput
from flask import Flask, jsonify
app = Flask(__name__)
# Create a new Picamera2 instance
picam2 = Picamera2()
# Create the H264Encoder and FfmpegOutput objects
encoder = H264Encoder(10000000)
output = FfmpegOutput('test.mp4', audio=True)
# Define a route to start recording
@app.route('/start_recording')
def start_recording():
global picam2, encoder, output
video_config = picam2.create_video_configuration()
picam2.configure(video_config)
picam2.start_recording(encoder, output)
return jsonify({'status': 'recording started'})
# Define a route to stop recording
@app.route('/stop_recording')
def stop_recording():
global picam2
picam2.stop_recording()
return jsonify({'status': 'recording stopped'})
# Start the app
if __name__ == '__main__':
app.run(debug=True)
Expected behaviour This is a Python script that uses the Picamera2 library to start and stop recording video from a camera module connected to a Raspberry Pi. The script creates a Flask application with two routes: /start_recording and /stop_recording. When the /start_recording route is accessed, the script starts recording video using the camera module and saves the output to an H.264-encoded file named test.mp4. When the /stop_recording route is accessed, the script stops the recording and returns a JSON response indicating that the recording has stopped.
Console Output, Screenshots
* Restarting with stat
[16:27:00.584755507] [9707] INFO Camera camera_manager.cpp:299 libcamera v0.0.4+22-923f5d70
[16:27:00.621828334] [9719] INFO RPI raspberrypi.cpp:1476 Registered camera /base/soc/i2c0mux/i2c@1/imx477@1a to Unicam device /dev/media1 and ISP device /dev/media0
[16:27:00.623849377] [9707] INFO Camera camera.cpp:841 Pipeline handler in use by another process
Camera __init__ sequence did not complete.
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 242, in __init__
self._open_camera()
File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 441, in _open_camera
raise RuntimeError(f"camera.acquire() returned unexpected code: {acq_code}")
RuntimeError: camera.acquire() returned unexpected code: -16
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/alphi/Desktop/picamera2/examples/app.py", line 10, in <module>
picam2 = Picamera2()
File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 249, in __init__
raise RuntimeError("Camera __init__ sequence did not complete.")
RuntimeError: Camera __init__ sequence did not complete.
Hardware : Pi 4B (Bullseye) with HQ camera attached
Additional context Add any other context about the problem here.
I solve the Problem in this way:
from flask import Flask, redirect, url_for
import time
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import FfmpegOutput
# Create class
class AVideoRecorder:
def __init__(self):
self.picam2 = None
self.encoder = None
self.output = None
def update(self):
self.picam2 = Picamera2()
video_config = self.picam2.create_video_configuration()
self.picam2.configure(video_config)
self.encoder = H264Encoder(2000000)
self.output = FfmpegOutput('/media/alphi/INTENSO/test-' +time.strftime("%Y%m%d-%H%M%S")+'.mp4', audio=False )
def start_video(self):
self.picam2.start_recording(self.encoder, self.output)
def stop_video(self):
self.picam2.stop_recording()
self.picam2.close()
def close_video(self):
self.picam2.close()
# Create Flask app
app = Flask(__name__)
# Create an instance of AVideoRecorder
avRecorder = AVideoRecorder()
#Route of index
@app.route('/')
def index():
return "This is the beginning of a Flask Av Recording App with Picamera2"
# Route to start recording
@app.route('/start')
def start():
avRecorder.update()
avRecorder.start_video()
time.sleep(5)
avRecorder.stop_video()
return redirect(url_for('stop'))
# Route of stop Recording
@app.route('/stop')
def stop():
return "The video should now saved on the usb stick"
# Run the Flask app
if __name__ == '__main__':
app.run()