问题 1. 当我使用 FFmpeg Java api 时,程序在grabber.start() 之后没有运行和打印任何内容。未生成预览。
代码示例:
public static boolean generatePreviewImage(String filePath, String previewFileName ) throws IOException {
boolean isPreviewGenerated = false;
System.out.println("Request received to generate thumbnail for video.");
System.out.println("VideoFilePath : "+filePath);
System.out.println("ResultFileName : "+previewFileName);
try {
FFmpegFrameGrabber fGrabber = new FFmpegFrameGrabber(filePath);
System.out.println("FrameGrabber found "+fGrabber);
fGrabber.start();
System.out.println("Frame started..");
ImageIO.write(fGrabber.grab().getBufferedImage(), "jpg", new File(previewFileName));
System.out.println("Image written successfully as "+previewFileName);
isPreviewGenerated = true;
fGrabber.stop();
System.out.println("FrameGrabber stopped.. "+fGrabber);
} catch(Exception e){
System.out.println("Exception while creating video thumbnail : "+previewFileName+" - exception - "+e);
e.printStackTrace();
}
System.out.println("Image written successfully? "+previewFileName);
return isPreviewGenerated;
}
结果:收到为视频生成缩略图的请求。VideoFilePath : /root/appdir/VIDEO20171124143855.mp4 ResultFileName : /root/appdir/vdthumb_0.jpg FrameGrabber 找到 org.bytedeco.javacv.FFmpegFrameGrabber@3529360e
在上述声明之后没有任何反应并被打印出来..
附加信息:我也在 Linux VPS 上安装了 FFmpeg,并且能够使用命令行 root@vps19984[~/usr/appdir]#ffmpeg -i /root/appdir/.VIDEO20171123165555.mp4 -r 1 -f image2 image- 生成预览%2d.png (上面的命令 ffmpeg 在 linux 机器上成功生成预览,但我想通过 Java 程序生成它)
问题 2. 当我使用 JCodec api 时,程序会生成黑色图像,但不会从视频文件中生成图像。代码示例:
public static boolean generatePreviewImage(String filePath, String previewFileName ) throws IOException, JCodecException {
logger.info("Request received to generate thumbnail for video. VideoFilePath : "+filePath+", resultFileName "+previewFileName);
boolean isPreviewGenerated = false;
Picture framePic = FrameGrab.getNativeFrame(new File(filePath),20);
logger.info("Frame grabbed successfully..");
Transform transform = ColorUtil.getTransform(framePic.getColor(), ColorSpace.RGB);
Picture rgb = Picture.create(framePic.getWidth(), framePic.getHeight(), ColorSpace.RGB);
transform.transform(framePic, rgb);
logger.info("Frame transformed successfully to RGB..");
BufferedImage dst = new BufferedImage(rgb.getCroppedWidth(), rgb.getCroppedHeight(),
BufferedImage.TYPE_INT_RGB);
ImageIO.write(dst, "jpg", new File(previewFileName));
isPreviewGenerated = true;
logger.info("Is preview generated.."+isPreviewGenerated);
}
结果:收到为视频生成缩略图的请求。VideoFilePath : /usr/appdir/VIDEO20171123165555.mp4, resultFileName /usr/appdir/vdthumb_0.jpg 抓帧成功.. 帧转换成功.. 预览生成..true
问题:JCodec 生成 5 KB 的黑色 jpg 图像