Accurate Video Time Delay
in
Core Library Questions
•
2 years ago
Hi all,
I am new to processing and have been scanning the forum old and new looking to gain some help for my problem.
I am trying to use processing to slow video capture from a camera for a period of around 500ms to about five seconds. I have found a previous piece of code off an older thread that I have been trying out which was designed to slow video down by several seconds. The problem is when I alter the timing parameters down to say between 750ms to 3 seconds the delay is erratic at best and accuracy is poor. As I can alter the capture rate to say 60fps then it seems the most sensible route to me to follow is to get the buffer to automatically output when its full. (ie you require 750ms delay, 60fps capture rate then if the buffer sent video to output when it overflows at say 45 frames then that would give me a delay of 750ms - 90 frames to give 1.5secs and so on) but I'm not having much luck with it. I would appreciate any help available.
Here's the code I'm using at the moment. Original code by rrrufusss, modified by Henrik G. Sundt.
I am new to processing and have been scanning the forum old and new looking to gain some help for my problem.
I am trying to use processing to slow video capture from a camera for a period of around 500ms to about five seconds. I have found a previous piece of code off an older thread that I have been trying out which was designed to slow video down by several seconds. The problem is when I alter the timing parameters down to say between 750ms to 3 seconds the delay is erratic at best and accuracy is poor. As I can alter the capture rate to say 60fps then it seems the most sensible route to me to follow is to get the buffer to automatically output when its full. (ie you require 750ms delay, 60fps capture rate then if the buffer sent video to output when it overflows at say 45 frames then that would give me a delay of 750ms - 90 frames to give 1.5secs and so on) but I'm not having much luck with it. I would appreciate any help available.
Here's the code I'm using at the moment. Original code by rrrufusss, modified by Henrik G. Sundt.
import processing.video.*;
Capture maCam;
VideoBuffer monBuff;
int display_xsize = 1440; // display size
int display_ysize = 900;
int capture_xsize = 320; // capture size
int capture_ysize = 240;
int delay_time = 30; // delay in seconds
int capture_frames = 30; // capture frames per second
void setup() {
size(display_xsize,display_ysize, P3D);
// Warning: VideoBuffer must be initiated BEFORE capture- or movie-events start
monBuff = new VideoBuffer(delay_time*capture_frames, capture_xsize,capture_ysize);
maCam = new Capture(this, capture_xsize, capture_ysize, capture_frames);
}
void captureEvent(Capture maCam) {
maCam.read();
monBuff.addFrame( maCam );
}
void draw() {
PImage bufimg = monBuff.getFrame();
PImage tmpimg = createImage(bufimg.width,bufimg.height,RGB);
tmpimg.copy(bufimg,0,0,bufimg.width,bufimg.height,0,0,bufimg.width,bufimg.height);
tmpimg.resize(display_xsize,display_ysize);
image( tmpimg, 0, 0 );
}
class VideoBuffer
{
PImage[] buffer;
int inputFrame = 0;
int outputFrame = 0;
int frameWidth = 0;
int frameHeight = 0;
/*
parameters:
frames - the number of frames in the buffer (fps * duration)
width - the width of the video
height - the height of the video
*/
VideoBuffer( int frames, int width, int height )
{
buffer = new PImage[frames];
for(int i = 0; i < frames; i++)
{
this.buffer[i] = new PImage(width, height);
}
this.inputFrame = frames - 1;
this.outputFrame = 0;
this.frameWidth = width;
this.frameHeight = height;
}
// return the current "playback" frame.
PImage getFrame()
{
int frr;
if(this.outputFrame>=this.buffer.length)
frr = 0;
else
frr = this.outputFrame;
return this.buffer[frr];
}
// Add a new frame to the buffer.
void addFrame( PImage frame )
{
// copy the new frame into the buffer.
System.arraycopy(frame.pixels, 0, this.buffer[this.inputFrame].pixels, 0, this.frameWidth * this.frameHeight);
// advance the input and output indexes
this.inputFrame++;
this.outputFrame++;
// wrap the values..
if(this.inputFrame >= this.buffer.length)
{
this.inputFrame = 0;
}
if(this.outputFrame >= this.buffer.length)
{
this.outputFrame = 0;
}
}
}
1