5
I Googled several terms of how to get the thumbnail of the videos but only found how to "generate" the thumbnail of a frame of the video using the Xuggler
, but I’m making mistakes.
Follow my class down:
package util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
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;
import model.Video;
public class BuildThumbnailUtil {
public static final double SECONDS_BETWEEN_FRAMES = 1;
private static final String outputFilePrefix = "C:/Documents and Settings/Panda/Desktop/MovieList/";
private static int mVideoStreamIndex = -1;
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 buildThumbnail(List<Video> videos) {
for (Video v : videos) {
IMediaReader mediaReader = ToolFactory.makeReader(v.getPath());
try {
mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);
ImageSnapListener isListener = new ImageSnapListener();
mediaReader.addListener(isListener);
while (!isListener.isImageGrabbed()) {
mediaReader.readPacket();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private static class ImageSnapListener extends MediaListenerAdapter {
public boolean imageGrabbed = false;
public void onVideoPicture(IVideoPictureEvent event) {
if (event.getStreamIndex() != mVideoStreamIndex) {
if (mVideoStreamIndex == -1)
mVideoStreamIndex = event.getStreamIndex();
else
return;
}
if (mLastPtsWrite == Global.NO_PTS)
mLastPtsWrite = event.getTimeStamp() - MICRO_SECONDS_BETWEEN_FRAMES;
if (event.getTimeStamp() - mLastPtsWrite >= MICRO_SECONDS_BETWEEN_FRAMES * 10) {
String outputFilename = dumpImageToFile(event.getImage());
this.imageGrabbed = true;
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);
mLastPtsWrite += MICRO_SECONDS_BETWEEN_FRAMES;
}
}
private String dumpImageToFile(BufferedImage image) {
try {
String outputFilename = outputFilePrefix + System.currentTimeMillis() + ".jpg";
System.out.println("Thumbnail image name is going to be : =====>" + outputFilename);
ImageIO.write(image, "jpg", new File(outputFilename));
return outputFilename;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public boolean isImageGrabbed() {
return imageGrabbed;
}
}
}
Does anyone know if they have a way to get the same thumbnail as a video, just like Windows displays?
Pandemonio, you could paste the error message in textual format (images are difficult to see and make life difficult for those who will respond).
– Anthony Accioly