我想从servlet或任何其他服务器端Java-Method中的视频创建缩略图。
视频文件上传到服务器上,上传后应创建缩略图。
我的问题不是创建缩略图,而是创建一个或一定数量的缩略图。
我的代码到目前为止:
public class Test {
public static void main(String[] args) throws IOException, InterruptedException, IM4JavaException {
createThumbnail(new File("00-50-C2-1D-7F-85_005.avi"), 512);
}
private static void createThumbnail(File sourceImage, int width) throws IOException,
InterruptedException, IM4JavaException {
ConvertCmd cmd = new ConvertCmd();
String destinationFileName = sourceImage.getName() + "_" + width + "_" + "thumb.png";
File thumbNailFile = new File(destinationFileName);
if (!thumbNailFile.exists()) {
IMOperation op = new IMOperation();
op.addImage(sourceImage.toString());
op.thumbnail(width);
op.addImage(destinationFileName);
cmd.run(op);
}
}
}
这里的问题是:对于视频中的每个帧,都会创建一个缩略图。在我的测试视频中,有超过1000个缩略图的21,012s正在创建。
有没有办法在每X
秒或Java中的帧中从视频创建缩略图?
EDIT1: 我还尝试使用FFMpeg与FFMpegWrapper fom com.day.cq.dam.handler.ffmpeg.FFMpegWrapper用作Maven-depenency:
<dependency>
<groupId>com.day.cq.dam</groupId>
<artifactId>cq-dam-video</artifactId>
<version>5.6.2</version>
</dependency>
与班级:
public class Test2 {
public static void main(String[] args) throws IOException {
String videoName = "Roentgen_A_VisarioG2_005.avi";
File videoFile = new File(videoName);
testFFMpeg(videoFile); // FFMpeg
// lets try VideoImageSource
VideoImageSource viSource = new VideoImageSource(videoFile);
viSource.setMediaTime(10);
BufferedImage thumb = viSource.getImage();
File output = new File(videoName + "_thumb.png");
ImageIO.write(thumb, "png", output);
}
public static void testFFMpeg(File videoFile) throws IOException {
FFMpegWrapper wraper = new FFMpegWrapper(videoFile);
int length = (int) wraper.getInputDuration() / 1000 / 2;
BufferedImage[] thumbnail = wraper.getThumbnails(10, length);
if (thumbnail == null) {
System.out.println("[ERROR] no thumbnail created!");
return;
}
ImageIO.write(thumbnail[0], "png", new File(videoFile.getAbsolutePath() + "_thumb.png"));
}
}
来自https://code.google.com/p/vitalopensource/source/browse/trunk/src/com/vtls/opensource/image/VideoImageSource.java的ViedeoImageSource也不起作用。
使用FFMpegWrapper(http://docs.adobe.com/docs/en/cq/5-6/javadoc/com/day/cq/dam/handler/ffmpeg/FFMpegWrapper.html)我没有收到任何BufferedImage
(getThumnbails()
返回null
),而使用VideoImageSource我得到错误:
无法处理格式:H264,1536x1024,FrameRate = 50.0,长度= 4722688 0个额外字节
未能实现:com.sun.media.PlaybackEngine@5defbbf
错误:无法实现com.sun.media.PlaybackEngine@5defbbf 线程“main”中的异常java.lang.IllegalArgumentException:image == null!在javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925) 在javax.imageio.ImageIO.getWriter(ImageIO.java:1591)at javax.imageio.ImageIO.write(ImageIO.java:1520)at testVideos.Test2.main(Test2.java:37)
test2-class中的方法都没有工作,类测试为每个帧提供了一个缩略图...... :(