Video Creation from a set of images with xuggler

Gianfra picture Gianfra · Feb 7, 2012 · Viewed 10.8k times · Source

I have been looking for a solution everywhere! On this website and on others. I have found some interesting things, but they didn't solve my problem. I will explain it.

I have one video, I grad each frame of it with xuggler. When I get all the frames I edit all of them with a color algorithm. Also, I store the audio in an mp3 file.

Now I need to create a video from all the frames, this video, of course, should have the same characteristics as frame rate e duration. After that I have to merge the audio.

I have done the first part, but I don't know how to create a video with the same characteristics. I am following this code:

http://www.javacodegeeks.com/2011/02/xuggler-tutorial-frames-capture-video.html

Can't encode video with Xuggler

But it takes the snapshot and it uses a strange loop:

for (int index = 0; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++)

I can't figure out how to set the right characteristic. It should be easy because I know everything about the video! size, frame rate and number of frame.

My code:

public static void main(String[] args) throws IOException {

    final IMediaWriter writer = ToolFactory.makeWriter(outputFilename);
    writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, 
               720, 304);
    long nextFrameTime = 0;
    final long frameRate =25/1000;
    long startTime = System.nanoTime();
    while (indexVideo<1597) {
        BufferedImage videoImage = null;
        try {
            videoImage = getVideoImage();
        } catch (AWTException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        writer.encodeVideo(0, videoImage,nextFrameTime, 
                TimeUnit.MILLISECONDS);
        nextFrameTime += frameRate;

}
    writer.close();
}

private static BufferedImage getVideoImage() throws IOException, AWTException {

     File imgLoc = new File("D:/Gianfranco/Sample/"+indexVideo+".png");
     BufferedImage img;
    img = ImageIO.read(imgLoc);
    System.out.println(imgLoc.getName());
    indexVideo++;
    return img;

}

Ca anyone help me out?

Answer

mohbandy picture mohbandy · Oct 22, 2013

A quick glance at your code shows me that you aren't setting the frame rate. I don't know what version of xuggler you are using, but for me I set the frame rate when adding video stream:

writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_MPEG4, IRational.make(<frames>/<per second>), 720, 304);

Now another (probably more appropriate) approach would be to create your IMediaWriter by opening your original video.

    IMediaReader reader = ToolFactory.makeReader("inputFile.mp4");
    IMediaWriter writer = ToolFactory.makeWriter("outputFile.mp4", reader);

Or maybe you make it by grabbing the container of the first, then grabbing the format. I've only done this for streaming data and not archived data.