1

Issue 1. When I use FFmpeg Java api, program doesn't run and print anything after grabber.start(). No preview generated.

Code Sample :

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

Result : Request received to generate thumbnail for video. VideoFilePath : /root/appdir/VIDEO20171124143855.mp4 ResultFileName : /root/appdir/vdthumb_0.jpg FrameGrabber found org.bytedeco.javacv.FFmpegFrameGrabber@3529360e

Nothing happens and gets printed after above statement..


Additional Information : I installed FFmpeg on Linux VPS as well and able to generate preview using command line root@vps19984[~/usr/appdir]#ffmpeg -i /root/appdir/.VIDEO20171123165555.mp4 -r 1 -f image2 image-%2d.png (above command ffmpeg generates preview successfully on linux box but I want to generate it via Java program)


Issue 2. When I use JCodec api, program generates a black image but NOT an image from video file. Code Sample :

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

}

Result : Request received to generate thumbnail for video. VideoFilePath : /usr/appdir/VIDEO20171123165555.mp4, resultFileName /usr/appdir/vdthumb_0.jpg Frame grabbed successfully.. Frame transformed successfully to RGB.. Is preview generated..true

Issue : A black jpg image of 5 KB gets generated by JCodec

Ravi Dua
  • 43
  • 1
  • 8

2 Answers2

2

Check this code, as it successfully creates the file. Have made a few changes.This is the solution for Issue1. If you are unable to see the logs, then the issue is with the logger. You can paste the logger you are using or google the issue with your logger.

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;

}
Urvashi Soni
  • 279
  • 1
  • 3
  • 13
  • Thanks for reply Urvashi but Loggers is not an issue.. grabber did not start even after removing the loggers and using your edited code.. – Ravi Dua Nov 24 '17 at 08:30
  • What are you using to resolve the dependency of FFmpegFrameGrabber?? I am using maven dependency from [repository](https://mvnrepository.com/artifact/org.bytedeco/javacv/0.8) Also you can use `System.out.println()` to get through the flow initially. – Urvashi Soni Nov 24 '17 at 09:44
  • Plus have you corrected your code form `throws IOException {}` to `throws IOException {` ?? – Urvashi Soni Nov 24 '17 at 09:52
  • I am using maven with following dependency org.bytedeco javacv 0.10 Also I changed every logger to System.out.println and still the program stuck on fGrabber.start() statement. It looks grabber even not starting. Note: the "{}" after IOException was a typo while pasting code in stackoverflow.. – Ravi Dua Nov 24 '17 at 10:01
  • Yes, this version you are using is giving exception. Please use the version already shared in my previous comment. Click on **repository**. Use version 0.8 and your work is done. – Urvashi Soni Nov 24 '17 at 12:27
  • Urvashi, With version 0.8 it even didn't create the FFmpegFrameGrabber object leave apart the start(). The program stopped one line before .start() were the FFmpegFrameGrabber object is being created. It didn't throw any exception either. Just to inform you that I am running the program on CentOs VPS machine. – Ravi Dua Nov 24 '17 at 13:55
  • Hi Ravi, I am running the program on JBoss Developer Studio (Eclipse like tool) on Fedora24, with both the files created successfully. This seems the issue of your environment then. What is the exception in creating the grabber object? can u paste the same here? – Urvashi Soni Nov 25 '17 at 10:17
  • Urvashi, it has started working with your code now. Thanks for all your help! – Ravi Dua Nov 27 '17 at 13:03
1

Thanks Urvashi for all your comments and suggestion. With FFMpeg, things are not working on my environment. I am not getting any exception either. I will try futher and see what is the issue.

But with JCodec (Issue no. 2) I am able to generate the preview with following code:

In pom.xml added following two dependencies:

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

And in java file written following code:

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

Thanks!

Ravi Dua
  • 43
  • 1
  • 8