This discussion is archived
0 Replies Latest reply: Nov 16, 2011 1:37 AM by 900492 RSS

Grab frames from video using JMF

900492 Newbie
Currently Being Moderated
I am making a project where i should grab every frame from one video stored in my computer.
I have found a lot of tutorials and examples about that, for example:
http://www.mokabyte.it/2004/10/videograbbing.htm (its an italian one but the code is understandable :)..) or http://www.daniweb.com/software-development/java/threads/173561 but i dont understand how to use these codec.

Btw i found out that the problem is when i create the image from the buffer:

buff = fgc.grabFrame();
if (buff==null){
System.out.println("buff null");
System.exit(0);
}
// converte buffer - image
BufferToImage bti = new BufferToImage((VideoFormat)buff.getFormat());
img = bti.createImage(buff);
if (img==null){
System.out.println("img null");
System.exit(0);
}

because the image is null.

I really dont get how it can work.

Does anyone can help me??





import java.awt.*;
import java.awt.event.*;
import javax.media.*;
import javax.media.format.VideoFormat;
import javax.media.control.FramePositioningControl;
import javax.media.protocol.DataSource;
import javax.media.control.FrameGrabbingControl;
import javax.media.util.*;
import java.io.*;
import java.lang.Object;
import java.awt.image.ImageObserver;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.Image;


