sketch video catpure with ffmpeg and xuggle
in
Library and Tool Development
•
3 years ago
I found an open source video library for Windows, Mac, and Linux today called
Xuggle . Xuggle is basically a Java wrapper for
ffmpeg . I was able to successfully get it capturing video from a simple test sketch in mpeg4 fairly smoothly even up to 1024 by 768.Whatever automatic settings Xuggle applies look decent in this case although it seemed to drop green from the grays making the image appear a little red / violet. The only thing I set was frame rate.
To get it working (on Windows), I downloaded and installed Xuggle and copied the files in Xuggle/share/java/jars to a folder called xuggle in my Processing libraries folder and renamed xuggle-zuggler.jar to just xuggle.jar to match. Following is my test code:
To get it working (on Windows), I downloaded and installed Xuggle and copied the files in Xuggle/share/java/jars to a folder called xuggle in my Processing libraries folder and renamed xuggle-zuggler.jar to just xuggle.jar to match. Following is my test code:
import processing.opengl.*;
import com.xuggle.xuggler.*;
import com.xuggle.mediatool.*;
import java.awt. image.BufferedImage;
import java.awt. image.ImageObserver;
import java.util.concurrent.TimeUnit;
IMediaWriter imw;
IStreamCoder isc;
BufferedImage bgr;
int vidRate;
long sTime;
long fTime;
void setup() {
size(1024,768);
vidRate = 30;
ellipseMode( CENTER);
imw = ToolFactory.makeWriter(sketchPath( "output.mp4"));
imw.addVideoStream(0,0, ICodec.ID.CODEC_ID_MPEG4,
IRational.make(( double)vidRate), width, height);
isc = imw.getContainer().getStream(0).getStreamCoder();
bgr = new BufferedImage( width, height,
BufferedImage.TYPE_3BYTE_BGR);
sTime = fTime = System.nanoTime();
}
void draw() {
background( sin( frameCount/25f)*255);
ellipse( mouseX, mouseY, 100,100);
loadPixels();
long cTime = System.nanoTime()-fTime;
if(cTime >= ( double)1000/vidRate) {
// bgr.setRGB(0, 0, width, height, pixels, 0, width);
bgr.getGraphics().drawImage(g.getImage(), 0, 0,
new ImageObserver() {
public boolean imageUpdate(Image i, int a, int b, int c, int d, int e){ return true;} });
imw.encodeVideo(0, bgr, System.nanoTime()-sTime,
TimeUnit.NANOSECONDS);
fTime = System.nanoTime();
}
}
void mouseClicked() {
imw. close();
exit();
}
import com.xuggle.xuggler.*;
import com.xuggle.mediatool.*;
import java.awt. image.BufferedImage;
import java.awt. image.ImageObserver;
import java.util.concurrent.TimeUnit;
IMediaWriter imw;
IStreamCoder isc;
BufferedImage bgr;
int vidRate;
long sTime;
long fTime;
void setup() {
size(1024,768);
vidRate = 30;
ellipseMode( CENTER);
imw = ToolFactory.makeWriter(sketchPath( "output.mp4"));
imw.addVideoStream(0,0, ICodec.ID.CODEC_ID_MPEG4,
IRational.make(( double)vidRate), width, height);
isc = imw.getContainer().getStream(0).getStreamCoder();
bgr = new BufferedImage( width, height,
BufferedImage.TYPE_3BYTE_BGR);
sTime = fTime = System.nanoTime();
}
void draw() {
background( sin( frameCount/25f)*255);
ellipse( mouseX, mouseY, 100,100);
loadPixels();
long cTime = System.nanoTime()-fTime;
if(cTime >= ( double)1000/vidRate) {
// bgr.setRGB(0, 0, width, height, pixels, 0, width);
bgr.getGraphics().drawImage(g.getImage(), 0, 0,
new ImageObserver() {
public boolean imageUpdate(Image i, int a, int b, int c, int d, int e){ return true;} });
imw.encodeVideo(0, bgr, System.nanoTime()-sTime,
TimeUnit.NANOSECONDS);
fTime = System.nanoTime();
}
}
void mouseClicked() {
imw. close();
exit();
}
If you try this make sure to click to end the video so the file gets finished properly. I would like to use this to develop a tool that would allow video to be captured from any sketch without adding extra code. I hear there may be a new core video library coming , though. I also had trouble with GSVideo for this purpose.
1