使用JAVA代码从视频文件(.mp4)创建视频预览图像,如youtube或whatsapp

使用JAVA代码从视频文件(.mp4)创建视频预览图像,如youtube或whatsapp,java,ffmpeg,video-processing,jcodec,Java,Ffmpeg,Video Processing,Jcodec,问题1。当我使用FFmpeg Java api时,程序不会在grabber.start()之后运行和打印任何内容。没有生成预览 代码示例: 结果: 接收到生成视频缩略图的请求。 VideoFilePath:/root/appdir/VIDEO20171124143855.mp4 结果文件名:/root/appdir/vdthumb_0.jpg FrameGrabber找到org.bytedeco.javacv。FFmpegFrameGrabber@3529360e 在上述声明之后,不会发生任何事

问题1。当我使用FFmpeg Java api时,程序不会在grabber.start()之后运行和打印任何内容。没有生成预览

代码示例: 结果: 接收到生成视频缩略图的请求。 VideoFilePath:/root/appdir/VIDEO20171124143855.mp4 结果文件名:/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 box上成功生成预览,但我想通过Java程序生成)


问题2。当我使用JCodecAPI时,程序生成一个黑色图像,但不是来自视频文件的图像。 代码示例:

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);

}
结果: 接收到生成视频缩略图的请求。视频文件路径:/usr/appdir/VIDEO20171123165555.mp4,结果文件名/usr/appdir/vdthumb_0.jpg 框架抓取成功。。 帧已成功转换为RGB。。 是否生成预览..true


问题:JCodec生成了一个5 KB的黑色jpg图像,请检查此代码,因为它成功地创建了文件。已经做了一些更改。这是问题1的解决方案。 如果无法查看日志,则问题在于记录器。您可以粘贴正在使用的记录器,也可以用谷歌搜索记录器的问题

public static boolean generatePreviewImage(String filePath,
        String previewFileName) throws IOException, Exception {
    logger.info("Request received to generate thumbnail for video. VideoFilePath : "
            + filePath + ", resultFileName " + previewFileName);
    boolean isPreviewGenerated = false;
    FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(filePath);
    logger.info("FrameGrabber found " + grabber);
    grabber.start();
    logger.info("FrameGrabber started.. " + grabber);
    for (int i = 20; i < 22; i++) {
        logger.info("Reading first 2 images..");
        ImageIO.write(grabber.grab().getBufferedImage(), "jpg", new File(
                previewFileName + "_" + i));
        logger.info(i + " image written successfully as " + previewFileName
                + "_" + i + ".jpg");
        isPreviewGenerated = true;
    }
    grabber.stop();
    logger.info("Is preview generated.." + isPreviewGenerated);
    return isPreviewGenerated;

}
公共静态布尔生成器预览图像(字符串文件路径,
字符串预览文件名)引发IOException,异常{
logger.info(“收到生成视频缩略图的请求。VideoFilePath:”
+文件路径+”,结果文件名“+预览文件名);
布尔值isPreviewGenerated=false;
FFmpegFrameGrabber grabber=新的FFmpegFrameGrabber(文件路径);
logger.info(“找到帧抓取器”+抓取器);
grabber.start();
logger.info(“FrameGrabber已启动..”+grabber);
对于(int i=20;i<22;i++){
logger.info(“读取前两幅图像…”);
write(grabber.grab().getBuffereImage(),“jpg”,新文件(
预览文件名+“”+i));
logger.info(i+)图像已成功写入“+previewFileName”
+“u”+i+”.jpg”);
isPreviewGenerated=true;
}
抓取器。停止();
logger.info(“是否生成预览..”+isPreviewGenerated);
返回isPreviewGenerated;
}

感谢乌尔瓦希提出的所有意见和建议。使用FFMpeg,我的环境无法正常工作。我也没有任何例外。我会再试一次,看看有什么问题

但有了JCodec(第2期),我可以用以下代码生成预览:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.jcodec.api.JCodecException;
import org.jcodec.api.awt.AWTFrameGrab;

public static boolean generatePreviewImage(String filePath, String previewFileName ) throws IOException, JCodecException {
    boolean isPreviewGenerated = false;
    try {
        double sec = 1;
        BufferedImage dst = AWTFrameGrab.getFrame(new File(filePath), sec);
        ImageIO.write(dst, "jpg", new File(previewFileName));
        isPreviewGenerated = true;
        //where filePath is the path of video file and previeFileName is the name of preview image file.    
    } 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;
}
在pom.xml中添加了以下两个依赖项:

<dependency>
    <groupId>org.jcodec</groupId>
    <artifactId>jcodec</artifactId>
    <version>0.2.2</version>
</dependency>
<dependency>
    <groupId>org.jcodec</groupId>
    <artifactId>jcodec-javase</artifactId>
    <version>0.2.1</version>
</dependency>

谢谢

感谢乌尔瓦希的回复,但伐木工人不是问题。。即使在删除记录器并使用编辑的代码后,grabber也没有启动。您使用什么来解决FFmpegFrameGrabber的依赖关系??我使用的是maven dependency from,您也可以使用
System.out.println()
来完成最初的流程。另外,您是否已将代码表单
throws IOException{}
更正为
throws IOException{
?我正在使用maven,它具有以下依赖项org.bytedeco javacv 0.10。我还将每个记录器都更改为System.out.println,并且程序仍然停留在fGrabber.start()语句上。它看起来抓取器甚至没有启动。注意:“{}”在stackoverflow中粘贴代码时,IOException之后是一个输入错误。是的,您使用的此版本出现异常。请使用我之前评论中已共享的版本。单击存储库。使用版本0.8,您的工作就完成了。
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.jcodec.api.JCodecException;
import org.jcodec.api.awt.AWTFrameGrab;

public static boolean generatePreviewImage(String filePath, String previewFileName ) throws IOException, JCodecException {
    boolean isPreviewGenerated = false;
    try {
        double sec = 1;
        BufferedImage dst = AWTFrameGrab.getFrame(new File(filePath), sec);
        ImageIO.write(dst, "jpg", new File(previewFileName));
        isPreviewGenerated = true;
        //where filePath is the path of video file and previeFileName is the name of preview image file.    
    } 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;
}