1

问题 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 图像

4

2 回答 2

2

检查此代码,因为它成功创建了文件。进行了一些更改。这是问题 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;

}
于 2017-11-24T07:57:46.693 回答
1

感谢 Urvashi 的所有意见和建议。使用 FFMpeg,我的环境无法正常工作。我也没有任何例外。我会进一步尝试,看看是什么问题。

但是使用 JCodec (Issue no. 2),我可以使用以下代码生成预览:

在 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>

并在java文件中编写以下代码:

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

谢谢!

于 2017-11-27T09:05:51.280 回答