我想从 servlet 或任何其他服务器端 Java 方法中的视频创建缩略图。
视频文件上传到服务器上,上传后应创建缩略图。
我的问题不是创建缩略图,而是仅创建一个或一定数量的缩略图。
到目前为止我的代码:
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);
}
}
}
这里的问题是:为视频中的每一帧创建一个缩略图。在我的 21,012 段测试视频中,创建了超过 1000 个缩略图。
有没有办法从视频中创建缩略图X
Java 中的秒或帧?
编辑1:
我还尝试将 FFMpeg 与 com.day.cq.dam.handler.ffmpeg.FFMpegWrapper 用作 Maven 依赖项的 FFMpegWrapper 一起使用:
<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 https://code.google.com/p/vitalopensource/source/browse/trunk/src/com/vtls/opensource/image/VideoImageSource.java不起作用。
使用 FFMpegWrapper (http://docs.adobe.com/docs/en/cq/5-6/javadoc/com/day/cq/dam/handler/ffmpeg/FFMpegWrapper.html 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、Length=4722688 0 个额外字节
未能实现:com.sun.media.PlaybackEngine@5defbbf
错误:无法实现 com.sun.media.PlaybackEngine@5defbbf
线程“main”中的异常 java.lang.IllegalArgumentException:图像
== 空!在 javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
在 javax.imageio.ImageIO.getWriter(ImageIO.java:1591) 处
javax.imageio.ImageIO.write(ImageIO.java:1520) 在
testVideos.Test2.main(Test2.java:37)
test2-class 中的任何方法都不起作用,并且类测试为我提供了每个帧的缩略图...:(