Re: how to capture a frame from a video file and save it as a jpeg

package com.snn.multimedia;
* @(#)FrameAccess.java     1.5 01/03/13
* Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
import java.util.Date;
import java.util.Arrays;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.ImageWriter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import javax.media.*;
import javax.media.control.FramePositioningControl;
import javax.media.control.TrackControl;
import javax.media.Format;
import javax.media.format.*;
import javax.media.util.BufferToImage;
import javax.media.util.ImageToBuffer;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.sun.image.codec.jpeg.*;
* 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 extends java.awt.Frame implements ControllerListener
Processor p;
Object waitSync = new Object();
boolean stateTransitionOK = true;
* 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(p.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[i];
break;
if (videoTrack == null)
System.err.println("The input media does not contain a video track.");
return false;
VideoFormat currentFormat = (VideoFormat)videoTrack.getFormat();
System.err.println("Video format: " + videoTrack.getFormat() );
videoTrack.setFormat(new VideoFormat("RGB", currentFormat.getSize(), currentFormat.getMaxDataLength(), currentFormat.getDataType(), currentFormat.getFrameRate()));
// Instantiate and set the frame access codec to the data flow path.
try
// Try to retrieve a FramePositioningControl from the player.
FramePositioningControl fpc = (FramePositioningControl) p.getControl("javax.media.control.FramePositioningControl");
if (fpc == null)
System.err.println("The player does not support FramePositioningControl.");
System.err.println("There's no reason to go on for the purpose of this demo.");
return false;
Time duration = p.getStopTime();
long totalFrames = 0;
if (duration != Duration.DURATION_UNKNOWN)
System.err.println("Movie duration: " + duration.getSeconds());
totalFrames = fpc.mapTimeToFrame(duration);
if (totalFrames != FramePositioningControl.FRAME_UNKNOWN)
System.err.println("Total # of video frames in the movies: "
+ totalFrames);
} else
System.err.println("The FramePositiongControl does not support mapTimeToFrame.");
} else
System.err.println("Movie duration: unknown");
long[] frames;
if (totalFrames > 0L)
double intervalDouble = Math.floor(totalFrames / 5.0);
long interval = new Double(intervalDouble).longValue();
frames = new long[5];
frames[0] = 1;
frames[1] = frames[0] + interval;
frames[2] = frames[1] + interval;
frames[3] = frames[2] + interval;
frames[4] = frames[3] + interval;
} else
frames = new long[1];
frames[0] = 1;
// Codec codec[] = { new PreAccessCodec(), new PostAccessCodec()};
Codec codec[] = { new OverlayCodec(frames)};
videoTrack.setCodecChain(codec);
catch (UnsupportedPlugInException e)
System.err.println("The process does not support effects.");
// Realize the processor.
p.prefetch();
if (!waitForState(p.Prefetched))
System.err.println("Failed to realize the processor.");
return false;
// Display the visual & control component if there's one.
setLayout(new BorderLayout());
Component cc;
Component vc;
if ((vc = p.getVisualComponent()) != null)
add("Center", vc);
if ((cc = p.getControlPanelComponent()) != null)
add("South", cc);
// Start the processor.
p.start();
setVisible(true);
return true;
public void addNotify()
super.addNotify();
pack();
* 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);
String url = args[0];
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";
// No op.
public void open()
// No op.
public void close()
// No op.
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.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 OverlayCodec extends PreAccessCodec
long[] myFrames;
BufferedImage work;
byte[] workData;
int width;
int height;
int dataLen;
RGBFormat supportedRGB = new RGBFormat(null, // size
Format.NOT_SPECIFIED, // maxDataLength
Format.byteArray, // dataType
Format.NOT_SPECIFIED, // frameRate
24, // bitsPerPixel
3, 2, 1, // red/green/blue masks
3, // pixelStride
Format.NOT_SPECIFIED, // lineStride
Format.FALSE, // flipped
Format.NOT_SPECIFIED); // endian
public OverlayCodec(long[] frames)
// force specific input format
supportedIns = new Format[] {
supportedRGB};
myFrames = new long[frames.length];
System.arraycopy(frames, 0, myFrames, 0, frames.length);
public String getName()
return "Capture Codec";
public Format setInputFormat(Format format)
if ((format != null) && (format instanceof RGBFormat)
&& format.matches(supportedRGB))
// set up working image if valid type
// (it should be since we insisted!)
Dimension size = ((RGBFormat) format).getSize();
width = size.width;
height = size.height;
dataLen = width * height * 3;
if ((dataLen > 0)
&& ((work == null) || (work.getWidth() != width)
|| (work.getHeight() != height)))
// working image - same 3-byte format as buffer
work = new BufferedImage(width, height,
BufferedImage.TYPE_3BYTE_BGR);
// reference to pixel data
workData = ((DataBufferByte) work.getRaster().getDataBuffer()).getData();
return format;
* Callback to access individual video frames.
void accessFrame(Buffer in)
try
if (Arrays.binarySearch(myFrames, in.getSequenceNumber()) >= 0)
BufferToImage stopBuffer = new BufferToImage((VideoFormat) in.getFormat());
Image stopImage = stopBuffer.createImage(in);
BufferedImage outImage = new BufferedImage(140, 96,
BufferedImage.TYPE_INT_RGB);
Graphics og = outImage.getGraphics();
og.drawImage(stopImage, 0, 0, 140, 96, null);
FileOutputStream fout = new FileOutputStream("image"
+ in.getSequenceNumber() + ".jpg");
writeImage(outImage, fout);
catch (Exception e)
e.printStackTrace();
public int process(Buffer in, Buffer out)
try
accessFrame(in);
BufferToImage stopBuffer = new BufferToImage((VideoFormat) in.getFormat());
Image stopImage = stopBuffer.createImage(in);
ImageToBuffer outImagebuffer = new ImageToBuffer();
out = outImagebuffer.createBuffer(stopImage, p.getRate());
// Swap the data between the input & output.
in.copy(out, true);
catch (Exception e)
e.printStackTrace();
return BUFFER_PROCESSED_OK;
void writeImage(BufferedImage outImage, OutputStream os) throws Exception
Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
writer.write(outImage);
ios.close();

