You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use FfmpegOutput to output to a mp4 file directly instead of outputing to a h264 file, but it seems there is a bug that results in no file being created.
I made a very simple Flask app to illustrate the issue:
from flask import Flask, render_template, request, jsonify
from datetime import datetime
import os
from libcamera import controls
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import FfmpegOutput
app = Flask(__name__)
is_recording = False
@app.route("/")
def index():
return render_template("index.html")
@app.route("/start-record", methods=["POST"])
def start_record():
global is_recording
if is_recording:
message = "Already recording."
return jsonify({"status": "error", "message": message, "is_recording": is_recording})
is_recording = True
encoder = H264Encoder()
# This doesn't work, no .mp4 file is created
filename = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p") + ".mp4"
output = FfmpegOutput(filename)
print(f"Starting recording of file: {filename}")
picam2.start_recording(encoder, output)
# This works, a .h264 file is created
# filename = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p") + ".h264"
# picam2.start_recording(encoder, filename)
message = "Recording started."
return jsonify({"status": "success", "message": message, "is_recording": is_recording})
@app.route("/stop-record", methods=["POST"])
def stop_record():
global is_recording
if not is_recording:
message = "Not recording."
return jsonify({"status": "error", "message": message, "is_recording": is_recording})
is_recording = False
print(f"Stoping recording.")
picam2.stop_recording()
message = "Recording stopped."
return jsonify({"status": "success", "message": message, "is_recording": is_recording})
if __name__ == "__main__":
picam2 = Picamera2()
video_config = picam2.create_video_configuration(main={"size": (1920, 1080),"format":"YUV420"})
picam2.configure(video_config)
app.run(host='0.0.0.0', debug=False)
As the comment indicates, there is no .mp4 file created at all by this code, however, if I record the h264 file directly instead of using FfmpegOutput, it works great and the .h264 file is created without any issue.
What is even weirder, is that if I add a time.sleep(10) after the picam2.start_recording(encoder, output), I do have a 10 second mp4 file that is created, but the recording stops right after the sleep.
Hi, thanks for the question. I don't know anything about Flask, unfortunately, but that certainly sounds a bit strange.
Have you maybe tried checking out Picamera2 and putting some debug into the FfmpegOutput? Just a print statement would do. We could find out whether outputframe in there is being called as expected, or whether somehow something is preventing the external FFmpeg process from running.
Another thing to consider is that the most recent version of Picamera2 has a (slightly experimental) new output called the PyavOutput. It uses FFmpeg directly, rather than as a separate process, so it might behave differently. You can use it like this (getting the documentation update approved is still ongoing).
Hello
I am trying to use FfmpegOutput to output to a mp4 file directly instead of outputing to a h264 file, but it seems there is a bug that results in no file being created.
I made a very simple Flask app to illustrate the issue:
As the comment indicates, there is no .mp4 file created at all by this code, however, if I record the h264 file directly instead of using FfmpegOutput, it works great and the .h264 file is created without any issue.
What is even weirder, is that if I add a
time.sleep(10)
after thepicam2.start_recording(encoder, output)
, I do have a 10 second mp4 file that is created, but the recording stops right after the sleep.Is this behavior expected ?
Please find a .zip file with the code I used to reproduce this issue: flask-mp4-record-issue.zip
The text was updated successfully, but these errors were encountered: