Dobry den, mohli by ste mi pomoct s touto castou kodu:
class VideoPlayer{
private PlayBin player;
private Element videosink;
private Bus bus;
public VideoPlayer(final Canvas can){
Gst.init();
this.player = new PlayBin("Player",new File("/path/to/videofile.avi").toURI());
this.videosink = ElementFactory.make("xvimagesink", "dest");
this.player.setVideoSink(videosink);
this.bus = player.getBus();
//Sem este treba dopisat kod pre prehravanie v Canvase pod Win
bus.setSyncHandler(new BusSyncHandler() {
public BusSyncReply syncMessage(Message msg) {
Structure s = msg.getStructure();
if (s == null || !s.hasName("prepare-xwindow-id"))
return BusSyncReply.PASS;
XOverlay.wrap(videosink).setWindowID(can);
return BusSyncReply.DROP;
}
});
player.play();
}
}
class VideoFrameCatcher{
private BufferedImage currentImage = null;
private PlayBin2 player = null;
Semaphore sem;
public VideoFrameCatcher() {
Gst.init();
player = new PlayBin2("VideoFrameCatcher");
player.setAudioSink(null);
sem = new Semaphore(0);
RGBDataSink.Listener listener1 = new RGBDataSink.Listener() {
public void rgbFrame(int w, int h, IntBuffer rgbPixels) {
System.out.println(" -> Got a frame:");
currentImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
currentImage.setRGB(0, 0, w, h, rgbPixels.array(), 0, w);
sem.release();
}
};
RGBDataSink videoSink = new RGBDataSink("rgb", listener1);
player.setVideoSink(videoSink);
}
public void setInputVideoFile(File inputVideoFile) {
player.setInputFile(inputVideoFile);
}
public BufferedImage CatchFrameAt(long time) throws InterruptedException{
player.pause();
player.getState();
player.seek(time, TimeUnit.MILLISECONDS);
player.play();
sem.acquire();
player.pause();
return this.currentImage;
}
}
class VideoController implements Runnable{
ImageStack imgS;
long step=60;
VideoFrameCatcher vidCatch;
private Thread vid;
public VideoController(ImageStack imgS){
this.vid = new Thread(this, "VideoThread");
this.imgS = imgS;
this.vid.start();
}
public VideoController(ImageStack imgS, long step){
this.vid = new Thread(this, "VideoThread");
this.imgS = imgS;
this.step = step;
this.vid.start();
}
public void run(){
this.vidCatch = new VideoFrameCatcher();
File videoFile = new File("/path/to/videofile.avi");
this.vidCatch.setInputVideoFile(videoFile);
int pos=0;
//Namiesto pos<12 tu bude nieco ako bin.EOS()
while(pos<12){
try {
imgS.putImageToArray(vidCatch.CatchFrameAt(pos*step), pos*step);
} catch (InterruptedException ex) {
Logger.getLogger(VideoController.class.getName()).log(Level.SEVERE, null, ex);
}
pos++;
}
}
}
Ako vidite moj kod je dost neohrabany. Potreboval by som ho zapisat trocha elegantnejsie. V tomto programe potrebujem aby mi bol schopny prehrat cele video v Canvase, co robi class VideoPlayer a aby mi dokazal zachytavat obrazky v urcitych casovych intervaloch toho isteho videa. Samozrejme nie sucasne s prehravanim videa, kazda cast bude cakat na Event ktory ju vyvola. Za Vase reakcie vopred dakujem.