Hi,
I have a jpeg movie file 60 mins long and a text file tell me five time-lines for breaking the movie. For example, break the movie at 10:21, 16:05�
The final output should be five small jpeg movie files. Each file contains a 90 sec (30 sec before the break time and 60 sec after the break time).
Do you know any java library (jar) contain the library that can help me on programming this? Any existing source code? Any SDK for a movie editor can do that?
Please help.
Thanks
Kenny

Similar Messages

  • Capture a frame from a video clip and save as a photo

    I create a freeze frame in a video clip, select it, and cut it, but it doesn't go into the general copy/paste buffer. How can I export the frame as a photo?

    See my post here for full instructions.
    https://discussions.apple.com/docs/DOC-3231

  • How to capture an image from my usb camera and display on my front panel

    How to capture an image from my usb camera and display on my front panel

    Install NI Vision Acquisition Software and NI IMAQ for USB and open an example.
    Christian

  • How do i export a single jpeg frame from a video file in final cut x

    how do i export a single jpeg frame from a video file in final cut x

    You can also use the 3 finger method of COMMAND+SHIFT+4, then drag across what you want to copy from the image. That will show up on your desktop, which then you can edit using Photoshop, if you want too.
    You can then change the file format from a PNG to either a .JPG or TIFF or even a Photoshop image.
    Make sure, you have stopped the playback, to what you want to save as a still.
    Either method works, to make a still image from video.

  • How to download a form from the web, complete and save it?

    How to download a form from the web, complete and save it?

    Download Adobe Reader. If the form author allows filling out digitally, you can do so and save it again.
    Mylenium

  • Which Version of Adobe do I need to be able to "extract" a page from a existing  file and save/download to another file?

    Which Version of Adobe do I need to be able to "extract" a page from a existing  file and save/download to another file?

    Acrobat Pro or Standard.

  • Capturing a frame from a *.mov file

    I need to capture a frame from a *.mov movie file, but Elements 9 apparently does not recognize that file format.  Does anyone know a way to do what I need to do either by changing the file format to something Elements 9 does recognize or by some other way?  If Elements 9 does recognize the .mov format, what am I doing wrong?  With Elements open, but no file selected, I click on Import/frame from video, then browse to the .mov file after selecting (all files).  When try to open the .mov file,  Elements says it is an unsupported file type.
    I am using Windows Vista.
    Robert Carney

      Hi Robert, you can do this in Windows Movie Maker on Vista
    1) Click Import Media
    2) Navigate to your video folder and select All Files (*.*) from the dropdown
    3) highlight your MOV file and click the Import button
    4) Play your clip and pause where you want the screen grab
    5) On the menu bar click tools and choose Take Picture From Preview
    6) Save as Jpeg
     

  • How to cut random frames in a video file

    Hi,
    I want to know how we can cut random frames in a video file using JMF? Please help!
    thanks in advance.

    Thanks for the answer, Is there any other way to do this using som plugins ot tools?

  • How to extract any picture (frame) from the video file

    I am writing an application that is going to show the grid of tiles. Each tile should have picture that is frame from some particular video.
    Basically something similar to Photos or Videos - standard Windows Phone app.
    The question is how can i extract any picture from the video to put it as picture of the tile or button?

    I pinged you offline.
    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.
    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined
    objects and unknown namespaces.

  • How to write individual frames from a media file to a file.And also play it

    Hi,
    How do i access indivual media frame and save it to another file. One way is to use Cut.java, but i am unable to cut the frames repeatedly.
    I want to Seek an individual frame in the media and then write the frame to a file.
    regards.
    devendra.

    FrameGrabbingControl allows you to grab a frame. but you cant directly write from that frame. Do i have to create a dataSink.
    Actually i tried to do one thing that is, I used the cut program. But from that i can not repeatedly take out frame as media data.

  • How to take a picture from a video file

    Is there a way to take a picture of video clip from the web?  I am trying to pull a pic of a video file, a single frame. Is there an app, or a really easy way to crop and save a photo to my library?

    Pause the video where you want the clip then use shift+command+4 to get a cross hair selector and drag that across the part of the screen you want to capture, it will be saved as Screenshot_date_time to your Desktop

  • Re: Capture the Image from a video file

    Thanks a lot. But when I view the program in the thread, it seems that it needs to some program code is to play the video first and then the image can be captured. Is it possible that even I don't write the code for playing the video first and just write the captured code so that can still capture the image by using Java only from the exists video file. Thanks a lot.

    Thanks a lot. But when I view the program in the thread, it seems that it needs to some program code is to play the video first and then the image can be captured. Is it possible that even I don't write the code for playing the video first and just write the captured code so that can still capture the image by using Java only from the exists video file. Thanks a lot.You can get away without playing the video by seeking to the frame you want using FramePositioningControl ( [example here|http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/solutions/Seek.html] )and then capturing the frame using FrameGrabbingControl (similar to the code I posted). But still you would have to load the video file, create the player using url, realize the player, perhaps prefetch it (all in the link I have given above) then you are ready to seek to particular frame and capture it. But now you don't need to actually start the player so that video/audio is visible/audible.
    Thanks!

  • How do I stop iTunes from duplicating video files?

    For some reason unknown to me, iTunes duplicates the video files when they are added to the library.
    For music, when I add these to the library, the files stay on the external drive, and they are listed in the library.
    However, I now moved my video files to an extrnal drive, but when I want to add them back into my library, it wants to copy all of them (the full file) from the external drive to my itunes folder.  I moved them off of the computer hard drive because they take up too much space.  I don't want them here, but I do want them in the library so I can select and sync them.
    How do I add these videos to my itunes library, but keep the actual files on the extrnal drive?

    Turn off the option "Copy to iTunes Media folder when adding to library" under Edit > Preferences > Advanced.
    tt2

  • How do extract one page from one pdf document and save as a new pdf?

    How do I extract one page from a pdf document and create a new document?

    In Acrobat: Tools - Pages - Extract.
    In Reader it's not possible.
    On Sat, Jan 31, 2015 at 10:29 PM, Ned Murphy <[email protected]>

  • How to make a movie from an audio file and 1 static image?

    Hi,
    I want to make a movie that it is basically an 8 minutes audio file and I want to have a background picture that is static and stays there during the whole 8 minutes of my audio.
    How can I do that?
    I'm able to add the audio and the picture, however I can't stretch the picture to 8 minutes.
    My iMovie version is 5.0.2
    Thanks,
    Martin

    Hi mcanovas - Simply duplicate the picture as many times as needed. In the Photos pane you can extend the pic to 30 seconds, so duplicate that 16 times and there you go!
    I hope the audience will stay focused

Maybe you are looking for