public class VideoGrabbing extends Frame implements ControllerListener, ActionListener {

Player p;
FramePositioningControl fpc;
FrameGrabbingControl fgc;
Object waitSync = new Object();
boolean stateTransitionOK = true;
int totalFrames = FramePositioningControl.FRAME_UNKNOWN;
Image img;
int currentFrame;
Time currentTime;
static String urlVideo,urlFrames;
static int camp;
Buffer buff = new Buffer();
BufferedImage bi=null;
Panel controlPanel;
Button grabButton;
Button closeButton;

/**
* Creazione del Player che verrà utilizzato per il grabbing.
*/

public boolean open(DataSource ds) {

System.err.println("Creato player per video in formato " + ds.getContentType());

try {
p = Manager.createPlayer(ds);

} catch (Exception e) {
System.err.println("Creazione del player per il DataSource: " + e + " fallita!");
return false;
}

p.addControllerListener(this);

p.realize();
if (!waitForState(p.Realized)) {
System.err.println("Realizzazione del player fallita!");
return false;
}

// Provo a ottenere FramePositioningControl dal player.
fpc = (FramePositioningControl)p.getControl("javax.media.control.FramePositioningControl");
// Provo a ottenere FrameGrabbingControl dal player.
fgc = (FrameGrabbingControl)p.getControl("javax.media.control.FrameGrabbingControl");

if (fpc == null) {
System.err.println("Il player non supporta il " +"FramePositioningControl.");
return false;
}
if (fgc == null) {
System.err.println("Il player non supporta il " +"FrameGrabbingControl.");
return false;
}
Time duration = p.getDuration();

if (duration != Duration.DURATION_UNKNOWN) {
System.err.println("Durata del video: " + duration.getSeconds() + " secondi.");

totalFrames = fpc.mapTimeToFrame(duration);

if (totalFrames != FramePositioningControl.FRAME_UNKNOWN){
System.err.println("Numero totale di frames del video: " + totalFrames);
} else
System.err.println("Il FramePositiongControl non " +" supporta mapTimeToFrame.");

} else {
System.err.println("Durata del video sconosciuta!");
}

// Prefetch del player.
p.prefetch();
if (!waitForState(p.Prefetched)) {
System.err.println("Prefetch del player fallito.");
return false;
}

// Costruzione dell'interfaccia grafica.

setLayout(new BorderLayout());

controlPanel = new Panel();

grabButton = new Button("Grab");
closeButton = new Button("Close");

grabButton.addActionListener(this);
closeButton.addActionListener(this);

controlPanel.add(new Label 
(urlVideo.substring(5,urlVideo.length())));
controlPanel.add(grabButton);
controlPanel.add(closeButton);

Component vc;
if ((vc = p.getVisualComponent()) != null) {
add("Center", vc);
}

add("South", controlPanel);
setTitle("VideoGrabber");
setLocation(500,100);
setVisible(true);
return true;
}


public void addNotify() {
super.addNotify();
pack();
}


/**
* Aspetta finché il player non è transitato in un dato stato.
* Return false se la transizione è fallita.
*/
boolean waitForState(int state) {
synchronized (waitSync) {
try {
while (p.getState() < state && stateTransitionOK)
waitSync.wait();
} catch (Exception e) {}
}
return stateTransitionOK;
}


public void actionPerformed(ActionEvent ae) {
String command = ae.getActionCommand();
if (command.equals("Grab")){
int frameId=0;
for ( int i=0; i<=totalFrames; i=i+camp) {
frameId++;
// inizialmente mi posiziono sul frame 0 (istante 0)
if (i==0) 
fpc.skip(0);
// poi mi posiziono sui frame successivi, secondo il 
// campionamento
else
fpc.skip(camp);
// grabba il frame relativo
if (currentFrame != FramePositioningControl.FRAME_UNKNOWN) {
currentFrame = fpc.mapTimeToFrame(p.getMediaTime());
currentTime = fpc.mapFrameToTime(i);
}

buff = fgc.grabFrame();
if (buff==null){
System.out.println("buff null");
System.exit(0);
}
// converte buffer - image
BufferToImage bti = new BufferToImage((VideoFormat)buff.getFormat());
img = bti.createImage(buff);
//the image is always null
if (img==null){
System.out.println("img null");
System.exit(0);
}
String pathFrame = ("F:\\immagine"+frameId+".jpg");
//System.out.println(pathFrame);
File fileFrame = new File(pathFrame);

try {
System.err.println("Sto salvando il frame n." + currentFrame + " del video " +" Tempo: " + currentTime.getSeconds());
ImageSaver(img , fileFrame);
} catch (Exception e) {
System.err.println("Non riesco a salvare il " + "frame, forse non esiste " +"la cartella " + e);
}
} 
System.err.println("Numero di frames salvati:" + frameId);

} else if (command.equals("Close")) {
this.dispose();
System.exit(0);
}
}

/**
* Controller Listener.
*/
public void controllerUpdate(ControllerEvent evt) {

if (evt instanceof ConfigureCompleteEvent ||
evt instanceof RealizeCompleteEvent ||
evt instanceof PrefetchCompleteEvent) {
synchronized (waitSync) {
stateTransitionOK = true;
waitSync.notifyAll();
}
} else if (evt instanceof ResourceUnavailableEvent) {
synchronized (waitSync) {
stateTransitionOK = false;
waitSync.notifyAll();
}
} else if (evt instanceof EndOfMediaEvent) {
p.setMediaTime(new Time(0));
} else if (evt instanceof SizeChangeEvent) {
}
}

void this_windowClosing(WindowEvent e) {
this.dispose();
}

public void ImageSaver (Image img,File fileFrame) throws IOException {

try {
System.out.println("inizio almeno");
if (!(img instanceof BufferedImage)) {
ImageObserver io=new ImageObserver() {
public boolean imageUpdate(Image im, int info,int x, int y, int w, int h) {
return false;
}
};
bi = new BufferedImage(img.getWidth(io),img.getHeight(io),BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(img,0,0,io);
img=bi;
System.out.println("e qua");
} else
System.out.println("etìì");
bi=(BufferedImage)img;
ImageIO.write(bi, "JPG", fileFrame);
} catch (Exception e) {
throw new IOException("Salvataggio dell'immagine fallito: " + e);
}

}

/**
* Main program
*/
public static void main(String[] args) {


urlVideo = "file:f:\\prova\\why.mov";
//urlFrames = "f:\\prova";
camp = 15;
MediaLocator ml;
if ((ml = new MediaLocator(urlVideo)) == null) 
System.err.println("Impossibile costruire MediaLocator " + "dalla url: " + urlVideo);
DataSource ds = null;
// Crea un DataSource da un dato MediaLocator
try {
ds = Manager.createDataSource(ml);
} catch (Exception e) {
System.err.println("Impossibile creare Datasource da: " 
+ ml);
System.err.println("Eccezione: "+e);
System.exit(0);
}
VideoGrabbing VideoGrabbing = new VideoGrabbing();
if (!VideoGrabbing.open(ds))
System.exit(0);

}


}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points