Hello ,
I will post u the code for converting movie to frames..
but u must change the file path at live 171 and put it with the file u want to extract it to movie..
import java.awt.*;
import javax.media.*;
import javax.media.control.TrackControl;
import javax.media.Format;
import javax.media.format.*;
import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.awt.image.*;
import java.util.*;
import javax.media.util.*;
/**
* Sample program to access individual video frames by using a
* "pass-thru" codec. The codec is inserted into the data flow
* path. As data pass through this codec, a callback is invoked
* for each frame of video data.
*/
public class FrameAccess implements ControllerListener {
Processor p;
Object waitSync = new Object();
boolean stateTransitionOK = true;
public boolean alreadyPrnt = false;
/**
* Given a media locator, create a processor and use that processor
* as a player to playback the media.
*
* During the processor's Configured state, two "pass-thru" codecs,
* PreAccessCodec and PostAccessCodec, are set on the video track.
* These codecs are used to get access to individual video frames
* of the media.
*
* Much of the code is just standard code to present media in JMF.
*/
public boolean open(MediaLocator ml) {
try {
p = Manager.createProcessor(ml);
} catch (Exception e) {
System.err.println(
"Failed to create a processor from the given url: " + e);
return false;
}
p.addControllerListener(this);
// Put the Processor into configured state.
p.configure();
if (!waitForState(Processor.Configured)) {
System.err.println("Failed to configure the processor.");
return false;
}
// So I can use it as a player.
p.setContentDescriptor(null);
// Obtain the track controls.
TrackControl tc[] = p.getTrackControls();
if (tc == null) {
System.err.println(
"Failed to obtain track controls from the processor.");
return false;
}
// Search for the track control for the video track.
TrackControl videoTrack = null;
for (int i = 0; i < tc.length; i++) {
if (tc[i].getFormat() instanceof VideoFormat) videoTrack = tc[i];
elsetc[i].setEnabled(false);
}
if (videoTrack == null) {
System.err.println("The input media does not contain a video track.");
return false;
}
String videoFormat = videoTrack.getFormat().toString();
Dimension videoSize = parseVideoSize(videoFormat);
System.err.println("Video format: " + videoFormat);
// Instantiate and set the frame access codec to the data flow path.
try {
Codec codec[] = { new PostAccessCodec(videoSize)};
videoTrack.setCodecChain(codec);
} catch (UnsupportedPlugInException e) {
System.err.println("The process does not support effects.");
}
// Realize the processor.
p.prefetch();
if (!waitForState(Processor.Prefetched)) {
System.err.println("Failed to realise the processor.");
return false;
}
p.start();
return true;
}
/**parse the size of the video from the string videoformat*/
public Dimension parseVideoSize(String videoSize){
int x=300, y=200;
StringTokenizer strtok = new StringTokenizer(videoSize, ", ");
strtok.nextToken();
String size = strtok.nextToken();
StringTokenizer sizeStrtok = new StringTokenizer(size, "x");
try{
x = Integer.parseInt(sizeStrtok.nextToken());
y = Integer.parseInt(sizeStrtok.nextToken());
} catch (NumberFormatException e){
System.out.println("unable to find video size, assuming default of 300x200");
}
System.out.println("Image width = " + String.valueOf(x) +"\nImage height = "+ String.valueOf(y));
return new Dimension(x, y);
}
/**
* Block until the processor has transitioned to the given state.
* Return false if the transition failed.
*/
boolean waitForState(int state) {
synchronized (waitSync) {
try {
while (p.getState() != state && stateTransitionOK)
waitSync.wait();
} catch (Exception e) {
}
}
return stateTransitionOK;
}
/**
* 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.close();
System.exit(0);
}
}
/**
* Main program
*/
public static void main(String[] args) {
//if (args.length == 0) {
//prUsage();
//System.exit(0);
//
//}
//System.out.print("masoud");
String url = new String("file:E:\\1.mov");
if (url.indexOf(":") < 0) {
prUsage();
System.exit(0);
}
MediaLocator ml;
if ((ml = new MediaLocator(url)) == null) {
System.err.println("Cannot build media locator from: " + url);
System.exit(0);
}
FrameAccess fa = new FrameAccess();
if (!fa.open(ml))
System.exit(0);
}
static void prUsage() {
System.err.println("Usage: java FrameAccess <url>");
}
/*********************************************************
* Inner class.
*
* A pass-through codec to access to individual frames.
*********************************************************/
public class PreAccessCodec implements Codec {
/**
* Callback to access individual video frames.
*/
void accessFrame(Buffer frame) {
// For demo, we'll just print out the frame #, time &
// data length.
long t = (long) (frame.getTimeStamp() / 10000000f);
System.err.println(
"Pre: frame #: "
+ frame.getSequenceNumber()
+ ", time: "
+ ((float) t) / 100f
+ ", len: "
+ frame.getLength());
}
/**
* The code for a pass through codec.
*/
// We'll advertize as supporting all video formats.
protected Format supportedIns[] = new Format[] { new VideoFormat(null)};
// We'll advertize as supporting all video formats.
protected Format supportedOuts[] = new Format[] { new VideoFormat(null)};
Format input = null, output = null;
public String getName() {
return "Pre-Access Codec";
}
//these dont do anything
public void open() {}
public void close() {}
public void reset() {}
public Format[] getSupportedInputFormats() {
return supportedIns;
}
public Format[] getSupportedOutputFormats(Format in) {
if (in == null)
return supportedOuts;
else {
// If an input format is given, we use that input format
// as the output since we are not modifying the bit stream
// at all.
Format outs[] = new Format[1];
outs[0] = in;
return outs;
}
}
public Format setInputFormat(Format format) {
input = format;
return input;
}
public Format setOutputFormat(Format format) {
output = format;
return output;
}
public int process(Buffer in, Buffer out) {
// This is the "Callback" to access individual frames.
accessFrame(in);
// Swap the data between the input & output.
Object data = in.getData();
in.setData(out.getData());
out.setData(data);
// Copy the input attributes to the output
out.setFlags(Buffer.FLAG_NO_SYNC);
out.setFormat(in.getFormat());
out.setLength(in.getLength());
out.setOffset(in.getOffset());
return BUFFER_PROCESSED_OK;
}
public Object[] getControls() {
return new Object[0];
}
public Object getControl(String type) {
return null;
}
}
public class PostAccessCodec extends PreAccessCodec {
// We'll advertize as supporting all video formats.
public PostAccessCodec(Dimension size) {
supportedIns = new Format[] { new RGBFormat()};
this.size = size;
}
/**
* Callback to access individual video frames.
*/
void accessFrame(Buffer frame) {
// For demo, we'll just print out the frame #, time &
// data length.
if (!alreadyPrnt) {
BufferToImage stopBuffer = new BufferToImage((VideoFormat) frame.getFormat());
Image stopImage = stopBuffer.createImage(frame);
try {
BufferedImage outImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics og = outImage.getGraphics();
og.drawImage(stopImage, 0, 0, size.width, size.height, null);
//prepareImage(outImage,rheight,rheight, null);
Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
//Once an ImageWriter has been obtained, its destination must be set to an ImageOutputStream:
File f = new File(frame.getSequenceNumber() + ".jpg");
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
//Finally, the image may be written to the output stream:
//BufferedImage bi;
//writer.write(imagebi);
writer.write(outImage);
ios.close();
} catch (IOException e) {
System.out.println("Error :" + e);
}
}
//alreadyPrnt = true;
long t = (long) (frame.getTimeStamp() / 10000000f);
System.err.println(
"Post: frame #: "
+ frame.getSequenceNumber()
+ ", time: "
+ ((float) t) / 100f
+ ", len: "
+ frame.getLength());
}
public String getName() {
return "Post-Access Codec";
}
private Dimension size;
}
}
Hi. Do you know how to do just two more things?
Write them as PNG files and only grab 3 or 4 selected frames instead of the whole clip.
Thanks in advance.
I have gone through the forum and haven't found any ideas on PNG formats. Found some threads to grab frames using FrameGrabbingControl but ran into problems when i try to grab more than one.
HI there ,
I've cuted and paste the code and I got this error :
Video format: JPEG, 160x120, FrameRate=12.0, Length=3574
The input format is not compatible with the given codec plugin: FrameAccess$PostAccessCodec@b162d5
Failed to realize: com.sun.media.ProcessEngine@1a7bf11
Cannot build a flow graph with the customized options:
Unable to add customed codecs:
FrameAccess$PreAccessCodec@15f5897
FrameAccess$PostAccessCodec@b162d5
Error: Unable to realize com.sun.media.ProcessEngine@1a7bf11
Failed to realize the processor.
I'm using a linux with kernel 2.6, jdk 1.5 and jmf2.1 ( alljava ).
Thnx
Thiago
> Program is unable to detect the end of media..
That is odd. It registers for EndOfMediaEvents.
> And what about if i want images in different sizes
Resizing images is something that can be done
within the core J2SE (1.4+) classes. Here is an example.
http://schmidt.devlib.org/java/save-jpeg-thumbnail.html
> regarding resizing actually that program is
> throwing errors on some video sizes..
The source I linked to deals with images, not videos,
so I cannot understand quite what you are talking
about.
The idea is to run the 'extract frames from video'
application, then (separately) run the application
that resizes images.
If you are having trouble with extracting the frames,
take it up here, but please be more clear about what
is going wrong - input(s) and outputs, complete with
stacktraces, if they appear.
If you are having trouble resizing the images, I
suggest you take it up on an appropriate forum
(which the JMF forum is not).
Hi Andrew,
I think i am pretty clear about my question but any ways i will try to explain in more details.
package test;
import java.awt.*;
import javax.media.*;
import javax.media.control.TrackControl;
import javax.media.Format;
import javax.media.format.*;
import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.awt.image.*;
import java.util.*;
import java.util.zip.ZipFile;
import javax.media.util.*;
/**
* Sample program to access individual video frames by using a
* "pass-thru" codec. The codec is inserted into the data flow
* path. As data pass through this codec, a callback is invoked
* for each frame of video data.
*/
public class FrameAccess implements ControllerListener {
Processor p;
Object waitSync = new Object();
boolean stateTransitionOK = true;
public boolean alreadyPrnt = false;
/**
* Given a media locator, create a processor and use that processor
* as a player to playback the media.
*
* During the processor's Configured state, two "pass-thru" codecs,
* PreAccessCodec and PostAccessCodec, are set on the video track.
* These codecs are used to get access to individual video frames
* of the media.
*
* Much of the code is just standard code to present media in JMF.
*/
public boolean open(MediaLocator ml) {
try {
p = Manager.createProcessor(ml);
}
catch (Exception e)
{
System.err.println(
"Failed to create a processor from the given url: " + e);
return false;
}
p.addControllerListener(this);
// Put the Processor into configured state.
p.configure();
if (!waitForState(Processor.Configured))
{
System.err.println("Failed to configure the processor.");
return false;
}
// So I can use it as a player.
p.setContentDescriptor(null);
// Obtain the track controls.
TrackControl tc[] = p.getTrackControls();
if (tc == null)
{
System.err.println(
"Failed to obtain track controls from the processor.");
return false;
}
// Search for the track control for the video track.
TrackControl videoTrack = null;
for (int i = 0; i < tc.length; i++)
{
if (tc.getFormat() instanceof VideoFormat) videoTrack = tc;
elsetc.setEnabled(false);
}
if (videoTrack == null)
{
System.err.println("The input media does not contain a video track.");
return false;
}
String videoFormat = videoTrack.getFormat().toString();
Dimension videoSize = parseVideoSize(videoFormat);
System.err.println("Video format: " + videoFormat);
// Instantiate and set the frame access codec to the data flow path.
try
{
Codec codec[] = { new PostAccessCodec(videoSize)};
videoTrack.setCodecChain(codec);
}
catch (UnsupportedPlugInException e)
{
System.err.println("The process does not support effects.");
}
// Realize the processor.
p.prefetch();
if (!waitForState(Processor.Prefetched))
{
System.err.println("Failed to realise the processor.");
return false;
}
p.start();
return true;
}
/**parse the size of the video from the string videoformat*/
public Dimension parseVideoSize(String videoSize){
int x=300, y=200;
StringTokenizer strtok = new StringTokenizer(videoSize, ", ");
strtok.nextToken();
String size = strtok.nextToken();
StringTokenizer sizeStrtok = new StringTokenizer(size, "x");
try{
x = Integer.parseInt(sizeStrtok.nextToken());
y = Integer.parseInt(sizeStrtok.nextToken());
} catch (NumberFormatException e){
System.out.println("unable to find video size, assuming default of 300x200");
}
System.out.println("Image width = " + String.valueOf(x) +"\nImage height = "+ String.valueOf(y));
return new Dimension(x, y);
}
/**
* Block until the processor has transitioned to the given state.
* Return false if the transition failed.
*/
boolean waitForState(int state) {
synchronized (waitSync) {
try {
while (p.getState() != state && stateTransitionOK)
waitSync.wait();
} catch (Exception e) {
}
}
return stateTransitionOK;
}
/**
* 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.close();
System.out.println("Detected end of media");
System.exit(0);
}
}
/**
* Main program
*/
/*public ZipFile extractFrame(File f)
{
}*/
public static void main(String[] args)
{
//if (args.length == 0) {
//prUsage();
//System.exit(0);
//
//}
//System.out.print("masoud");
String url = new String("file:C:\\frame\\kidplaying.mpg");
if (url.indexOf(":") < 0) {
prUsage();
}
MediaLocator ml;
if ((ml = new MediaLocator(url)) == null)
{
System.err.println("Cannot build media locator from: " + url);
System.exit(0);
}
FrameAccess fa = new FrameAccess();
if (!fa.open(ml))
System.exit(0);
}
static void prUsage()
{
System.err.println("Usage: java FrameAccess <url>");
}
/*********************************************************
* Inner class.
*
* A pass-through codec to access to individual frames.
*********************************************************/
public class PreAccessCodec implements Codec
{
/**
* Callback to access individual video frames.
*/
void accessFrame(Buffer frame)
{
// For demo, we'll just print out the frame #, time &
// data length.
long t = (long) (frame.getTimeStamp() / 10000000f);
System.err.println("Pre: frame #: "+ frame.getSequenceNumber()
+ ", time: "
+ ((float) t) / 100f
+ ", len: "
+ frame.getLength());
}
/**
* The code for a pass through codec.
*/
// We'll advertize as supporting all video formats.
protected Format supportedIns[] = new Format[] { new VideoFormat(null)};
// We'll advertize as supporting all video formats.
protected Format supportedOuts[] = new Format[] { new VideoFormat(null)};
Format input = null, output = null;
public String getName() {
return "Pre-Access Codec";
}
//these dont do anything
public void open() {}
public void close() {}
public void reset() {}
public Format[] getSupportedInputFormats() {
return supportedIns;
}
public Format[] getSupportedOutputFormats(Format in) {
if (in == null)
return supportedOuts;
else {
// If an input format is given, we use that input format
// as the output since we are not modifying the bit stream
// at all.
Format outs[] = new Format[1];
outs[0] = in;
return outs;
}
}
public Format setInputFormat(Format format) {
input = format;
return input;
}
public Format setOutputFormat(Format format) {
output = format;
return output;
}
public int process(Buffer in, Buffer out) {
// This is the "Callback" to access individual frames.
accessFrame(in);
// Swap the data between the input & output.
Object data = in.getData();
in.setData(out.getData());
out.setData(data);
// Copy the input attributes to the output
out.setFlags(Buffer.FLAG_NO_SYNC);
out.setFormat(in.getFormat());
out.setLength(in.getLength());
out.setOffset(in.getOffset());
return BUFFER_PROCESSED_OK;
}
public Object[] getControls() {
return new Object[0];
}
public Object getControl(String type) {
return null;
}
}
public class PostAccessCodec extends PreAccessCodec {
// We'll advertize as supporting all video formats.
public PostAccessCodec(Dimension size) {
supportedIns = new Format[] { new RGBFormat()};
this.size = size;
}
/**
* Callback to access individual video frames.
*/
void accessFrame(Buffer frame) {
// For demo, we'll just print out the frame #, time &
// data length.
if (!alreadyPrnt)
{
BufferToImage stopBuffer = new BufferToImage((VideoFormat) frame.getFormat());
Image stopImage = stopBuffer.createImage(frame);
try
{
BufferedImage outImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics og = outImage.getGraphics();
og.drawImage(stopImage, 0, 0, size.width, size.height, null);
//prepareImage(outImage,rheight,rheight, null);
Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
//Once an ImageWriter has been obtained, its destination must be set to an ImageOutputStream:
//This is saved for every 1000th frame
if((frame.getSequenceNumber()%200)==0)
{
File f = new File("c:\\frame\\"+frame.getSequenceNumber() + ".jpg");
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
//Finally, the image may be written to the output stream:
//BufferedImage bi;
//writer.write(imagebi);
writer.write(outImage);
ios.close();
}
}
catch (IOException e)
{
System.out.println("Error :" + e);
}
}
//alreadyPrnt = true;
long t = (long) (frame.getTimeStamp() / 10000000f);
System.err.println(
"Post: frame #: "
+ frame.getSequenceNumber()
+ ", time: "
+ ((float) t) / 100f
+ ", len: "
+ frame.getLength());
}
public String getName()
{
return "Post-Access Codec";
}
private Dimension size;
}
}
This is my sample code which i am trying to run.
1. Now when i run this code
It goes in to infinite loop whitout giving any errors
(It never terminates)
If you could explain me why this kind of behaviour happening that will be great
Thanks in advance..
Hi,
I got error 500 Internal Server Error when i send a text message from jain-sip client, Client send the Text message to server but server return the above error.
There is no Text Message Handler Code in Server Proxy Class,
Also when i try to start Voice Connversation, Client sent a INVITE request to server but server return 500 Server Intnal Error. also there is no INVITE handler code in Server Proxy Class.
I am using jain-sip-presence-proxy SIP server and jain-sip-applet-phone.
please help me .......................................Its Urgent
why server could not handle the text or voice messages.
How i make the text and voice conversation.
with regards