6

Well, there is FFMPEG and some Java bindings and wrappers for it but I need to distribute for each specific platform the right binary file of FFMPEG.

Isnt there any plain Java solution or library without any dependencies like FFMPEG for converting a video fle to an image sequence?

Solutions like FFMPEG, XUGGLER or JMF (abandoned) are not suitable. Is there really no pure Java solution for this?

Maybe for specific video codecs / files at least?

I just want to extract the images from the video file to jpeg / png files and save them to the disk

  • Since there are so much different encodings available I think ffmpeg is the option you have. – micha Dec 27 '12 at 15:08
  • well but then I need binaries and its not available as pure Java solution –  Dec 27 '12 at 15:10
  • There is [Jave](http://www.sauronsoftware.it/projects/jave/). – asgoth Dec 27 '12 at 15:16
  • Jave needs also specificbinaries for each platform, jffmpeg (real java port?) is maybe one solution, but there has to be a better one. –  Dec 27 '12 at 15:21
  • This Stack Overflow question might be helpful: http://stackoverflow.com/questions/10114413/java-guide-to-write-a-custom-video-codec – Gilbert Le Blanc Dec 27 '12 at 16:05

3 Answers3

2

Is there really no pure Java solution for [extracting images from a video stream]?

Let's see. You have to:

  • Decode the video.
  • Present the decoded images at least as fast as 24 images / second. I suppose you can skip this step.
  • Save the decoded images.

It appears that decoding the video would be the most challenging step. People and companies have spent years developing codecs (encoder / decoder) for various video formats.

There's a project on SourceForge, JMF wrapper for ffmpeg, that has developed a few pure Java video codecs. Perhaps you can look at their source code and see how to develop a Java video codec for yourself.

You can look for other pure Java video codecs if you wish.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • is there some specific codec which would suit best? mjpeg? any ffmpeg wrapper can not be used. –  Dec 27 '12 at 15:48
  • @Daniel Ruf: The one that accurately decodes your video? You need a video codec for every video encoding that you want to process. As I said in my answer, the wrapper project produced some pure Java video codecs. Those pure Java video codecs are what you want. – Gilbert Le Blanc Dec 27 '12 at 15:54
  • I want to choose the best encoding for the first small version and thought about Motion JPEG. But it seems it is not standardized. –  Dec 27 '12 at 15:56
  • 1
    @Daniel Ruf: Take your pick of the formats listed here: http://jffmpeg.sourceforge.net/formats.html – Gilbert Le Blanc Dec 27 '12 at 16:01
2

There is a pure Java implementation of the following codecs: H.264 ( AVC ), MPEG 1/2, Apple ProRes, JPEG; and the following file formats: MP4 ( ISO BMF, QuickTime ), Matroska, MPEG PS and MPEG TS.
The library is called JCodec ( http://www.jcodec.org ).
It has very little documentation for now but the development team is constantly working on this.
Here's how you can simply grab a frame from an MP4 file ( sample from their web site ):

int frameNumber = 150;
BufferedImage frame = FrameGrab.getFrame(new File("filename.mp4"), frameNumber);
ImageIO.write(frame, "png", new File("frame_150.png"));

To add JCodec to your project you can simply add below to your pom.xml:

<dependency>
    <groupId>org.jcodec</groupId>
    <artifactId>jcodec</artifactId>
    <version>0.1.3</version>
</dependency>

For latest version, see here.

Kirill
  • 7,580
  • 6
  • 44
  • 95
Stanislav Vitvitskyy
  • 2,297
  • 20
  • 17
  • well, they failed at creating the website, www.jcodec.org does not work but jcodec.org (without www), sad that they have not a real documentation for the methods, classes ... =( –  May 15 '13 at 10:18
  • I tried their sample, but I get NullPointerException and don't know why. Could someone help please? http://stackoverflow.com/questions/19344051/nullpointerexception-in-framegrab-getframe – Matt Oct 14 '13 at 10:11
  • I would suggest to NOT use jcodec if you need a fast solution. The last version 0.1.10 takes +- 700 ms to grab one frame. It's certainly not suitable for video playback – Jeroen Vervaeke Aug 06 '15 at 09:10
2
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

import javax.imageio.ImageIO;

import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber.Exception;

public class Read{

    public static void main(String []args) throws IOException, Exception, InterruptedException, ExecutionException
    {
        FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber("C:/Users/Digilog/Downloads/Test.mp4");
        frameGrabber.start();
        IplImage i;
        try {
            for(int ii=0;ii<frameGrabber.getLengthInFrames();ii++){

            i = frameGrabber.grab();
            BufferedImage  bi = i.getBufferedImage();
            String path = "D:/Image/Image"+ii+".png";
            ImageIO.write(bi,"png", new File(path));

            }
            frameGrabber.stop();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}