Skip to content

Instantly share code, notes, and snippets.

@askvictor
Created November 4, 2020 08:44
Show Gist options
  • Save askvictor/7a544488e0b733bd2fd95fc1d88fce14 to your computer and use it in GitHub Desktop.
Save askvictor/7a544488e0b733bd2fd95fc1d88fce14 to your computer and use it in GitHub Desktop.
Flask script for a video ringbuffer for raspberry pi camera; HTTP trigger to dump the buffer contents (last x seconds) to disk.
import subprocess
from flask import Flask
import signal
from time import sleep
from datetime import datetime
vidproc = None
def start():
cmd = "raspivid -c -o out.h264 -s -t 120000 -fps 30 -rot 180 -w 1920 -h 1080 -b 15000000"
global vidproc
vidproc = subprocess.Popen(cmd.split())
def save_vid():
global vidproc
vidproc.send_signal(signal.SIGUSR1)
vidproc.wait()
cmd = "MP4Box -fps 30 -add out.h264 " + datetime.now().strftime("%Y%m%d-%H%M%S") + '.mp4'
subprocess.run(cmd.split())
start()
app = Flask(__name__)
@app.route('/')
def save():
save_vid()
return "Hello World!"
if __name__ == '__main__':
start()
app.run(host='0.0.0.0')
@askvictor
Copy link
Author

askvictor commented Nov 4, 2020

Requires MP4Box to be installed on the Pi, and flask be installed for python.
To use, point a browser or GET request to the Pi's IP address, port 5000 e.g. http://192.168.4.9:5000/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment