Capture Frames or Thumbnails of Video Files using Xuggler Library in JAVA

Xuggle_Logo


In this Tutorial, i am going to describe , How to capture Frame or Thumbnails of Video Files using Xuggler library.For using Xuggler you need to download the library version greater than  5.2 i.e. xuggle-xuggler-5.3.jar or latest version, because  earlier version has a large amount of native code that needs to be specifically compiled for different operating systems.
So you can download the Xuggler library form here:
1. http://xuggle.googlecode.com/svn/trunk/repo/share/java/xuggle/xuggle-xuggler/
OR
2.Download from here

 Here is the code:
package com.jkit;
package com.jkit;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.xuggle.mediatool.IMediaReader;
import com.xuggle.mediatool.MediaListenerAdapter;
import com.xuggle.mediatool.ToolFactory;
import com.xuggle.mediatool.event.IVideoPictureEvent;
import com.xuggle.xuggler.Global;

public class ThumbnailCreation {

public static final double SECONDS_BETWEEN_FRAMES = 15;
private static final String inputFilename = "D:/loginInterceptor.mp4";
private static final String outputFilePrefix = "D:/snapshots/mysnapshot";
// The video stream index, used to ensure we display frames from one and
// only one video stream from the media container.
private static int mVideoStreamIndex = -1;
// Time of last frame write
private static long mLastPtsWrite = Global.NO_PTS;
public static final long MICRO_SECONDS_BETWEEN_FRAMES =
(long) (Global.DEFAULT_PTS_PER_SECOND * SECONDS_BETWEEN_FRAMES);

public static void main(String[] args) {
IMediaReader mediaReader = ToolFactory.makeReader(inputFilename);

// we want BufferedImages created in BGR 24bit color space
mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);

mediaReader.addListener(new ImageSnapListener());

// read out the contents of the media file and
// dispatch events to the attached listener
while (mediaReader.readPacket() == null);

}

private static class ImageSnapListener extends MediaListenerAdapter {

public void onVideoPicture(IVideoPictureEvent event) {

if (event.getStreamIndex() != mVideoStreamIndex) {
// if the selected video stream id is not yet set, go ahead an
// select this lucky video stream
if (mVideoStreamIndex == -1) {
mVideoStreamIndex = event.getStreamIndex();
} // no need to show frames from this video stream
else {
return;
}
}

// if uninitialized, back date mLastPtsWrite to get the very first frame
if (mLastPtsWrite == Global.NO_PTS) {
mLastPtsWrite = event.getTimeStamp() - MICRO_SECONDS_BETWEEN_FRAMES;
}

// if it's time to write the next frame
if (event.getTimeStamp() - mLastPtsWrite
>= MICRO_SECONDS_BETWEEN_FRAMES) {

String outputFilename = dumpImageToFile(event.getImage());

// indicate file written
double seconds = ((double) event.getTimeStamp())
/ Global.DEFAULT_PTS_PER_SECOND;
System.out.printf(
"at elapsed time of %6.3f seconds wrote: %s\n",
seconds, outputFilename);

// update last write time
mLastPtsWrite += MICRO_SECONDS_BETWEEN_FRAMES;
}

}

private String dumpImageToFile(BufferedImage image) {
try {
String outputFilename = outputFilePrefix
+ System.currentTimeMillis() + ".png";
ImageIO.write(image, "png", new File(outputFilename));
return outputFilename;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}

Now run the application, you will see the Console output like this:

References:
1. http://www.xuggle.com/xuggler
Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

2 comments: