Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#ifndef max
#define max(a,b) (((a) (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#define BLOCK_SIZE 512
#define BUFFER_SIZE 64*1024*1024
int main(int argc, const char *argv[])
{
//printf("Debug");
int delay = 0;
if (argc != 1) {
delay = atoi( argv[1]);
}
int output = 0; //to be set to 1 when delay is reached
uint8_t *buffer= malloc(BUFFER_SIZE);
uint8_t *write = buffer;
uint8_t *read = buffer;
int n_read = BLOCK_SIZE;
int n_write = 0;
struct timeval tv;
gettimeofday(&tv,NULL);
struct timeval tv2;
while(1) {
if(!output){
gettimeofday(&tv2,NULL);
if(tv2.tv_sec - tv.tv_sec >= delay){
output = 1;
fprintf(stderr, "Required buffer size: %iKb\n", (int) (write - read)/1024);
}
}
n_read = min(BLOCK_SIZE,BUFFER_SIZE - (write-buffer));
//printf("Debug %i %ui %ui %ui",n_read, write, buffer, read );
size_t bytes = fread(write,sizeof(uint8_t), n_read, stdin);
//printf("Debug");
if(bytes==0){
sleep(1);
}else{
write = write+bytes;
if(write>=buffer+BUFFER_SIZE){
if(!output){
fprintf(stderr, "Buffer overflow\n");
exit(1);
}else{
write = buffer;
}
}
if(output){
n_write = min(bytes,buffer+BUFFER_SIZE-read);
fwrite(read, sizeof(uint8_t), n_write,stdout);
read+=n_write;
if(bytes >n_write){
n_write = bytes - n_write;
read = buffer;
fwrite(read, sizeof(uint8_t), n_write,stdout);
read+=n_write;
}
}
}
}
return 0;
}Code: Select all
#!/bin/bash
DELAY=$1
rm -f video.h264
mkfifo video.h264
/opt/vc/src/hello_pi/hello_video/hello_video.bin video.h264 &
raspivid -t 0 -w 1280 -h 720 -o - -fps 30 -n | ./timed_buffer.out $DELAY > video.h264
Code: Select all
raspivid -o - | ffmpeg -i - -map 0 -c copy -f segment -segment_time 5 -reset_timestamps 1 %02d.h264Don't delete the last video - always leave a couple on (ram)diskdqpi wrote:I have a video of someone walking by. I want the camera to start when motion is detected but I also want to have the person come walk into the video. Recording short videos and throwing away is not an option because what if I erase the last video and then motion happens. I need a continuous buffer or a delayed stream that I can record? Is this possible?